Stack and Queue
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 w...
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.pus...
Array Implementation of Stack Just like the array implementation of the List, we  also need the array of items where we a...
Array Implementation of Stack Our Stack class should look very much like this:   const MAX = 100;   class Stack{   privat...
Array Implementation of Stack The constructor             The push    Stack::Stack(){             bool Stack::push(int x...
Array Implementation of Stacktop = -1  10        13      16      19      22top=0      top=1   top=2   top=3   top=4
Array Implementation of Stack 10      13      43                 16      19                         107     22top=0   top=...
Array Implementation of Stack The peek  int Stack::peek(){     return items[top];  } The display  void Stack::display(){...
Linked-list Implementation of StackThis implementation is the linked-list implementation of the list except for the follo...
Linked-list Implementation of Stack    head:                 tail:    44      97       23   17                 9
Linked-list Implementation of StackPUSH                            top:    44     97          23   17                9    ...
Linked-list Implementation of Stack POP       head:                      top:       44      97          23     17       tm...
Linked-list Implementation of Stack The class Stack can be declared as below   class Stack{   private:      node *head, *...
Linked-list Implementation of Stack The constructor  Stack::Stack(){    head = top = NULL;  } The empty check  bool Stac...
Linked-list Implementation of Stack The push  bool Stack::push(int x){    node *n = new node(x);      if(n==NULL)        ...
Linked-list Implementation of Stack The pop   bool Stack::pop(){       if(isEmpty())           return false;       node *...
Doubly-linked List Implementation             of Stack Let us review the pop of the singly-linked list  implementation of...
Doubly-linked List Implementation            of Stack                           top   23          5           90          ...
Doubly-linked List Implementation            of Stack                         top  23          5          90        del   ...
Doubly-linked List Implementation             of Stack The push  bool Stack::push(int x){    node *n = new node(x);      ...
Doubly-linked List Implementation            of StackThe pop bool Stack::pop(){   if(isEmpty())      return false;   node...
Queue The Queue is like the List but with “limited”  insertion and deletion. Insertion can be done only at the end or re...
QueueQueue<int> q;try{        cout<<q.front()<<endl;}catch(char *msg){        cout<<msg<<endl;}for(int i=10; i<25; i+=3)  ...
Array Implementation of Queue Just like the array implementation of the List, we  also need the array of items where we a...
Array Implementation of Queue Our Queue class should look very much like this:   const MAX = 100;   template <class type>...
Array Implementation of Queue                               The enqueue The constructor                bool Queue :: enq...
Array Implementation of Queuesize= 0 10       13   16     19     22size=1 size=2 size=3 size=4 size=5
Array Implementation of Queue   10   13    16   19     22size=1 size=2 size=3 size=4 size=5
Array Implementation of Queue   13    16   19    22    22size=1 size=2 size=3 size=4
Circular Array ImplementationDequeue takes O(n) time.This can be improved by simulating a circular array.
Circular Arrayfront rear   front   front                        rear rear   rear -4 10     13      16      19     22   2  ...
Array Implementation of Queue Our Queue class should look very much like this:   const MAX = 100;   template <class type>...
Array Implementation of Queue                               The enqueue The constructor                bool Queue :: enq...
Circular Array ImplementationThe dequeue bool Queue :: dequeue(){    if(isEmpty())       return false;    front=(front+1)...
Stack and queue
Upcoming SlideShare
Loading in...5
×

Stack and queue

2,330

Published on

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

Published in: Education, Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,330
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
218
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Stack and queue

  1. 1. Stack and Queue
  2. 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. 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. 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. 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. 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. 7. Array Implementation of Stacktop = -1 10 13 16 19 22top=0 top=1 top=2 top=3 top=4
  8. 8. Array Implementation of Stack 10 13 43 16 19 107 22top=0 top=1 top=2 top=3 top=4
  9. 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. 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. 11. Linked-list Implementation of Stack head: tail: 44 97 23 17 9
  12. 12. Linked-list Implementation of StackPUSH top: 44 97 23 17 9 top:
  13. 13. Linked-list Implementation of Stack POP head: top: 44 97 23 17 tmp tmp tmp tmp 9 top: del
  14. 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. 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. 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. 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. 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. 19. Doubly-linked List Implementation of Stack top 23 5 90 49 top
  20. 20. Doubly-linked List Implementation of Stack top 23 5 90 del 49 top
  21. 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. 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. 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. 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. 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. 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. 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. 28. Array Implementation of Queuesize= 0 10 13 16 19 22size=1 size=2 size=3 size=4 size=5
  29. 29. Array Implementation of Queue 10 13 16 19 22size=1 size=2 size=3 size=4 size=5
  30. 30. Array Implementation of Queue 13 16 19 22 22size=1 size=2 size=3 size=4
  31. 31. Circular Array ImplementationDequeue takes O(n) time.This can be improved by simulating a circular array.
  32. 32. Circular Arrayfront rear front front rear rear rear -4 10 13 16 19 22 2 15 7 5 34
  33. 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. 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. 35. Circular Array ImplementationThe dequeue bool Queue :: dequeue(){ if(isEmpty()) return false; front=(front+1)%MAX; size--; }
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×