Stack and queue
Upcoming SlideShare
Loading in...5
×
 

Stack and queue

on

  • 1,767 views

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

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

Statistics

Views

Total Views
1,767
Views on SlideShare
1,767
Embed Views
0

Actions

Likes
2
Downloads
151
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Stack and queue Stack and queue Presentation Transcript

  • 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 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
  • 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;
  • 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 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; } }
  • 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=1 top=2 top=3 top=4
  • 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; }
  • 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
  • Linked-list Implementation of Stack head: tail: 44 97 23 17 9
  • Linked-list Implementation of StackPUSH top: 44 97 23 17 9 top:
  • Linked-list Implementation of Stack POP head: top: 44 97 23 17 tmp tmp tmp tmp 9 top: del
  • 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(); };
  • 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; }
  • 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; }
  • 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; }
  • 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(); };
  • Doubly-linked List Implementation of Stack top 23 5 90 49 top
  • Doubly-linked List Implementation of Stack top 23 5 90 del 49 top
  • 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; }
  • 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; }
  • 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
  • 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;
  • 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
  • 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(); };
  • 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; }
  • 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 15 7 5 34
  • 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(); };
  • 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; } }
  • Circular Array ImplementationThe dequeue bool Queue :: dequeue(){ if(isEmpty()) return false; front=(front+1)%MAX; size--; }