1 Chapter 7 Stacks and Queues. 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,

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
Jerry Lebowitz.  Stacks  Queues 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition.
CS Data Structures II Review COSC 2006 April 14, 2017
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Queues.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
Stacks, Queues, and Deques
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 7 Stacks II CS Data Structures I COSC 2006
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
C++ Classes and Data Structures Jeffrey S. Childs
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
CC 215 Data Structures Queue ADT
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
C++ Plus Data Structures
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
ADT list.
Stacks and Queues 1.
Chapter 5 Stack (part 1).
Stacks, Queues, and Deques
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

1 Chapter 7 Stacks and Queues

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.

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.

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

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

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

7 Pop Pop means take a data element off the top of the stack stack

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

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

10 Peek Peek means retrieve the top of the stack without removing it stack

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

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

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

14 Array Stack Pop elements top An element can’t really be removed from an array, as one would think pop would achieve.

15 Array Stack Pop (cont.) elements 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… client

16 Array Stack Pop (cont.) elements top and top is decremented… client

17 Array Stack Pop (cont.) elements 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… client

18 Array Stack Push elements top First, top is incremented…

19 Array Stack Push (cont.) elements top Then, 63 is pushed into that position…

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

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

Bob Ali 22 Linked-List Stack (cont.) top To pop, we remove the node at the front of the linked list, and return the element to the client… Ali top

23 Linked-List Stack (cont.) top To push Cat, we place the new element in a node and insert it at the front of the linked list… Cat top Ali

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

25 8 Stack( ) : top(NULL) { } 7 8 ~Stack( ) 9 { 10 makeEmpty( ); 11 } Linked-List Stack Constructor and Destructor

26 Linked-List Push 32 void push( T& element ) { 33Node *newNode = new Node ; 34newNode->info = element; 35 newNode->next = top; 36 top = newNode; 37 } newNode

27 Linked-List Push into Empty Stack 32 void push( T& element ) { 33Node *newNode = new Node ; 34newNode->info = element; 35 newNode->next = top; 36 top = newNode; 37 } top newNode NULL

28 Linked-List Push into Empty Stack (cont.) 32 void push( T& element ) { 33Node *newNode = new Node ; 34newNode->info = element; 35 newNode->next = top; 36 top = newNode; 37 } top newNode

29 Linked-List Push into Non-Empty Stack 32 void push( T& element ) { 33Node *newNode = new Node ; 34newNode->info = element; 35 newNode->next = top; 36 top = newNode; 37 } newNode top

30 Linked-List Push into Non-Empty Stack (cont.) 32 void push( T& element ) { 33Node *newNode = new Node ; 34newNode->info = element; 35 newNode->next = top; 36 top = newNode; 37 } newNode top

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

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

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

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 (first-in, first- out) data structure because the last data to enter the queue is the last to exit from the queue.

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

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

37 Linked-List Dequeue front back Bob Ali

38 Linked-List Dequeue (cont.) front back Ali

39 Linked-List Enqueue front back

40 Linked-List Enqueue (cont.) front back

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

42 4 Queue( ) 5 { 6 front = back = NULL; 7 } 8 9 ~Queue( ) 10 { 11 makeEmpty( ); 12 } Linked-List Queue Constructor and Destructor

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

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

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

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

47 Linked-List Queue Dequeue 41 bool dequeue( T & deqElement ) 42 { 43if ( front == NULL) 44return false; Dequeue continued… Returns false if client tries to dequeue an empty queue.

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

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

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

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

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

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.

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

55 Array Queue Enqueue / Dequeue frontback DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback We have reached the end of array. How to enqueue? ? Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback Array Queue Enqueue / Dequeue (cont.) We could double the size of the array here. But if we keep doing this, we may have a million elements in the array, but only a few at the end are used! DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

frontback We handle this problem by having the back wrap around to the beginning of the array. The front also wraps to the beginning when it reaches the end of the array Array Queue Enqueue / Dequeue (cont.) DEQUEUE ENQUEUE DEQUEUE ENQUEUE DEQUEUE ENQUEUE

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

69 A Full Array Queue frontback If the next operation is ENQUEUE, the array capacity will need to be doubled

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