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

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

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

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:

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

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

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

Chapter 18: Stacks And Queues

CS24 Week 4 Lecture 2

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

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)

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

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

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

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

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


UNIT-2 Stack & Queue

.:: UNIT 4 ::. STACK AND QUEUE

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

Lecture Data Structure Stack

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

More Data Structures (Part 1) Stacks

An Introduction to Queues With Examples in C++

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Contents. Errors in Procedural Programming

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

Ch. 17: Linked Lists. Introduction to Linked Lists

! 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)

! 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)

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

SYSC 2006 Winter 2012 Linear Collections: Queues

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

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

CS107, Lecture 9 C Generics Function Pointers

Stack and Queue. Stack:

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.

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

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

Chapter 9 STACK, QUEUE

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

Programming Abstractions

STACKS AND QUEUES. Problem Solving with Computers-II

UNIVERSITY REGULATIONS

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

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

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

CS 216 Exam 1 Fall SOLUTION

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

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

Object Oriented Programming COP3330 / CGS5409

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

Stacks and Queues. Stack - Abstract Data Type. Stack Applications. Stack - Abstract Data Type - C Interface

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

Unit 4: Stacks and Queues

Associate Professor Dr. Raed Ibraheem Hamed

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSC 1052 Algorithms & Data Structures II: Queues

Queues. Queue ADT Queue Implementation Priority Queues

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

1 P age DS & OOPS / UNIT II

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

C++ Templates. David Camp

Queue: Queue Representation: Basic Operations:

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

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

Data Abstraction and Specification of ADTs

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

15. Stacks and Queues

Priority Queue ADT. Revised based on textbook author s notes.

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

ADTs Stack and Queue. Outline

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

1 Short Answer (7 Points Each)

Information Science 2

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

Apply to be a Meiklejohn! tinyurl.com/meikapply

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

Chapter 5. ADTs Stack and Queue

== isn t always equal?

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

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

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

CMPT 125: Practice Midterm Answer Key

Lecture 3: Stacks & Queues

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Strux. Sophie Stadler srs2231 Manager Millie Yang my2440 Tester. 26 September 2017

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

ITI Introduction to Computing II

Introduction to the C programming language

Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü

Transcription:

Stacks and Queues Week 9 Gaddis: Chapter 18 CS 5301 Fall 2015 Jill Seaman Introduction to the Stack Stack: a data structure 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 Examples: - plates in a cafeteria - bangles... 1 2 Stack Operations Stack illustrated Operations: - push: add a value onto the top of the stack make sure it s not full first. - pop: remove (and return) the value from the top of the stack make sure it s not empty first. - 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 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); 3 4

Implementing a Stack Class 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 Templates - any of the above can be implemented using templates A static stack class class IntStack const int STACKSIZE = 100; // The stack size int stackarray[stacksize]; // 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; 5 6 A static stack class: push&pop A static stack class: functions // Member function push pushes the argument onto * // the stack. * void IntStack::push(int num) assert(isfull()); top++; stackarray[top] = num; assert will abort the program if its argument evaluates to false // Member function pop pops the value at the top * // of the stack off, and returns it. * int IntStack::pop() assert(isempty()); int num = stackarray[top]; top--; return num; 7 ** // Member function isfull returns true if the stack * // is full, or false otherwise. * ** bool IntStack::isFull() const return (top == stacksize - 1); // Member function isempty returns true if the stack * // is empty, or false otherwise. * bool IntStack::isEmpty() const return (top == -1); 8

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. * // Member function pop pops the value at the top * // of the stack off, and returns it. * void IntStack2::push(int num) assert(isfull()); Node *temp=new Node; temp->data = num; //insert at head of list temp->next = head; head = temp; struct Node ; Node* head; // ptr to top int IntStack2::pop() assert(isempty()); int result = head->data; Node * temp = head; head = head->next; delete temp; return result; struct Node ; Node* head; // ptr to top 9 10 Introduction to the Queue Queue: a data structure 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 - restocking perishable inventory 11 Operations: Queue Operations - 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. - 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 elements 12

q.enqueue(2); Queue illustrated q.enqueue(3); q.enqueue(5); item = q.dequeue(); Note: and are variables used by the implementation to carry out the operations 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); item = q.dequeue(); q.enqueue(10); 13 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 Templates - any of the above can be implemented using templates 14 When and indices move in the array: problem: hits end of array quickly solution: wrap index around to of array 7 9 6 q.enqueue(3): 3 7 9 6 q.enqueue(4): 3 4 7 9 6 15 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. 16

t: t: When is it full? 3 4 5 2 1 7 9 6 When is it empty? one element left: no elements left, passes : (+1)%queueSize== 1 Don t use and to determine if the queue is full or empty (+1)%queueSize== 17 A static queue class class IntQueue const int QUEUESIZE = 100; // capacity of the queue int queuearray[queuesize]; // The queue array int ; // Subscript of the queue int ; // Subscript of the queue int numitems; // Number of items in the queue public: // Constructor IntQueue() = 0; = -1; numitems = 0; ; // Queue operations void enqueue(int); int dequeue(); bool isempty() const; bool isfull() const; 18 A static queue: enqueue/dequeue A static queue class: functions // Enqueue inserts a value at the of the queue. * void IntQueue::enqueue(int num) assert(isfull()); = ( + 1) % QUEUESIZE; queuearray[] = num; numitems++; // Dequeue removes the value at the of the * // queue and returns the value. * int IntQueue::dequeue() assert(isempty()); // isempty returns true if the queue is empty * bool IntQueue::isEmpty() const return (numitems == 0); // isfull returns true if the queue is full * bool IntQueue::isFull() const return (numitems == QUEUESIZE); int result = queuearray[]; = ( + 1) % QUEUESIZE; numitems--; return result; 19 20

A Dynamic Queue Class: Use pointers and to point to first and last elements of the list: NULL A Dynamic Queue Class: Enqueue at the of the list, dequeue from the : // Enqueue inserts a value at the of the queue. * void IntQueue2::enqueue(int num) assert(isfull()); Node *temp=new Node; temp->data = num; temp->next = NULL; struct Node ; Node* ; // ptr to first Node* ; // ptr to last 21 //append to of list, reset if (isempty()) = = temp; else ->next = temp; = temp; 22 A Dynamic Queue Class: Enqueue at the of the list, dequeue from the : // Dequeue removes the value at the of the * // queue and returns the value. * int IntQueue2::dequeue() assert(isempty()); int value = ->data; // remove the first node () Node *temp = ; = ->next; delete temp; struct Node ; Node* ; // ptr to first Node* ; // ptr to last Sample Problems Stack Copy Constructor : Modify the DynIntStack from the textbook (or your answer to Programming Challenge 2, Dynamic Stack Template) to add a copy constructor. Queue Copy Constructor : Modify the DynIntQueue from the textbook (or your answer to Programming Challenge 4, Dynamic Queue Template) to add a copy constructor. return value; 23 24