CMSC 341 Lecture 5 Stacks, Queues

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
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
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
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.
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.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
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.”
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
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.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Lecture No.09 Data Structures Dr. Sohail Aslam
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Program based on queue & their operations for an application
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Queues.
(not in any book that we are using)
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Memory Organization Process 1 (browser) Code Process 3 (word)
Lesson Objectives Aims
Stacks and Queues CSE 373 Data Structures.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks and Queues 1.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Queues Jyh-Shing Roger Jang (張智星)
More Data Structures (Part 1)
Stacks CS-240 Dick Steflik.
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Data Structures & Programming
Presentation transcript:

CMSC 341 Lecture 5 Stacks, Queues Prof. Neary

Last Class Review Linked Lists Using Linked Lists vs Arrays Nodes “Supporting Actors” (member variables) Overview Creation Traversal Deletion

Topics for Today Stacks Queues Types of Stacks Examples Types of Queues

Implementation Methods Today we are going to talk about the ways that we can implement stacks and queues 3 Ways to Create a Stack or Queue Create a static stack or queue using an array Create a dynamic stack or queue using a linked list Create a stack or queue using the STL

Stacks

Introduction to Stacks A stack is a data structure that stores and retrieves items in a last-in-first-out (LIFO) manner.

Applications of Stacks Program execution Functions & variables in process memory Postfix mathematical expressions Browsing history Depth First Search PEZ Candy

The Stack ADT push(e) pop() top() size() isEmpty() Insert element e at the top of the stack pop() Remove top element of the stack top() Return reference to the top element of the stack size() Return the number of elements in the stack isEmpty() Return true if the stack contains no elements

Implementations of Stacks Static Stacks Fixed size Can be implemented with an array Dynamic Stacks Grow in size as needed Can be implemented with a linked list Using STL (dynamic)

The Push Operation Suppose we have an empty integer stack that is capable of holding a maximum of three values. With that stack we execute the following push operations. push(5); push(10); push(15);

The state of the stack after each of the push operations:

The Pop Operation Now, suppose we execute three consecutive pop operations on the same stack:

Other Stack Operations isFull(): A Boolean operation needed for static stacks. Returns true if the stack is full. Otherwise, returns false. isEmpty(): A Boolean operation needed for all stacks. Returns true if the stack is empty. Otherwise, returns false.

Static Stacks

Static Stacks A static stack is built on an array As we are using an array, we must specify the starting size of the stack The stack may become full if the array becomes full

Member Variables for Stacks Three major variables: Pointer Creates a pointer to stack size Tracks elements in stack top Tracks top element in stack

Member Functions for Stacks CONSTRUCTOR Creates a stack DESTRUCTOR Deletes a stack push() Pushes element to stack pop() Pops element from stack isEmpty() Is the stack empty? isFull() Is the stack full?

Dynamic Stacks

Dynamic Stacks A dynamic stack is built on a linked list instead of an array. A linked list-based stack offers two advantages over an array-based stack. No need to specify the starting size of the stack. A dynamic stack simply starts as an empty linked list, and then expands by one node each time a value is pushed. A dynamic stack will never be full, as long as the system has enough free memory.

Member Variables for Dynamic Stacks Parts: Linked list Linked list for stack (nodes) size Tracks elements in stack

Member Functions for Dynamic Stacks CONSTRUCTOR Creates a stack DESTRUCTOR Deletes a stack push() Pushes element to stack pop() Pops element from stack isEmpty() Is the stack empty? top() What is the top element? What happened to isFull()?

Common Problems with Stacks Stack underflow no elements in the stack, and you tried to pop Stack overflow maximum elements in stack, and tried to add another not an issue using STL or a dynamic implementation

Runtime Analysis How fast can we perform each operation for a stack? Each operation is O(1)!

Examples void push(int e){ ++top; stackArray[top] = e; } Constant time void push(int e){ ++top; stackArray[top] = e; } int pop(){ int item = stackArray[top]; --top; return item; } Constant time

Queues

Introduction to the Queue Like a stack, a queue is a data structure that holds a sequence of elements. A queue, however, provides access to its elements in first-in, first-out (FIFO) order. The elements in a queue are processed like customers standing in a line: the first customer to get in line is the first one to be served (and leave the line).

Example Applications of Queues Print jobs OS processes communication between processes/systems Breadth First Search

Implementations of Queues Just like stacks! Static Queues Fixed size Can be implemented with an array Dynamic Queues Grow in size as needed Can be implemented with a linked list Using STL (dynamic)

Queue ADT size() isEmpty() enqueue(e) dequeue() front() back() Number of elements in the queue isEmpty() Does the queue have elements in it? enqueue(e) Add element e to the end of the queue dequeue() Remove the first element from the queue front() What is at the front of the queue? back() What is at the back of the queue?

Queue Operations Think of queues as having a front and a rear. rear: position where elements are added front: position from which elements are removed

Queue Operations The two primary queue operations are enqueuing and dequeuing. To enqueue means to insert an element at the rear of a queue. To dequeue means to remove an element from the front of a queue.

Queue Operations Suppose we have an empty static integer queue that is capable of holding a maximum of three values. With that queue we execute the following enqueue operations.   Enqueue(3); Enqueue(6); Enqueue(9);

Queue Operations - Dequeue Now let's see how dequeue operations are performed. The figure on the right shows the state of the queue after each of three consecutive dequeue operations An important remark After each dequeue, remaining items shift toward the front of the queue. 3 removed 6 removed 9 removed

Efficiency Problem of Dequeue & Solution Shifting after each dequeue operation causes inefficiency. Solution Let front index move as elements are removed let rear index "wrap around" to the beginning of array, treating array as circular Similarly, the front index as well Yields more complex enqueue, dequeue code, but more efficient Let's see the trace of this method on the board for the enqueue and dequeue operations given on the right (queue size is 3) Enqueue(3); Enqueue(6); Enqueue(9); Dequeue(); Enqueue(12);

Implementation of a Static Queue The previous discussion was about static arrays Container is an array Class Implementation for a static integer queue Member functions enqueue() dequeue() isEmpty() isFull() clear()

Member Variables for Static Queues Five major variables: queueArray Creates a pointer to queue queueSize Tracks capacity of queue numItems Tracks elements in queue front rear The variables front and rear are used when our queue “rotates,” as discussed earlier

Member Functions for Queues CONSTRUCTOR Creates a queue DESTRUCTOR Deletes a queue enqueue() Adds element to queue dequeue() Removes element from queue isEmpty() Is the queue empty? isFull() Is the queue full? clear() Empties queue

Runtime Analysis Just like a stack, every operation is O(1) void enqueue(int element){ queueArr[rear] = element; rear = (rear + 1) mod N; n++; } int dequeue(){ int item = queueArr[front]; front = (front + 1) mod N; n--; return item; }

Dynamic Queue Easily implemented with linked list FRONT and REAR pointers, instead of indices Change pointers instead of modular arithmetic

Other types of Queues Priority Queue Double-ended Queue (Dequeue) Regular queue, but elements have an additional ‘priority’ High priority served before lower priority Implement with an array or linked list, usually a heap Double-ended Queue (Dequeue) Supports insertion and deletion at both ends Implement this with a doubly-linked list