SlideShare a Scribd company logo
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 Stack
top = -1



  10        13      16      19      22


top=0      top=1   top=2   top=3   top=4
Array Implementation of Stack


 10      13      43
                 16      19
                         107     22


top=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;
  }
Stack and queue
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 Stack
PUSH
                            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
Queue
Queue<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 Queue
size= 0



 10       13   16     19     22


size=1 size=2 size=3 size=4 size=5
Array Implementation of Queue


   10   13    16   19     22


size=1 size=2 size=3 size=4 size=5
Array Implementation of Queue


   13    16   19    22    22


size=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 Array

front
 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--;
 }

More Related Content

PPTX
Linked list
PPSX
Data Structure (Queue)
PDF
Python multithreaded programming
PPTX
Stacks IN DATA STRUCTURES
PPT
Data Structure and Algorithms Linked List
PPTX
stack & queue
PPTX
Data structure using c module 1
Linked list
Data Structure (Queue)
Python multithreaded programming
Stacks IN DATA STRUCTURES
Data Structure and Algorithms Linked List
stack & queue
Data structure using c module 1

What's hot (20)

PPSX
Modules and packages in python
PPTX
Python Datatypes by SujithKumar
PPTX
PPT
PPTX
Linked List
PPTX
Priority Queue in Data Structure
PPT
Vi editor in linux
PPTX
Queue in Data Structure
PPTX
PPTX
sorting and its types
PPTX
Stacks and Queue - Data Structures
PDF
Data Structures & Algorithm design using C
PPTX
Hashing
PDF
Linear search algorithm
PPTX
Binary Tree in Data Structure
PPTX
What is Link list? explained with animations
PPTX
Python SQite3 database Tutorial | SQlite Database
PPTX
Tree - Data Structure
PPTX
Data structure - Graph
Modules and packages in python
Python Datatypes by SujithKumar
Linked List
Priority Queue in Data Structure
Vi editor in linux
Queue in Data Structure
sorting and its types
Stacks and Queue - Data Structures
Data Structures & Algorithm design using C
Hashing
Linear search algorithm
Binary Tree in Data Structure
What is Link list? explained with animations
Python SQite3 database Tutorial | SQlite Database
Tree - Data Structure
Data structure - Graph
Ad

Viewers also liked (20)

PPTX
Stack and Queue
PPT
Stack & queue
PDF
Stack and Queue (brief)
PPTX
single linked list
PPT
comp.org Chapter 2
PPTX
Method overloading
PPTX
PPT
Computer Organization and Assembly Language
PPTX
Computer organization and architecture
PPT
Input Output Operations
PPT
Queue and stacks
PPTX
PDF
Computer architecture
PPTX
Data structures and algorithms
PPTX
Computer Architecture – An Introduction
PPTX
Input Output Organization
PPTX
Computer architecture and organization
PPT
Linked lists
Stack and Queue
Stack & queue
Stack and Queue (brief)
single linked list
comp.org Chapter 2
Method overloading
Computer Organization and Assembly Language
Computer organization and architecture
Input Output Operations
Queue and stacks
Computer architecture
Data structures and algorithms
Computer Architecture – An Introduction
Input Output Organization
Computer architecture and organization
Linked lists
Ad

Similar to Stack and queue (20)

PDF
Chapter 5 Stack and Queue.pdf
PPT
StacksandQueues.pptnajaiananaajaoakanabjana
PPTX
Chapter 4 data structure and algorithm - Stacks and Queues
PPTX
U3.stack queue
PPT
03 stacks and_queues_using_arrays
PPT
23 stacks-queues-deques
PPT
Ch03_stacks_and_queues.ppt
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPT
Stack queue
PPTX
Stack and its applications
PPTX
Stack in Sata Structure
PPTX
My lecture stack_queue_operation
PDF
1- The design of a singly-linked list below is a picture of the functi (1).pdf
PPTX
Data Structures Algorithms and Applications
Chapter 5 Stack and Queue.pdf
StacksandQueues.pptnajaiananaajaoakanabjana
Chapter 4 data structure and algorithm - Stacks and Queues
U3.stack queue
03 stacks and_queues_using_arrays
23 stacks-queues-deques
Ch03_stacks_and_queues.ppt
Stack queue
Stack queue
Stack queue
Stack queue
Stack queue
Stack queue
Stack queue
Stack and its applications
Stack in Sata Structure
My lecture stack_queue_operation
1- The design of a singly-linked list below is a picture of the functi (1).pdf
Data Structures Algorithms and Applications

More from Katang Isip (7)

PPSX
Reflection paper
PPSX
Punctuation tips
PPSX
3 act story
PPTX
Class list data structure
PPTX
Hash table and heaps
PPT
Binary Search Tree and AVL
PPT
Time complexity
Reflection paper
Punctuation tips
3 act story
Class list data structure
Hash table and heaps
Binary Search Tree and AVL
Time complexity

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Lesson notes of climatology university.
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Classroom Observation Tools for Teachers
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
master seminar digital applications in india
PDF
Anesthesia in Laparoscopic Surgery in India
Cell Structure & Organelles in detailed.
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
Chinmaya Tiranga quiz Grand Finale.pdf
Weekly quiz Compilation Jan -July 25.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
STATICS OF THE RIGID BODIES Hibbelers.pdf
Lesson notes of climatology university.
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Microbial diseases, their pathogenesis and prophylaxis
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Classroom Observation Tools for Teachers
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Orientation - ARALprogram of Deped to the Parents.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
master seminar digital applications in india
Anesthesia in Laparoscopic Surgery in India

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