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

What's hot (20)

DOCX
Python unit 3 and Unit 4
Anandh Arumugakan
 
PDF
Array data structure
maamir farooq
 
PPTX
Decision Making & Loops
Akhil Kaushik
 
PDF
Python list
Mohammed Sikander
 
PPTX
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
PPTX
Tree Traversal
Md. Israil Fakir
 
PPTX
Stack - Data Structure
Bhavesh Sanghvi
 
PPTX
My lectures circular queue
Senthil Kumar
 
PPTX
Sorting Algorithms
Pranay Neema
 
PPTX
Linked List
Ashim Lamichhane
 
PPTX
Data structure by Digvijay
Digvijay Singh Karakoti
 
PPTX
Unit II - LINEAR DATA STRUCTURES
Usha Mahalingam
 
PPTX
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
PPTX
Constructor and destructor
Shubham Vishwambhar
 
PPTX
BINARY SEARCH TREE
Usha Mahalingam
 
DOCX
Stack - Operations and Applications
Sagacious IT Solution
 
PPT
Stack
srihariyenduri
 
PPTX
Stacks in DATA STRUCTURE
Mandeep Singh
 
PPT
RECURSION IN C
v_jk
 
Python unit 3 and Unit 4
Anandh Arumugakan
 
Array data structure
maamir farooq
 
Decision Making & Loops
Akhil Kaushik
 
Python list
Mohammed Sikander
 
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Tree Traversal
Md. Israil Fakir
 
Stack - Data Structure
Bhavesh Sanghvi
 
My lectures circular queue
Senthil Kumar
 
Sorting Algorithms
Pranay Neema
 
Linked List
Ashim Lamichhane
 
Data structure by Digvijay
Digvijay Singh Karakoti
 
Unit II - LINEAR DATA STRUCTURES
Usha Mahalingam
 
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
Constructor and destructor
Shubham Vishwambhar
 
BINARY SEARCH TREE
Usha Mahalingam
 
Stack - Operations and Applications
Sagacious IT Solution
 
Stacks in DATA STRUCTURE
Mandeep Singh
 
RECURSION IN C
v_jk
 

Viewers also liked (20)

PPTX
stack & queue
manju rani
 
PPTX
Stack and Queue
Apurbo Datta
 
PPT
Stack & queue
Siddique Ibrahim
 
PDF
Stack and Queue (brief)
Sanjay Saha
 
PPTX
single linked list
Sathasivam Rangasamy
 
PPT
comp.org Chapter 2
Rajat Sharma
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
Data structures
Lovely Professional University
 
PPT
Computer Organization and Assembly Language
fasihuddin90
 
PPTX
Computer organization and architecture
Subesh Kumar Yadav
 
PPT
Input Output Operations
kdisthere
 
PPT
Queue and stacks
grahamwell
 
PPTX
Ebw
pankaj sharma
 
PDF
Computer architecture
Zuhaib Zaroon
 
PPTX
Data structures and algorithms
Julie Iskander
 
PPTX
Computer Architecture – An Introduction
Dilum Bandara
 
PPTX
Input Output Organization
Kamal Acharya
 
PPTX
Computer architecture and organization
Tushar B Kute
 
stack & queue
manju rani
 
Stack and Queue
Apurbo Datta
 
Stack & queue
Siddique Ibrahim
 
Stack and Queue (brief)
Sanjay Saha
 
single linked list
Sathasivam Rangasamy
 
comp.org Chapter 2
Rajat Sharma
 
Method overloading
Lovely Professional University
 
Computer Organization and Assembly Language
fasihuddin90
 
Computer organization and architecture
Subesh Kumar Yadav
 
Input Output Operations
kdisthere
 
Queue and stacks
grahamwell
 
Computer architecture
Zuhaib Zaroon
 
Data structures and algorithms
Julie Iskander
 
Computer Architecture – An Introduction
Dilum Bandara
 
Input Output Organization
Kamal Acharya
 
Computer architecture and organization
Tushar B Kute
 
Ad

Similar to Stack and queue (20)

PPT
03 stacks and_queues_using_arrays
tameemyousaf
 
PPT
Stack linked list
bhargav0077
 
PDF
Please review my code (java)Someone helped me with it but i cannot.pdf
fathimafancyjeweller
 
PPTX
U3.stack queue
Ssankett Negi
 
PPTX
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
PDF
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
arorastores
 
PPT
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
partho5958
 
PPT
Lect-28-Stack-Queue.ppt
ThekkepatSankalp
 
PPT
Stack queue
James Wong
 
PPT
Stack queue
Tony Nguyen
 
PPT
Stack queue
Young Alista
 
PPT
Stack queue
Luis Goldster
 
PPT
Stack queue
Hoang Nguyen
 
PPT
Stack queue
Harry Potter
 
PPT
Stack queue
Fraboni Ec
 
PDF
Stacks
Sadaf Ismail
 
PPTX
6 - STACKS in Data Structure and Algorithm.pptx
RahulRaj493025
 
PPT
Link list part 2
Anaya Zafar
 
PDF
Datastructures asignment
sreekanth3dce
 
03 stacks and_queues_using_arrays
tameemyousaf
 
Stack linked list
bhargav0077
 
Please review my code (java)Someone helped me with it but i cannot.pdf
fathimafancyjeweller
 
U3.stack queue
Ssankett Negi
 
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
arorastores
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
partho5958
 
Lect-28-Stack-Queue.ppt
ThekkepatSankalp
 
Stack queue
James Wong
 
Stack queue
Tony Nguyen
 
Stack queue
Young Alista
 
Stack queue
Luis Goldster
 
Stack queue
Hoang Nguyen
 
Stack queue
Harry Potter
 
Stack queue
Fraboni Ec
 
Stacks
Sadaf Ismail
 
6 - STACKS in Data Structure and Algorithm.pptx
RahulRaj493025
 
Link list part 2
Anaya Zafar
 
Datastructures asignment
sreekanth3dce
 
Ad

More from Katang Isip (7)

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

Recently uploaded (20)

PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 

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