Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Stack and queue

2,255

Published on

Basic info about how to stack and how to use queue.

Basic info about how to stack and how to use queue.

3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
2,255
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
211
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Stack and Queue
• 2. StackA data structure where insertion can only be done in the end part and deletion can only be done in the end part as wellLast-in first-out data structure (LIFO)Supports the following operations push – inserts an item at the end pop – deletes the end item peek – returns the last element
• 3. Stack Study the code below Stack s; for(int i=10; i<25; i+=3) s.push(i); s.display(); s.pop(); s.display(); s.push(100); s.display(); cout<<s.peek()<<endl;
• 4. 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)
• 5. 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(); };
• 6. Array Implementation of Stack The constructor  The push Stack::Stack(){ bool Stack::push(int x){ top = -1; if(isFull()) } return false; The full check items[++top] = x; bool Stack::isFull(){ if(top+1==MAX) return true; return true; } return false;  The pop } bool Stack::pop(){ The empty check if(isEmpty()) bool Stack::isEmpty(){ return false; if(top==-1) return true; top--; return false; return true; } }
• 7. Array Implementation of Stacktop = -1 10 13 16 19 22top=0 top=1 top=2 top=3 top=4
• 8. Array Implementation of Stack 10 13 43 16 19 107 22top=0 top=1 top=2 top=3 top=4
• 9. Array Implementation of Stack The peek int Stack::peek(){ return items[top]; } The display void Stack::display(){ for(int i=top; i>=0; i--) cout<<items[i]<<endl; }
• 10. 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
• 11. Linked-list Implementation of Stack head: tail: 44 97 23 17 9
• 12. Linked-list Implementation of StackPUSH top: 44 97 23 17 9 top:
• 13. Linked-list Implementation of Stack POP head: top: 44 97 23 17 tmp tmp tmp tmp 9 top: del
• 14. Linked-list Implementation of Stack The class Stack can be declared as below class Stack{ private: node *head, *top; public: Stack(); bool push(int); bool pop(); int peek(); //int top(); bool isEmpty(); void display(); ~Stack(); };
• 15. Linked-list Implementation of Stack The constructor Stack::Stack(){ head = top = NULL; } The empty check bool Stack::isEmpty(){ if(top==NULL) return true; return false; }
• 16. Linked-list Implementation of Stack The push bool Stack::push(int x){ node *n = new node(x); if(n==NULL) return false; if(isEmpty()) head = top = n; else{ top->next = n; top = n; } return true; }
• 17. Linked-list Implementation of Stack The pop bool Stack::pop(){ if(isEmpty()) return false; node *tmp = head; node *del; if(tmp == top){ del = top; delete del; head = top = NULL; } else{ while(tmp->next!=top) tmp = tmp->next; del = tmp->next; tmp->next = NULL; top = tmp; delete del; } return true; }
• 18. Doubly-linked List Implementation of Stack Let us review the pop of the singly-linked list implementation of Stack Let’s us change the definition of a node Why not include a pointer to the previous node as well? class node{ public: int item; node *next, *prev; node(int); node(); };
• 19. Doubly-linked List Implementation of Stack top 23 5 90 49 top
• 20. Doubly-linked List Implementation of Stack top 23 5 90 del 49 top
• 21. Doubly-linked List Implementation of Stack The push bool Stack::push(int x){ node *n = new node(x); if(n==NULL) return false; if(isEmpty()) top = n; else{ top->next = n; n->prev = top; top = n; } return true; }
• 22. Doubly-linked List Implementation of StackThe pop bool Stack::pop(){ if(isEmpty()) return false; node *del = top; top = top->prev; if(top!=NULL) top->next = NULL; del->prev = NULL; return true; }
• 23. 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
• 24. QueueQueue<int> q;try{ cout<<q.front()<<endl;}catch(char *msg){ cout<<msg<<endl;}for(int i=10; i<25; i+=3) q.enqueue(i);q.display();q.dequeue();q.display();q.enqueue(100);q.display(); cout<<"front: "<<q.front()<<" rear: "<<q.rear()<<endl;
• 25. Array Implementation of Queue 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 Queue Aside from this, we also need an object that keeps track of where the first and last elements are located  Size will do
• 26. Array Implementation of Queue Our Queue class should look very much like this: const MAX = 100; template <class type> class Queue{ private: int size, items[MAX]; public: Queue(); bool enqueue(type); bool dequeue(); type front(); type rear(); bool isEmpty(); bool isFull(); void display(); };
• 27. Array Implementation of Queue  The enqueue The constructor bool Queue :: enqueue(type x){ Queue:: Queue(){ if(isFull()) size= 0; return false; } items[size++] = x; The full check bool Queue ::isFull(){ return true; if(size==MAX) } return true;  The dequeue return false; bool Queue :: dequeue(){ } if(isEmpty()) The empty check return false; bool Queue ::isEmpty(){ if(size==0) for(int i=1; i<size; i++) return true; items[i-1] = items[i]; return false; size--; } return true; }
• 28. Array Implementation of Queuesize= 0 10 13 16 19 22size=1 size=2 size=3 size=4 size=5
• 29. Array Implementation of Queue 10 13 16 19 22size=1 size=2 size=3 size=4 size=5
• 30. Array Implementation of Queue 13 16 19 22 22size=1 size=2 size=3 size=4
• 31. Circular Array ImplementationDequeue takes O(n) time.This can be improved by simulating a circular array.
• 32. Circular Arrayfront rear front front rear rear rear -4 10 13 16 19 22 2 15 7 5 34
• 33. Array Implementation of Queue Our Queue class should look very much like this: const MAX = 100; template <class type> class Queue{ private: int front, rear, items[MAX]; public: Queue(); bool enqueue(type); bool dequeue(); type front(); type rear(); bool isEmpty(); bool isFull(); void display(); };
• 34. Array Implementation of Queue  The enqueue The constructor bool Queue :: enqueue(type x){ Queue:: Queue(){ front=rear=-1; if(isFull()) size=0; return false; } if(isEmpty()){ The full check front = rear = 0; bool Queue ::isFull(){ items[rear] = x; if(size==MAX) } return true; return false; else{ } rear = (rear + 1)%MAX; The empty check items[rear] = x; bool Queue ::isEmpty(){ } if(size==0) size++; return true; return true; return false; } }
• 35. Circular Array ImplementationThe dequeue bool Queue :: dequeue(){ if(isEmpty()) return false; front=(front+1)%MAX; size--; }