Stack and Queue APURBO DATTA.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures & Algorithms
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
More on Linked Lists, Stacks, and Queues
COSC160: Data Structures: Lists and Queues
CC 215 Data Structures Queue ADT
Queue data structure.
Stacks and Queues.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
Pointers and Linked Lists
Chapter 19: Stacks and Queues.
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Queues: Implemented using Arrays
Stacks and Queues.
Stacks CS-240 Dick Steflik.
CSI 1340 Introduction to Computer Science II
Queues Definition of a Queue Examples of Queues
Queues: Implemented using Linked Lists
CSCS-200 Data Structure and Algorithms
Data Structures & Programming
Presentation transcript:

Stack and Queue APURBO DATTA

Overview what is stack ? Stack in Programming. Basic operations of stack(Pushing, popping etc.) Implementation of Stacks Queue Definition Basic operations of queue Enqueuing, dequeuing etc. Implementation of queue

Stack Overview Stack Definition what is stack ? Condition of stack. Stack in Programming. Basic operations of stack(Pushing, popping etc.) Implementation of Stacks

Stack Definition We know that the Stack is LIFO Structure i,e Last in First Out. It is very useful data structure in C Programming. Stack can be implemented using the Linked List or Array.

Condition of stack Stack is LIFO Structure [ Last in First Out ] Stack is Ordered List of Elements of Same Type. Stack is Linear List In Stack all Operations such as Insertion and Deletion are permitted at only one end called Top

Stack in Programming Similar is the idea of stack in Programming 1. You can add an element onto the stack. 2. You can remove the top most element. 3. You can see the top most element.

Fundamental operations Push: Equivalent to an insert Pop: Deletes the most recently inserted element Top: Examines the most recently inserted element

Push and Pop Push Pop Add an element to the top of the stack Remove the element at the top of the stack empty stack push an element push another pop top B top top A A A top

Implementation of Stacks Any list implementation could be used to implement a stack Arrays (static: the size of stack is given initially) Linked lists (dynamic: never become full) We will explore implementations based on array and linked list Let’s see how to use an array to implement a stack first

Array Implementation of Stack Just like the array implementation of the List, we also need the array of items where we are going to store the elements of the Stack Aside from this, we also need an object that keeps track of where the last element is located From this point on, we are going to call it the top top is simply an integer (very much like the head in the cursor implementation of the List)

Array Implementation of Stack Our Stack class should look very much like this: const MAX = 100; class Stack{ private: int top, items[MAX]; public: Stack(); bool push(int); bool pop(); int peek(); //int top(); bool isEmpty(); bool isFull(); void display(); };

Array Implementation of Stack The push bool Stack::push(int x){ if(isFull()) return false; items[++top] = x; return true; } The pop bool Stack::pop(){ if(isEmpty()) top--; The constructor Stack::Stack(){ top = -1; } The full check bool Stack::isFull(){ if(top+1==MAX) return true; return false; The empty check bool Stack::isEmpty(){ if(top==-1)

Array Implementation of Stack top = -1 10 13 16 19 22 top = 0 top =1 top =2 top =3 top =4 10 13 16 19 22 top = 0 top =1 top =2 top =3 top =4

Linked-list Implementation of Stack This implementation is the linked-list implementation of the list except for the following operations General insert and append General delete 44 97 23 17 head: tail: 9

Linked-list Implementation of Stack PUSH top: 44 97 23 17 9 top:

Linked-list Implementation of Stack pop 44 97 23 17 top: tmp del 9

Queue Overview Queue Definition Basic operations of queue Enqueuing, dequeuing etc. Implementation of queue Array Linked list

What is Queue The Queue is like the List but with “limited” insertion and deletion. Insertion can be done only at the end or rear Deletion can only be done in the front FIFO – first-in-first-out data structure Operations enqueue dequeue

Basic operations of queue Primary queue operations: Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear. Enqueue Insert an element at the rear of the queue Dequeue Remove an element from the front of the queue Insert (Enqueue) Remove (Dequeue) front rear

Enqueue Operation Algorithm C Program

Dequeue Operation Algorithm C Program

Storing a queue in a static data structure This implementation stores the queue in an array. The array indices at which the head and tail of the queue are currently stored must be maintained. The head of the queue is not necessarily at index 0. The array can be a “circular array” – the queue “wraps round” if the last index of the array is reached. Example – storing a queue in an array of length 5

Storing a queue in a dynamic data structure Each node in a dynamic data structure contains data AND a reference to the next node. A queue also needs a reference to the head node AND a reference to the tail node. The following diagram describes the storage of a queue called Queue. Each node consists of data (DataItem) and a reference (NextNode). The first node is accessed using the name Queue.Head. Its data is accessed using Queue.Head.DataItem The second node is accessed using Queue.Head.NextNode The last node is accessed using Queue.Tail

Adding a node (Add) in a dynamic data structure The new node is to be added at the tail of the queue. The reference Queue.Tail should point to the new node, and the NextNode reference of the node previously at the tail of the queue should point to the DataItem of the new node.

Removing a node (Remove) in a dynamic data structure The value of Queue.Head.DataItem is returned. A temporary reference Temp is declared and set to point to head node in the queue (Temp = Queue.Head). Queue.Head is then set to point to the second node instead of the top node. The only reference to the original head node is now Temp and the memory used by this node can then be freed.

Implementation of Queue Just as stacks can be implemented as arrays or linked lists, so with queues. Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks

Array Implementation of Queue 10 13 16 19 22 size=5 size=1 size=2 size=3 size=4 10 13 16 29 22 size=1 size=2 size=3 size=4 size=5

Implement queue using linked list in c int main(){ /* Drive code to test the implementation. */ // Printing elements in Queue after each Enqueue or Dequeue Enqueue(2); Print(); Enqueue(4); Print(); Enqueue(6); Print(); Dequeue(); Print(); Enqueue(8); Print(); queue using linked list in c // To Dequeue an integer. void Dequeue() { struct Node* temp = front; if(front == NULL) { printf("Queue is Empty\n"); return; } if(front == rear) { front = rear = NULL; else { front = front->next; free(temp); int Front() { printf("Queue is empty\n"); return front->data; void Print() { while(temp != NULL) { printf("%d ",temp->data); temp = temp->next; printf("\n"); /*Queue - Linked List implementation*/ #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node* next; }; // Two l variables to store address of front and rear nodes. struct Node* front = NULL; struct Node* rear = NULL; // To Enqueue an integer void Enqueue(int x) struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); temp->data =x; temp->next = NULL; if(front == NULL && rear == NULL){ front = rear = temp; return; } rear->next = temp; rear = temp;

Comparing queue implementations Memory requirements Array-based implementation Assume a queue (size: 100) of strings (80 bytes each) Assume indices take 2 bytes Total memory: (80 bytes x 101 slots) + (2 bytes x 2 indexes) = 8084 bytes Linked-list-based implementation Assume pointers take 4 bytes Total memory per node: 80 bytes + 4 bytes = 84 bytes

Comparing queue implementations (cont.)

Big-O Comparison of Queue Operations Comparing queue implementations Big-O Comparison of Queue Operations Operation Array Implementation Linked Implementation Class constructor O(1) MakeEmpty O(N) IsFull IsEmpty Enqueue Dequeue Destructor