! A data type for which: ! An ADT may be implemented using various. ! Examples:

Similar documents
Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

ADTs: Stacks and Queues

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

Chapter 18: Stacks And Queues

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 18: Stacks And Queues

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CS24 Week 4 Lecture 2

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

1 P age DS & OOPS / UNIT II

Lecture Data Structure Stack

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

UNIT-2 Stack & Queue


CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Introduction to the C programming language

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Introduction to the C programming language

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Ch. 17: Linked Lists. Introduction to Linked Lists

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

An Introduction to Queues With Examples in C++

Assertion. C++ Object Oriented Programming Pei-yih Ting NTOU CS

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

.:: UNIT 4 ::. STACK AND QUEUE

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

SYSC 2006 Winter 2012 Linear Collections: Queues

Object Oriented Programming COP3330 / CGS5409

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Ashish Gupta, Data JUET, Guna

CMSC 132: Object-Oriented Programming II. Stack and Queue

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Contents. Errors in Procedural Programming

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

! A data type for which: ! An ADT may be implemented using various. ! Examples:

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

More Data Structures (Part 1) Stacks

1 Short Answer (7 Points Each)

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Programming Abstractions

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

ITI Introduction to Computing II

Queues. Queue ADT Queue Implementation Priority Queues

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

CS 216 Exam 1 Fall SOLUTION

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Chapter 17: Linked Lists

Associate Professor Dr. Raed Ibraheem Hamed

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Linked List. April 2, 2007 Programming and Data Structure 1

Queue with Array Implementation

LIFO : Last In First Out

Stack and Queue. Stack:

Chapter 9 STACK, QUEUE

Stacks and Queues. CSE 373 Data Structures Lecture 6

Name CPTR246 Spring '17 (100 total points) Exam 3

Chapter 7: Stacks. Exercises 7.2

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

Stacks and Queues. CSE Data Structures April 12, 2002

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Dynamic Data Structures

Queue: Queue Representation: Basic Operations:

Unit 4: Stacks and Queues

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Lecture No.04. Data Structures

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Linked List using a Sentinel

ADTs Stack and Queue. Outline

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

CS 103 Unit 11. Linked Lists. Mark Redekopp

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Abstract Data Types. Abstract Data Types

CSEN 301 Data Structures and Algorithms

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

ADTs Stack and Queue. Outline

Data Structures using OOP C++ Lecture 9

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Arrays and Linked Lists

Data Structures. Jia-Liang Lu. Slides based on the course notes of C.A. Shaffer

C++ Templates. David Camp

Example Final Questions Instructions

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

Transcription:

Stacks and Queues Unit 6 Chapter 19.1-2,4-5 CS 2308 Fall 2018 Jill Seaman 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are specific, - how the data will be represented or how the operations will be implemented is unspecified. An ADT may be implemented using various specific data types or data structures, in many ways and in many programming languages. Examples: - NumberList (implemented using linked list or array) - string class (not sure how it s implemented) 2 19.1 Introduction to the Stack Stack: an abstract data type that holds a collection of elements of the same type. - The elements are accessed according to LIFO order: last in, first out - No random access to other elements Operations: Stack Operations - push: add a value onto the top of the stack make sure it s not full first. - pop: remove a value from the top of the stack make sure it s not empty first. Examples: - plates or trays in a cafeteria - bangles... 3 - isfull: true if the stack is currently full, i.e.,has no more space to hold additional elements - isempty: true if the stack currently contains no elements 4

Stack illustrated Implementing a Stack Class int item; stack.push(2); stack.push(3); stack.push(5); item = stack.pop(); //item is 5 item = stack.pop(); //item is 3 stack.push(10); item = stack.pop() item = stack.pop() 5 Array implementations: - fixed size (static) arrays: size doesn t change - dynamic arrays: can resize as needed in push Linked List - grow and shrink in size as needed 6 IntStack: A stack class IntStack: push class IntStack private: static const int STACK_SIZE = 100; // The stack size int stackarray[stack_size]; // The stack array int top; // Index to the top of the stack public: // Constructor IntStack() top = -1; // empty stack ; // Stack operations void push(int); int pop(); bool isfull() const; bool isempty() const; 7 //************************************************* // Member function push pushes the argument onto * // the stack. * //************************************************* void IntStack::push(int num) assert (isfull()); top++; stackarray[top] = num; Stack Overflow: attempting to push onto a full stack. assert will abort the program if its argument evaluates to false. it requires #include <cassert> The driver should ensure that the assert condition is always true before push is called. 8

IntStack: pop IntStack: test functions // Member function pop pops the value at the top * // of the stack off, and returns it as the result. * int IntStack::pop() assert (isempty()); int num = stackarray[top]; top--; return num; Stack Underflow: attempting to pop from an empty stack. The driver should ensure that the assert condition is always true before pop is called. //*************************************************** // Member function isfull returns true if the stack * // is full, or false otherwise. * //*************************************************** bool IntStack::isFull() const return (top == STACK_SIZE - 1); // Member function isempty returns true if the stack * // is empty, or false otherwise. * 9 bool IntStack::isEmpty() const return (top == -1); 10 IntStack: driver 19.4 Introduction to the Queue #include<iostream> using namespace std; #include "IntStack.h" int main() // set up the stack IntStack stack; stack.push(2); stack.push(3); stack.push(5); int x; x = stack.pop(); x = stack.pop(); stack.push(10); cout << x << endl; What is output? What is left on the stack when the driver is done? 11 Queue: an abstract data type that holds a collection of elements of the same type. - The elements are accessed according to FIFO order: first in, first out - No random access to other elements Examples: - people in line at a theatre box office - print jobs sent to a (shared) printer 12

Operations: Queue Operations q.enqueue(2); Queue illustrated q.enqueue(3); q.enqueue(5); item = q.dequeue(); item = q.dequeue(); q.enqueue(10); - enqueue: add a value onto the of the queue (the end of the line) make sure it s not full first. - dequeue: remove a value from the of the queue (the of the line) Next make sure it s not empty first. Note: and are variables used by the implementation to carry out the operations - isfull: true if the queue is currently full, i.e.,has no more space to hold additional elements - isempty: true if the queue currently contains no 13 elements int item; q.enqueue(2): q.enqueue(3); q.enqueue(5); item = q.dequeue(); //item is 2 item = q.dequeue(); //item is 3 q.enqueue(10); 14 Implementing a Queue Class Same as for Stacks: Array implementations: - fixed size (static) arrays: size doesn t change - dynamic arrays: can resize as needed in enqueue Linked List - grow and shrink in size as needed Implementing a Queue class issues using a fixed length array The previous illustration assumed we were using an array to implement the queue When an item was dequeued, the items were NOT shifted up to fill the slot vacated by dequeued item why not? Instead, both and indices move through the array. 15 16

Implementing a Queue Class Implementing a Queue Class When and indices move in the array: - problem: hits end of array quickly - solution: circular array : wrap index around to of array 7 9 6 q.enqueue(3): 3 7 9 6 q.enqueue(4): 3 4 7 9 6 17 To wrap the index back to the of the array, you can use this code to increment during enqueue: if ( == queuesize-1) = 0; else = +1; The following code is equivalent, but shorter (assuming 0 <= < queuesize): = ( + 1) % queuesize; Do the same for advancing the index. 18 Implementing a Queue Class Implementing a Queue Class When is it full? When is it empty? : 3 4 7 9 6 q.enqueue(5); q.enqueue(2); q.enqueue(1); 3 4 5 2 1 7 9 6 Note: enqueue increments : int x; for (int i=0; i<queuesize;i++) x = q.dequeue(); after the first one: one element left: no elements left, passes : 1 Note: dequeue increments 3 4 5 2 1 9 6 It s full: (+1)%queueSize== 19 It s empty: (+1)%queueSize== 20

Implementing a Queue Class IntQueue: a queue class When is it full? (+1)%queueSize== When is it empty? (+1)%queueSize== How do we define isfull and isempty? - Use a counter variable, numitems, to keep track of the total number of items in the queue. enqueue: numitems++ dequeue: numitems-- class IntQueue private: static const int QUEUE_SIZE = 100; //The queue size int queuearray[queue_size]; // The queue array int ; // Subscript of the elem int ; // Subscript of the elem int numitems; // Number of items in the queue public: // Constructor IntQueue() = 0; = -1; numitems = 0; // Queue operations void enqueue(int); int dequeue(); bool isempty(); bool isfull(); ; Why =0; =-1;? isempty is true when numitems == 0 The first enqueue increments and puts element at position 0 (now ====0). The first dequeue removes element at isfull is true when numitems == queuesize ( position 0). 21 22 A static queue: enqueue/dequeue // Enqueue inserts a value at the of the queue. * void IntQueue::enqueue(int num) assert(isfull()); = ( + 1) % QUEUESIZE; //calc new position queuearray[] = num; //insert new item numitems++; //update count // Dequeue removes the value at the of the * // queue and returns the value. * int IntQueue::dequeue() assert(isempty()); int result = queuearray[]; //retrieve item = ( + 1) % QUEUESIZE; //calc new position numitems ; //update count 23 return result; IntQueue: test functions // isempty returns true if the queue is empty, * // otherwise false. * bool IntQueue::isEmpty() return (numitems == 0); // isfull returns true if the queue is full, otherwise * // false. * bool IntQueue::isFull() return (numitems == QUEUE_SIZE); 24

IntQueue: driver 19.2 A Dynamic Stack Class: #include<iostream> using namespace std; #include "IntQueue.h" int main() // set up the queue IntQueue q; int item; q.enqueue(2); q.enqueue(3); q.enqueue(5); item = q.dequeue(); item = q.dequeue(); q.enqueue(10); cout << item << endl; What is output? What is left on the queue when the driver is done? 25 class DynIntStack private: struct Node int data; Node* next; ; Node* head; // ptr to top public: // Constructor DynIntStack() head = NULL; // empty stack ; head points to top element. add and remove at of list // Stack operations void push(int); int pop(); bool isfull() const return false; bool isempty() const return head == NULL; 26 A Dynamic Stack Class: Push and pop from the head of the list: A Dynamic Stack Class: Push and pop from the head of the list: //************************************************* // Member function push pushes the argument onto * // the stack. * //************************************************* void DynIntStack::push(int num) assert(isfull()); Node *temp = new Node; temp->data = num; temp->next = head; head = temp; //allocate new node //insert at head of list 27 // Member function pop pops the value at the top * // of the stack off, and returns it. * int DynIntStack::pop() assert(isempty()); int result = head->data; //retrieve item Node * temp = head; head = head->next; //head points to second item delete temp; //deallocate item return result; 28

19.5 A Dynamic Queue Class: Use pointers and to point to first and last elements of the list: NULL A Dynamic Queue Class: class DynIntQueue private: struct Node int data; Node* next; ; Node* ; // ptr to first Node* ; // ptr to last public: // Constructor DynIntQueue() = NULL; = NULL; // Queue operations void enqueue(int); int dequeue(); bool isfull() const return false; bool isempty() const return == NULL; ; 29 30 A Dynamic Queue Class: Enqueue at the, dequeue from the : A Dynamic Queue Class: Enqueue at the, dequeue from the : // Enqueue inserts a value at the of the queue. * void DynIntQueue::enqueue(int num) assert(isfull()); Node *temp=new Node; temp->data = num; temp->next = NULL; //allocate new node if (isempty()) = = temp; //set AND to node else ->next = temp; //append to of list = temp; //reset 31 // Dequeue removes the value at the of the * // queue and returns the value. * int DynIntQueue::dequeue() assert(isempty()); int value = ->data; Node *temp = ; = ->next; delete temp; return value; //retrieve item // points to 2nd item //deallocate removed item 32