Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack and Queue APURBO DATTA.

Similar presentations


Presentation on theme: "Stack and Queue APURBO DATTA."— Presentation transcript:

1 Stack and Queue APURBO DATTA

2 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

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

4 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.

5 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

6 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.

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

8 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

9 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

10 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)

11 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(); };

12 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)

13 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

14 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

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

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

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

18 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

19 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

20 Enqueue Operation Algorithm C Program

21 Dequeue Operation Algorithm C Program

22 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

23 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

24 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.

25 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.

26 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

27 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

28 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;

29 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

30 Comparing queue implementations
(cont.)

31 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

32


Download ppt "Stack and Queue APURBO DATTA."

Similar presentations


Ads by Google