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

Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
Bhavesh Sanghvi
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
CIIT Atd.
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
sumitbardhan
 
stack presentation
stack presentationstack presentation
Stacks
StacksStacks
Stacks
sweta dargad
 
Stack
StackStack
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
Dr. Jasmine Beulah Gnanadurai
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
Srajan Shukla
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
Selvin Josy Bai Somu
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
Vrushali Dhanokar
 

What's hot (20)

Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
stack presentation
stack presentationstack presentation
stack presentation
 
Stacks
StacksStacks
Stacks
 
Stack
StackStack
Stack
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Heaps
HeapsHeaps
Heaps
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Linked List
Linked ListLinked List
Linked List
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 

Viewers also liked

Stack & queue
Stack & queueStack & queue
Stack & queue
Siddique Ibrahim
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)
Sanjay Saha
 
comp.org Chapter 2
comp.org Chapter 2comp.org Chapter 2
comp.org Chapter 2Rajat Sharma
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Data structures
Data structuresData structures
Computer Organization and Assembly Language
Computer Organization and Assembly LanguageComputer Organization and Assembly Language
Computer Organization and Assembly Language
fasihuddin90
 
Computer organization and architecture
Computer organization and architectureComputer organization and architecture
Computer organization and architectureSubesh Kumar Yadav
 
Input Output Operations
Input Output OperationsInput Output Operations
Input Output Operationskdisthere
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacksgrahamwell
 
Ebw
EbwEbw
Computer architecture
Computer architectureComputer architecture
Computer architecture
Zuhaib Zaroon
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
ELECTRON BEAM WELDING (EBW) PPT
ELECTRON BEAM WELDING (EBW) PPTELECTRON BEAM WELDING (EBW) PPT
Computer Architecture – An Introduction
Computer Architecture – An IntroductionComputer Architecture – An Introduction
Computer Architecture – An Introduction
Dilum Bandara
 
Input Output Organization
Input Output OrganizationInput Output Organization
Input Output Organization
Kamal Acharya
 
Computer architecture and organization
Computer architecture and organizationComputer architecture and organization
Computer architecture and organization
Tushar B Kute
 
Linked lists
Linked listsLinked lists
Linked lists
SARITHA REDDY
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 

Viewers also liked (20)

Stack & queue
Stack & queueStack & queue
Stack & queue
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)
 
single linked list
single linked listsingle linked list
single linked list
 
comp.org Chapter 2
comp.org Chapter 2comp.org Chapter 2
comp.org Chapter 2
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
group 7
group 7group 7
group 7
 
Data structures
Data structuresData structures
Data structures
 
Computer Organization and Assembly Language
Computer Organization and Assembly LanguageComputer Organization and Assembly Language
Computer Organization and Assembly Language
 
Computer organization and architecture
Computer organization and architectureComputer organization and architecture
Computer organization and architecture
 
Input Output Operations
Input Output OperationsInput Output Operations
Input Output Operations
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
 
Ebw
EbwEbw
Ebw
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
ELECTRON BEAM WELDING (EBW) PPT
ELECTRON BEAM WELDING (EBW) PPTELECTRON BEAM WELDING (EBW) PPT
ELECTRON BEAM WELDING (EBW) PPT
 
Computer Architecture – An Introduction
Computer Architecture – An IntroductionComputer Architecture – An Introduction
Computer Architecture – An Introduction
 
Input Output Organization
Input Output OrganizationInput Output Organization
Input Output Organization
 
Computer architecture and organization
Computer architecture and organizationComputer architecture and organization
Computer architecture and organization
 
Linked lists
Linked listsLinked lists
Linked lists
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 

Similar to Stack and queue

03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
fathimafancyjeweller
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
Ssankett Negi
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
arorastores
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
partho5958
 
Lect-28-Stack-Queue.ppt
Lect-28-Stack-Queue.pptLect-28-Stack-Queue.ppt
Lect-28-Stack-Queue.ppt
ThekkepatSankalp
 
Stack queue
Stack queueStack queue
Stack queue
James Wong
 
Stack queue
Stack queueStack queue
Stack queue
Fraboni Ec
 
Stack queue
Stack queueStack queue
Stack queue
Hoang Nguyen
 
Stacks
StacksStacks
Stacks
Sadaf Ismail
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
Anaya Zafar
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignmentsreekanth3dce
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 

Similar to Stack and queue (20)

03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.pptlecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
lecture10trsgfchjvxgfzfdchgdchgcgshyjh.ppt
 
Lect-28-Stack-Queue.ppt
Lect-28-Stack-Queue.pptLect-28-Stack-Queue.ppt
Lect-28-Stack-Queue.ppt
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stacks
StacksStacks
Stacks
 
Stack
StackStack
Stack
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 

More from Katang Isip

Reflection paper
Reflection paperReflection paper
Reflection paperKatang Isip
 
Punctuation tips
Punctuation tipsPunctuation tips
Punctuation tipsKatang Isip
 
Class list data structure
Class list data structureClass list data structure
Class list data structure
Katang Isip
 
Hash table and heaps
Hash table and heapsHash table and heaps
Hash table and heaps
Katang Isip
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
Katang Isip
 
Time complexity
Time complexityTime complexity
Time complexity
Katang Isip
 

More from Katang Isip (7)

Reflection paper
Reflection paperReflection paper
Reflection paper
 
Punctuation tips
Punctuation tipsPunctuation tips
Punctuation tips
 
3 act story
3 act story3 act story
3 act story
 
Class list data structure
Class list data structureClass list data structure
Class list data structure
 
Hash table and heaps
Hash table and heapsHash table and heaps
Hash table and heaps
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Time complexity
Time complexityTime complexity
Time complexity
 

Recently uploaded

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 

Recently uploaded (20)

How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 

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; }
  • 10.
  • 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--; }