SlideShare una empresa de Scribd logo
1 de 70
Chapter 7
Stacks and Queues



                    1
Stack ADT
• Recall that ADT is abstract data type, a set
  of data and a set of operations that act
  upon the data.
• In a stack, the set of data is the stack of
  elements.
• Stack is known as a LIFO (last-in-first-out)
  data structure because the last data to
  enter the stack is the first to exit the stack.
                                                2
Stack ADT Operations
• push: places an element onto the top of a
  stack.
• pop: removes an element from the top of the
  stack.
• peek: which retrieves (copies) a value from the
  top of the stack without removing it.
• an operation to determine whether or not the
  stack is empty.
• an operation to empty out a stack.

                                                    3
Push
• Push means place a new data element at
  the top of the stack


                         11

                          5

         3               17

                        stack              4
Push (cont.)
• Push means place a new data element at
  the top of the stack


                         11
             3
                          5

                         17

                        stack              5
Push (cont.)
• Push means place a new data element at
  the top of the stack
                          3

                         11

                          5

                         17

                        stack              6
Pop
• Pop means take a data element off the top
  of the stack
                           3

                           11

                           5

                           17

                          stack           7
Pop (cont.)
• Pop means take a data element off the top
  of the stack

                  3
                           11

                           5

                           17

                          stack           8
Pop (cont.)
• Pop means take a data element off the top
  of the stack


                           11

                           5

          3                17

                          stack           9
Peek
• Peek means retrieve the top of the stack
  without removing it
                            3

                           11

                            5

          3                17

                          stack              10
Array Stack Class Template
3    template <typename T>
4    class Stack {
5    public:
6           Stack() { … }
7           ~Stack( ) { … }
8           void push( T& elementToPush ) { … }
9           bool pop( T& poppedElement ) { … }
10          bool peek( T& topElement ) { … }
11          bool isEmpty( ) const { … }
12          void makeEmpty( ) { … }
13    private:
14          T* elements; // dynamic array used as an
15          int size;                     index to the top
16          int top;                      of the stack
17    };
                                                        11
Array Stack
     Constructor and Destructor

3    Stack () : top(-1), size(0)
4    {
5      elements = NULL;
6    }
7
8    ~Stack( )
9    {
10     makeEmpty( );
11   }

                                   12
Array Stack
         isEmpty and makeEmpty
65   bool isEmpty( ) const
66   {
67     return top == -1;
68   }
69
70   void makeEmpty( )
71   {
72      size = 0;
73      top = -1;
74      delete [] elements;
75    }


                                 13
Array Stack Pop

       elements


          125 25    200 70
           0   1    2     3
                         top
An element can’t really be removed from an
array, as one would think pop would achieve.



                                               14
Array Stack Pop
               (cont.)
       elements
                                         client

          125 25     200 70
           0    1    2     3
                          top
The element 70 is at the top of the stack, and
what really happens during a pop, is that 70 is
returned to the client…


                                                  15
Array Stack Pop
             (cont.)
      elements
                              client

         125 25   200 70
          0   1    2      3
                  top
and top is decremented…




                                       16
Array Stack Pop
               (cont.)
        elements
                                            client

           125 25     200 70
            0    1     2     3
                      top
The element 70 is still in the array, but it is no
longer accessible. The next push will
overwrite it. Say, we would like to push 63…


                                                     17
Array Stack Push

       elements


          125 25    200 70
           0   1    2    3
                        top
First, top is incremented…




                              18
Array Stack Push
             (cont.)
       elements


          125 25    200 63
           0   1    2    3
                        top
Then, 63 is pushed into that position…




                                         19
Is the Array Stack Full/Empty?
• An array stack is full when
  – top == size - 1
• An array stack is empty when
  – top == -1




                                    20
Linked-List Stack
• Stacks can also be implemented with a
  linked list.
• The front node is the top of the stack.




                                            21
Linked-List Stack
                 (cont.)

          Bob


                   Ali
top


  To pop, we remove the node at the front of the
  linked list, and return the element to the client…
                   Ali



top



                                                       22
Linked-List Stack
                 (cont.)



                  Ali
top


  To push Cat, we place the new element in a node
  and insert it at the front of the linked list…
          Cat


                  Ali



top



                                                    23
Linked-List Stack Class Template
13   template <typename T>
14   class Stack {
15   public:
16      Stack( ) { … }
17      ~Stack( ) { … }
18      void push( const T & element ) { … }
19      bool pop( T & element ) { … }
20      bool peek( T & element ) { … }
21      bool isEmpty( ) const { … }
22      void makeEmpty( ) { … }
23   private:
24      Node<T> *top;
25   };
                                               24
Linked-List Stack
     Constructor and Destructor

8    Stack( ) : top(NULL) { }
7
8    ~Stack( )
9    {
10     makeEmpty( );
11   }




                                  25
Linked-List Push
32 void push( const T& element ) {
33     Node<T> *newNode = new Node<T>;
34     newNode->info = element;
35     if (top == NULL) {
36         newNode->next = NULL;
37         top = newNode;
38     }
39     else {
40         newNode->next = top;
41         top = newNode;
42     }
                           newNode
43 }
                                         26
Linked-List Push into Empty Stack
32 void push( const T& element ) {
33   Node<T> *newNode = new Node<T>;
34   newNode->info = element;
35   if (top == NULL) { // if stack is empty.
36      newNode->next = NULL;
37      top = newNode;
38   }                                 top      NULL
39   else {
40      newNode->next = top;
41      top = newNode;
42   }
43 }                    newNode
                                                   27
Linked-List Push into Empty Stack
              (cont.)
32 void push( const T& element ) {
33   Node<T> *newNode = new Node<T>;
34   newNode->info = element;
35   if (top == NULL) { // if stack is empty.
36       newNode->next = NULL;
37       top = newNode;
38   }                                     top
39   else {
40      newNode->next = top;
41      top = newNode;
42   }
                        newNode
43 }
                                                 28
Linked-List Push into Non-Empty
              Stack
32 void push( const T& element ) {
33   Node<T> *newNode = new Node<T>;
34   newNode->info = element;
35   if (top == NULL) { // if stack is empty.
36       newNode->next = NULL;
37     top = newNode;
38   }                                        top
39     else { // stack is not empty.
40     newNode->next = top;
41     top = newNode;
42   }
43 }                   newNode
                                                    29
Linked-List Push into Non-Empty
           Stack (cont.)
32 void push( const T& element ) {
33   Node<T> *newNode = new Node<T>;
34   newNode->info = element;
35   if (top == NULL) { // if stack is empty.
36       newNode->next = NULL;
37     top = newNode;
38   }                                        top
39   else { // stack is not empty.
40        newNode->next = top;
41        top = newNode;
42   }
                            newNode
43 }
                                                    30
Linked-List Peek
56 bool peek( T& element )
57 {
58   if ( top == NULL )
59       return false;
60   element = top->info;
61   return true;
62 }




                                31
Linked-List Pop
56 bool pop( T& element )
57 {
58   if ( top == NULL )
59       return false;
60   element = top->info;
61   Node<T> *ptr = top;
62   top = top->next;
63   delete ptr;
64   return true;
65 }


                               32
Linked-List Stack
         isEmpty and makeEmpty
65   bool isEmpty( ) const
66   {
67     return top == NULL;
68   }
69
70   void makeEmpty( )
71   {
72     T temp;
73     while ( pop( temp ) );
74   }



                                 33
The Queue ADT
• The queue is a data structure that is like a
  line of people
  – When people join the line, they go at the end
  – When people are served, they come off the
    front of the line
• Queue is known as a FIFO (last-in, last-
  out) data structure because the last data
  to enter the queue is the last to exit from
  the queue.
                                                    34
Queue ADT Operations
• enqueue: add an element to the end of
  the line
• dequeue: take an element from the front
  of the line
• peek: retrieve (copy) the element at the
  front of the line without removing it
• an operation to determine whether or not
  the queue is empty
• an operation that will empty out the queue
                                           35
Queue (cont.)
• In addition to a pointer at the beginning of
  the linked list (called front), a pointer to
  the end of the linked list (called back) is
  also maintained in the private section
• The back pointer makes it fast to add new
  elements to the end of the queue – you
  don’t have to use a loop to go all the way
  through the queue to find the last node

                                             36
Linked-List Dequeue

front            back
 Bob


          Ali




                             37
Linked-List Dequeue
       (cont.)

 front    back
   Ali




                      38
Linked-List Enqueue

front         back




                          39
Linked-List Enqueue
               (cont.)

front                  back




                              40
Linked-List Queue Class Template
13   template <typename T>
14   class Queue {
15   public:
16      Queue( );
17      ~Queue( );
18      void enqueue( const T & element );
19      bool dequeue( T & deqElement );
20      bool peek( T & frontElement );
21      bool isEmpty( ) const;
22      void makeEmpty( );
23   private:
24      Node<T> *front;
25      Node<T> *back;
26   };
                                             41
Linked-List Queue
     Constructor and Destructor

4    Queue( )
5    {
6      front = back = NULL;
7    }
8
9    ~Queue( )
10   {
11     makeEmpty( );
12   }

                                  42
Linked-List Queue Enqueue
21 void enqueue( const T & element )
22 {
23     Node<T> *newNode = new Node<T>;
24     newNode->info = element;
25     newNode->next = NULL;
28     if (front == NULL) { // list is empty.
29         front = back = newNode;
30     }
31     else { // list is not empty.
32         back->next = newNode;
33         back = newNode;
                                              newNode
34     }
35 }



                                                        43
Linked-List Queue Enqueue (cont.)
22 void enqueue( const T & element )
23 {
24     Node<T> *newNode = new Node<T>;
25     newNode->info = element;
26     newNode->next = NULL;
                                                  back
27     if (front == NULL) { // list is empty.
28         front = newNode;
29         back = front;
                                          front
30     }
31     else { // list is not empty.     newNode
32         back->next = newNode;
33         back = newNode;
34     }
35 }               Case 1: The queue
                   is initially empty.
                                                     44
Linked-List Queue Enqueue (cont.)
22 void enqueue( const T & element )
23 {
24     Node<T> *newNode = new Node<T>;          front
25     newNode->info = element;
26     newNode->next = NULL;
27     if (front == NULL) { // list is empty.
28         front = newNode;
29         back = front;
30     }
                                               back
31     else { // list is not empty.
32        back->next = newNode;          newNode
33         back = newNode;
34     }
35 }
                   Case 2: The queue
                   has nodes.
                                                        45
Linked-List Queue Enqueue (cont.)
22 void enqueue( const T & element )
23 {
24     Node<T> *newNode = new Node<T>;             front
25     newNode->info = element;
26     newNode->next = NULL;
27     if (front == NULL) { // list is empty.
28         front = newNode;
29         back = front;
30     }
31     else { // list is not empty.                  back
32         back->next = newNode;              newNode
33         back = newNode;
34     }
35 }
                   Case 2: The queue
                   has nodes.
                                                            46
Linked-List Queue Dequeue

41 bool dequeue( T & deqElement )
42 {
43   if ( front == NULL)    Returns false if client tries
44            return false; to dequeue an empty
                             queue.




                Dequeue continued…


                                                       47
Linked-List Queue
             Dequeue (cont.)
45    deqElement = front->info;
46    Node<T> *ptr = front;
47   front = front->next;     deqElement:
48   delete ptr;                  passed in by
49   return true;                 reference
50 }
                    front   ptr                  back




                                                    48
Linked-List Queue
            Dequeue (cont.)
45    deqElement = front->info;
46    Node<T> *ptr = front;
47    front = front->next;          deqElement:
48   delete ptr;
49   return true;
50 }
                                  ptr     front   back




                                                     49
Linked-List Queue
            Dequeue (cont.)
45    deqElement = front->info;
46    Node<T> *ptr = front;
47    front = front->next;          deqElement:
48    delete ptr;
49   return true;
50 }
                                  ptr     front   back




                                                     50
Linked-List Queue Peek
56 bool peek( T & frontElement )
57 {
58   if ( front == NULL)
59            return false;
60   frontElement = front->info;
61   return true;
62 }




                                   51
Linked-List Queue
         isEmpty and makeEmpty
65   bool isEmpty( ) const
66   {
67     return front == NULL;
68   }
69
70   void makeEmpty( )
71   {
72     T temp;
73     while ( dequeue( temp ) );
74   }



                                    52
Array Queue
• Similar to the linked-list queue, there are 2
  attributes called front and back, but they
  are indexes into an array instead of
  pointers.
• When enqueuing, the back index is
  incremented, and when dequeuing, the
  front index is incremented.


                                              53
Array Queue Class Template
3    template <typename T>
4    class Queue {
5    public:
6           Queue( ) { … }
7           ~Queue( ) { … }
8           void enqueue( T element ) { …
9           bool dequeue( T & deqElement ) { … }
10          bool peek( T & frontElement ) { … }
11          bool isEmpty( ) const { … }
12          void makeEmpty( ) { … }
13     private:
14          T *elements;
15          int size
16          int front;
17          int back;
                                                   54
18    };
Array Queue
            Enqueue / Dequeue

                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
  0     1    2   3    4     5   6   7
                                        ENQUEUE
front                back               DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                              55
Array Queue
    Enqueue / Dequeue (cont.)

                                       DEQUEUE
                                       DEQUEUE
0    1      2   3    4     5   6   7   ENQUEUE
                                       ENQUEUE
    front           back               DEQUEUE
                                       DEQUEUE
                                       ENQUEUE
                                       DEQUEUE
                                       DEQUEUE
                                       ENQUEUE
                                             56
Array Queue
    Enqueue / Dequeue (cont.)

                                        DEQUEUE
                                        DEQUEUE
0    1     2     3    4     5   6   7   ENQUEUE
                                        ENQUEUE
         front       back               DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                              57
Array Queue
    Enqueue / Dequeue (cont.)

                                        DEQUEUE
                                        DEQUEUE
0    1     2     3   4    5     6   7   ENQUEUE
                                        ENQUEUE
         front           back           DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                              58
Array Queue
    Enqueue / Dequeue (cont.)

                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
0    1     2     3   4   5    6     7
                                        ENQUEUE
         front               back       DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                              59
Array Queue
    Enqueue / Dequeue (cont.)

                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
0    1   2    3      4   5    6     7
                                        ENQUEUE
             front           back       DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                              60
Array Queue
    Enqueue / Dequeue (cont.)

                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
0    1   2   3    4      5    6     7
                                        ENQUEUE
                 front       back       DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                              61
Array Queue
    Enqueue / Dequeue (cont.)

                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
0    1   2   3    4      5   6    7
                                        ENQUEUE
                 front           back   DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                              62
Array Queue
    Enqueue / Dequeue (cont.)

                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
0    1   2   3   4     5     6    7
                                        ENQUEUE
                     front       back   DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
                                              63
Array Queue
    Enqueue / Dequeue (cont.)

                                      DEQUEUE
                                      DEQUEUE
                                      ENQUEUE
0    1   2   3   4   5    6     7
                                      ENQUEUE
                         front back   DEQUEUE
                                      DEQUEUE
                                      ENQUEUE
                                      DEQUEUE
                                      DEQUEUE
                                      ENQUEUE
                                            64
Array Queue
    Enqueue / Dequeue (cont.)

                                            DEQUEUE
                                            DEQUEUE
                                            ENQUEUE
0    1   2    3    4       5    6     7
                                            ENQUEUE
                               front back   DEQUEUE
                                            DEQUEUE
                                            ENQUEUE
    We have reached the                     DEQUEUE
    end of array. How to
    enqueue?                    ?           DEQUEUE
                                            ENQUEUE
                                                  65
Array Queue
     Enqueue / Dequeue (cont.)

                                         DEQUEUE
                                         DEQUEUE
                                         ENQUEUE
 0    1   2    3   4    5    6     7
                                         ENQUEUE
                            front back   DEQUEUE
                                         DEQUEUE
We could double the size of the array    ENQUEUE
here.                                    DEQUEUE
But if we keep doing this, we may        DEQUEUE
have a million elements in the           ENQUEUE
array, but only a few at the end are
                                               66
used!
Array Queue
     Enqueue / Dequeue (cont.)

                                        DEQUEUE
                                        DEQUEUE
                                        ENQUEUE
 0     1   2   3   4    5     6     7
                                        ENQUEUE
back                        front       DEQUEUE
                                        DEQUEUE
We handle this problem by having the    ENQUEUE
back wrap around to the beginning of    DEQUEUE
the array.                              DEQUEUE
The front also wraps to the beginning   ENQUEUE
when it reaches the end of the array          67
Is Array Queue Full/Empty?
• An array queue is empty when
  – front = -1
• An array queue has one element when
  – front = back
• An array queue is full when
  – back + 1 = front



                                        68
A Full Array Queue



0   1    2   3   4     5    6     7

                     back front

    If the next operation is ENQUEUE, the array
    capacity will need to be doubled



                                                  69
Reference
• Childs, J. S. (2008). Stack and Queue.
  C++ Classes and Data Structures.
  Prentice Hall.




                                           70

Más contenido relacionado

La actualidad más candente

Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
Sandesh Sharma
 

La actualidad más candente (20)

Java Collections API
Java Collections APIJava Collections API
Java Collections API
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on Steroids
 
Generics in .NET, C++ and Java
Generics in .NET, C++ and JavaGenerics in .NET, C++ and Java
Generics in .NET, C++ and Java
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
 
Clojure: The Art of Abstraction
Clojure: The Art of AbstractionClojure: The Art of Abstraction
Clojure: The Art of Abstraction
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Python Programming: Data Structure
Python Programming: Data StructurePython Programming: Data Structure
Python Programming: Data Structure
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Java generics
Java genericsJava generics
Java generics
 
Java generics final
Java generics finalJava generics final
Java generics final
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Introduction to julia
Introduction to juliaIntroduction to julia
Introduction to julia
 
Lec2
Lec2Lec2
Lec2
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Core java by a introduction sandesh sharma
Core java by a introduction sandesh sharmaCore java by a introduction sandesh sharma
Core java by a introduction sandesh sharma
 
Arrays
ArraysArrays
Arrays
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 

Destacado

Ch08 evaluating arguments
Ch08 evaluating argumentsCh08 evaluating arguments
Ch08 evaluating arguments
Hariz Mustafa
 
Cognition and problem_solving
Cognition and problem_solvingCognition and problem_solving
Cognition and problem_solving
Hariz Mustafa
 
Bassham3 powerpoint lecturenotes_ch05
Bassham3 powerpoint lecturenotes_ch05Bassham3 powerpoint lecturenotes_ch05
Bassham3 powerpoint lecturenotes_ch05
Hariz Mustafa
 
Lecture03 inheritance
Lecture03 inheritanceLecture03 inheritance
Lecture03 inheritance
Hariz Mustafa
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
Hariz Mustafa
 
Chapter 6 logical_fallacies_ii
Chapter 6 logical_fallacies_iiChapter 6 logical_fallacies_ii
Chapter 6 logical_fallacies_ii
Hariz Mustafa
 
Lecture04 polymorphism
Lecture04 polymorphismLecture04 polymorphism
Lecture04 polymorphism
Hariz Mustafa
 
Chapter 5 fallacies
Chapter 5 fallaciesChapter 5 fallacies
Chapter 5 fallacies
scrasnow
 
Bassham3 powerpoint lecturenotes_ch06
Bassham3 powerpoint lecturenotes_ch06Bassham3 powerpoint lecturenotes_ch06
Bassham3 powerpoint lecturenotes_ch06
Hariz Mustafa
 
Critical thinking fall 2014 2015 (chapters 6,7,8,11 and 12 analyzing and eval...
Critical thinking fall 2014 2015 (chapters 6,7,8,11 and 12 analyzing and eval...Critical thinking fall 2014 2015 (chapters 6,7,8,11 and 12 analyzing and eval...
Critical thinking fall 2014 2015 (chapters 6,7,8,11 and 12 analyzing and eval...
XixiViolet
 
Ch03 basic logical_concepts
Ch03 basic logical_conceptsCh03 basic logical_concepts
Ch03 basic logical_concepts
Hariz Mustafa
 
Exercise answers chapter 1, 2 & 3
Exercise answers chapter 1, 2 & 3Exercise answers chapter 1, 2 & 3
Exercise answers chapter 1, 2 & 3
Hariz Mustafa
 

Destacado (15)

Lecture09 recursion
Lecture09 recursionLecture09 recursion
Lecture09 recursion
 
Lecture10 trees v3
Lecture10 trees v3Lecture10 trees v3
Lecture10 trees v3
 
Decision making
Decision makingDecision making
Decision making
 
Ch08 evaluating arguments
Ch08 evaluating argumentsCh08 evaluating arguments
Ch08 evaluating arguments
 
Cognition and problem_solving
Cognition and problem_solvingCognition and problem_solving
Cognition and problem_solving
 
Bassham3 powerpoint lecturenotes_ch05
Bassham3 powerpoint lecturenotes_ch05Bassham3 powerpoint lecturenotes_ch05
Bassham3 powerpoint lecturenotes_ch05
 
Lecture03 inheritance
Lecture03 inheritanceLecture03 inheritance
Lecture03 inheritance
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
 
Chapter 6 logical_fallacies_ii
Chapter 6 logical_fallacies_iiChapter 6 logical_fallacies_ii
Chapter 6 logical_fallacies_ii
 
Lecture04 polymorphism
Lecture04 polymorphismLecture04 polymorphism
Lecture04 polymorphism
 
Chapter 5 fallacies
Chapter 5 fallaciesChapter 5 fallacies
Chapter 5 fallacies
 
Bassham3 powerpoint lecturenotes_ch06
Bassham3 powerpoint lecturenotes_ch06Bassham3 powerpoint lecturenotes_ch06
Bassham3 powerpoint lecturenotes_ch06
 
Critical thinking fall 2014 2015 (chapters 6,7,8,11 and 12 analyzing and eval...
Critical thinking fall 2014 2015 (chapters 6,7,8,11 and 12 analyzing and eval...Critical thinking fall 2014 2015 (chapters 6,7,8,11 and 12 analyzing and eval...
Critical thinking fall 2014 2015 (chapters 6,7,8,11 and 12 analyzing and eval...
 
Ch03 basic logical_concepts
Ch03 basic logical_conceptsCh03 basic logical_concepts
Ch03 basic logical_concepts
 
Exercise answers chapter 1, 2 & 3
Exercise answers chapter 1, 2 & 3Exercise answers chapter 1, 2 & 3
Exercise answers chapter 1, 2 & 3
 

Similar a Lecture08 stacks and-queues_v3

23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
Rishabh Jindal
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
07 ds and algorithm session_10
07 ds and algorithm session_1007 ds and algorithm session_10
07 ds and algorithm session_10
Niit Care
 

Similar a Lecture08 stacks and-queues_v3 (20)

U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
STACKS AND QUEUES CONCEPTS
STACKS AND QUEUES CONCEPTSSTACKS AND QUEUES CONCEPTS
STACKS AND QUEUES CONCEPTS
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Stacks fundamentals
Stacks fundamentalsStacks fundamentals
Stacks fundamentals
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Data structures
Data structuresData structures
Data structures
 
Stack
StackStack
Stack
 
07 ds and algorithm session_10
07 ds and algorithm session_1007 ds and algorithm session_10
07 ds and algorithm session_10
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
04 stacks
04 stacks04 stacks
04 stacks
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 

Más de Hariz Mustafa

Más de Hariz Mustafa (12)

Topic6decisionmaking
Topic6decisionmakingTopic6decisionmaking
Topic6decisionmaking
 
Topic5cognition and problem_solving
Topic5cognition and problem_solvingTopic5cognition and problem_solving
Topic5cognition and problem_solving
 
Topic2 argument
Topic2 argumentTopic2 argument
Topic2 argument
 
Topic2
Topic2Topic2
Topic2
 
Topic 1
Topic 1Topic 1
Topic 1
 
Problem solving activities
Problem solving activitiesProblem solving activities
Problem solving activities
 
Decision making scenarios
Decision making scenariosDecision making scenarios
Decision making scenarios
 
Chapter 4 language
Chapter 4 languageChapter 4 language
Chapter 4 language
 
Chapter 5 logical_fallacies_i
Chapter 5 logical_fallacies_iChapter 5 logical_fallacies_i
Chapter 5 logical_fallacies_i
 
Bassham3 powerpoint lecturenotes_ch04
Bassham3 powerpoint lecturenotes_ch04Bassham3 powerpoint lecturenotes_ch04
Bassham3 powerpoint lecturenotes_ch04
 
6a
6a6a
6a
 
3a
3a3a
3a
 

Lecture08 stacks and-queues_v3

  • 2. Stack ADT • Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. • In a stack, the set of data is the stack of elements. • Stack is known as a LIFO (last-in-first-out) data structure because the last data to enter the stack is the first to exit the stack. 2
  • 3. Stack ADT Operations • push: places an element onto the top of a stack. • pop: removes an element from the top of the stack. • peek: which retrieves (copies) a value from the top of the stack without removing it. • an operation to determine whether or not the stack is empty. • an operation to empty out a stack. 3
  • 4. Push • Push means place a new data element at the top of the stack 11 5 3 17 stack 4
  • 5. Push (cont.) • Push means place a new data element at the top of the stack 11 3 5 17 stack 5
  • 6. Push (cont.) • Push means place a new data element at the top of the stack 3 11 5 17 stack 6
  • 7. Pop • Pop means take a data element off the top of the stack 3 11 5 17 stack 7
  • 8. Pop (cont.) • Pop means take a data element off the top of the stack 3 11 5 17 stack 8
  • 9. Pop (cont.) • Pop means take a data element off the top of the stack 11 5 3 17 stack 9
  • 10. Peek • Peek means retrieve the top of the stack without removing it 3 11 5 3 17 stack 10
  • 11. Array Stack Class Template 3 template <typename T> 4 class Stack { 5 public: 6 Stack() { … } 7 ~Stack( ) { … } 8 void push( T& elementToPush ) { … } 9 bool pop( T& poppedElement ) { … } 10 bool peek( T& topElement ) { … } 11 bool isEmpty( ) const { … } 12 void makeEmpty( ) { … } 13 private: 14 T* elements; // dynamic array used as an 15 int size; index to the top 16 int top; of the stack 17 }; 11
  • 12. Array Stack Constructor and Destructor 3 Stack () : top(-1), size(0) 4 { 5 elements = NULL; 6 } 7 8 ~Stack( ) 9 { 10 makeEmpty( ); 11 } 12
  • 13. Array Stack isEmpty and makeEmpty 65 bool isEmpty( ) const 66 { 67 return top == -1; 68 } 69 70 void makeEmpty( ) 71 { 72 size = 0; 73 top = -1; 74 delete [] elements; 75 } 13
  • 14. Array Stack Pop elements 125 25 200 70 0 1 2 3 top An element can’t really be removed from an array, as one would think pop would achieve. 14
  • 15. Array Stack Pop (cont.) elements client 125 25 200 70 0 1 2 3 top The element 70 is at the top of the stack, and what really happens during a pop, is that 70 is returned to the client… 15
  • 16. Array Stack Pop (cont.) elements client 125 25 200 70 0 1 2 3 top and top is decremented… 16
  • 17. Array Stack Pop (cont.) elements client 125 25 200 70 0 1 2 3 top The element 70 is still in the array, but it is no longer accessible. The next push will overwrite it. Say, we would like to push 63… 17
  • 18. Array Stack Push elements 125 25 200 70 0 1 2 3 top First, top is incremented… 18
  • 19. Array Stack Push (cont.) elements 125 25 200 63 0 1 2 3 top Then, 63 is pushed into that position… 19
  • 20. Is the Array Stack Full/Empty? • An array stack is full when – top == size - 1 • An array stack is empty when – top == -1 20
  • 21. Linked-List Stack • Stacks can also be implemented with a linked list. • The front node is the top of the stack. 21
  • 22. Linked-List Stack (cont.) Bob Ali top To pop, we remove the node at the front of the linked list, and return the element to the client… Ali top 22
  • 23. Linked-List Stack (cont.) Ali top To push Cat, we place the new element in a node and insert it at the front of the linked list… Cat Ali top 23
  • 24. Linked-List Stack Class Template 13 template <typename T> 14 class Stack { 15 public: 16 Stack( ) { … } 17 ~Stack( ) { … } 18 void push( const T & element ) { … } 19 bool pop( T & element ) { … } 20 bool peek( T & element ) { … } 21 bool isEmpty( ) const { … } 22 void makeEmpty( ) { … } 23 private: 24 Node<T> *top; 25 }; 24
  • 25. Linked-List Stack Constructor and Destructor 8 Stack( ) : top(NULL) { } 7 8 ~Stack( ) 9 { 10 makeEmpty( ); 11 } 25
  • 26. Linked-List Push 32 void push( const T& element ) { 33 Node<T> *newNode = new Node<T>; 34 newNode->info = element; 35 if (top == NULL) { 36 newNode->next = NULL; 37 top = newNode; 38 } 39 else { 40 newNode->next = top; 41 top = newNode; 42 } newNode 43 } 26
  • 27. Linked-List Push into Empty Stack 32 void push( const T& element ) { 33 Node<T> *newNode = new Node<T>; 34 newNode->info = element; 35 if (top == NULL) { // if stack is empty. 36 newNode->next = NULL; 37 top = newNode; 38 } top NULL 39 else { 40 newNode->next = top; 41 top = newNode; 42 } 43 } newNode 27
  • 28. Linked-List Push into Empty Stack (cont.) 32 void push( const T& element ) { 33 Node<T> *newNode = new Node<T>; 34 newNode->info = element; 35 if (top == NULL) { // if stack is empty. 36 newNode->next = NULL; 37 top = newNode; 38 } top 39 else { 40 newNode->next = top; 41 top = newNode; 42 } newNode 43 } 28
  • 29. Linked-List Push into Non-Empty Stack 32 void push( const T& element ) { 33 Node<T> *newNode = new Node<T>; 34 newNode->info = element; 35 if (top == NULL) { // if stack is empty. 36 newNode->next = NULL; 37 top = newNode; 38 } top 39 else { // stack is not empty. 40 newNode->next = top; 41 top = newNode; 42 } 43 } newNode 29
  • 30. Linked-List Push into Non-Empty Stack (cont.) 32 void push( const T& element ) { 33 Node<T> *newNode = new Node<T>; 34 newNode->info = element; 35 if (top == NULL) { // if stack is empty. 36 newNode->next = NULL; 37 top = newNode; 38 } top 39 else { // stack is not empty. 40 newNode->next = top; 41 top = newNode; 42 } newNode 43 } 30
  • 31. Linked-List Peek 56 bool peek( T& element ) 57 { 58 if ( top == NULL ) 59 return false; 60 element = top->info; 61 return true; 62 } 31
  • 32. Linked-List Pop 56 bool pop( T& element ) 57 { 58 if ( top == NULL ) 59 return false; 60 element = top->info; 61 Node<T> *ptr = top; 62 top = top->next; 63 delete ptr; 64 return true; 65 } 32
  • 33. Linked-List Stack isEmpty and makeEmpty 65 bool isEmpty( ) const 66 { 67 return top == NULL; 68 } 69 70 void makeEmpty( ) 71 { 72 T temp; 73 while ( pop( temp ) ); 74 } 33
  • 34. The Queue ADT • The queue is a data structure that is like a line of people – When people join the line, they go at the end – When people are served, they come off the front of the line • Queue is known as a FIFO (last-in, last- out) data structure because the last data to enter the queue is the last to exit from the queue. 34
  • 35. Queue ADT Operations • enqueue: add an element to the end of the line • dequeue: take an element from the front of the line • peek: retrieve (copy) the element at the front of the line without removing it • an operation to determine whether or not the queue is empty • an operation that will empty out the queue 35
  • 36. Queue (cont.) • In addition to a pointer at the beginning of the linked list (called front), a pointer to the end of the linked list (called back) is also maintained in the private section • The back pointer makes it fast to add new elements to the end of the queue – you don’t have to use a loop to go all the way through the queue to find the last node 36
  • 37. Linked-List Dequeue front back Bob Ali 37
  • 38. Linked-List Dequeue (cont.) front back Ali 38
  • 40. Linked-List Enqueue (cont.) front back 40
  • 41. Linked-List Queue Class Template 13 template <typename T> 14 class Queue { 15 public: 16 Queue( ); 17 ~Queue( ); 18 void enqueue( const T & element ); 19 bool dequeue( T & deqElement ); 20 bool peek( T & frontElement ); 21 bool isEmpty( ) const; 22 void makeEmpty( ); 23 private: 24 Node<T> *front; 25 Node<T> *back; 26 }; 41
  • 42. Linked-List Queue Constructor and Destructor 4 Queue( ) 5 { 6 front = back = NULL; 7 } 8 9 ~Queue( ) 10 { 11 makeEmpty( ); 12 } 42
  • 43. Linked-List Queue Enqueue 21 void enqueue( const T & element ) 22 { 23 Node<T> *newNode = new Node<T>; 24 newNode->info = element; 25 newNode->next = NULL; 28 if (front == NULL) { // list is empty. 29 front = back = newNode; 30 } 31 else { // list is not empty. 32 back->next = newNode; 33 back = newNode; newNode 34 } 35 } 43
  • 44. Linked-List Queue Enqueue (cont.) 22 void enqueue( const T & element ) 23 { 24 Node<T> *newNode = new Node<T>; 25 newNode->info = element; 26 newNode->next = NULL; back 27 if (front == NULL) { // list is empty. 28 front = newNode; 29 back = front; front 30 } 31 else { // list is not empty. newNode 32 back->next = newNode; 33 back = newNode; 34 } 35 } Case 1: The queue is initially empty. 44
  • 45. Linked-List Queue Enqueue (cont.) 22 void enqueue( const T & element ) 23 { 24 Node<T> *newNode = new Node<T>; front 25 newNode->info = element; 26 newNode->next = NULL; 27 if (front == NULL) { // list is empty. 28 front = newNode; 29 back = front; 30 } back 31 else { // list is not empty. 32 back->next = newNode; newNode 33 back = newNode; 34 } 35 } Case 2: The queue has nodes. 45
  • 46. Linked-List Queue Enqueue (cont.) 22 void enqueue( const T & element ) 23 { 24 Node<T> *newNode = new Node<T>; front 25 newNode->info = element; 26 newNode->next = NULL; 27 if (front == NULL) { // list is empty. 28 front = newNode; 29 back = front; 30 } 31 else { // list is not empty. back 32 back->next = newNode; newNode 33 back = newNode; 34 } 35 } Case 2: The queue has nodes. 46
  • 47. Linked-List Queue Dequeue 41 bool dequeue( T & deqElement ) 42 { 43 if ( front == NULL) Returns false if client tries 44 return false; to dequeue an empty queue. Dequeue continued… 47
  • 48. Linked-List Queue Dequeue (cont.) 45 deqElement = front->info; 46 Node<T> *ptr = front; 47 front = front->next; deqElement: 48 delete ptr; passed in by 49 return true; reference 50 } front ptr back 48
  • 49. Linked-List Queue Dequeue (cont.) 45 deqElement = front->info; 46 Node<T> *ptr = front; 47 front = front->next; deqElement: 48 delete ptr; 49 return true; 50 } ptr front back 49
  • 50. Linked-List Queue Dequeue (cont.) 45 deqElement = front->info; 46 Node<T> *ptr = front; 47 front = front->next; deqElement: 48 delete ptr; 49 return true; 50 } ptr front back 50
  • 51. Linked-List Queue Peek 56 bool peek( T & frontElement ) 57 { 58 if ( front == NULL) 59 return false; 60 frontElement = front->info; 61 return true; 62 } 51
  • 52. Linked-List Queue isEmpty and makeEmpty 65 bool isEmpty( ) const 66 { 67 return front == NULL; 68 } 69 70 void makeEmpty( ) 71 { 72 T temp; 73 while ( dequeue( temp ) ); 74 } 52
  • 53. Array Queue • Similar to the linked-list queue, there are 2 attributes called front and back, but they are indexes into an array instead of pointers. • When enqueuing, the back index is incremented, and when dequeuing, the front index is incremented. 53
  • 54. Array Queue Class Template 3 template <typename T> 4 class Queue { 5 public: 6 Queue( ) { … } 7 ~Queue( ) { … } 8 void enqueue( T element ) { … 9 bool dequeue( T & deqElement ) { … } 10 bool peek( T & frontElement ) { … } 11 bool isEmpty( ) const { … } 12 void makeEmpty( ) { … } 13 private: 14 T *elements; 15 int size 16 int front; 17 int back; 54 18 };
  • 55. Array Queue Enqueue / Dequeue DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 55
  • 56. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE 0 1 2 3 4 5 6 7 ENQUEUE ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 56
  • 57. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE 0 1 2 3 4 5 6 7 ENQUEUE ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 57
  • 58. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE 0 1 2 3 4 5 6 7 ENQUEUE ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 58
  • 59. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 59
  • 60. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 60
  • 61. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 61
  • 62. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 62
  • 63. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 63
  • 64. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE DEQUEUE DEQUEUE ENQUEUE 64
  • 65. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE front back DEQUEUE DEQUEUE ENQUEUE We have reached the DEQUEUE end of array. How to enqueue? ? DEQUEUE ENQUEUE 65
  • 66. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE front back DEQUEUE DEQUEUE We could double the size of the array ENQUEUE here. DEQUEUE But if we keep doing this, we may DEQUEUE have a million elements in the ENQUEUE array, but only a few at the end are 66 used!
  • 67. Array Queue Enqueue / Dequeue (cont.) DEQUEUE DEQUEUE ENQUEUE 0 1 2 3 4 5 6 7 ENQUEUE back front DEQUEUE DEQUEUE We handle this problem by having the ENQUEUE back wrap around to the beginning of DEQUEUE the array. DEQUEUE The front also wraps to the beginning ENQUEUE when it reaches the end of the array 67
  • 68. Is Array Queue Full/Empty? • An array queue is empty when – front = -1 • An array queue has one element when – front = back • An array queue is full when – back + 1 = front 68
  • 69. A Full Array Queue 0 1 2 3 4 5 6 7 back front If the next operation is ENQUEUE, the array capacity will need to be doubled 69
  • 70. Reference • Childs, J. S. (2008). Stack and Queue. C++ Classes and Data Structures. Prentice Hall. 70