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

Size: px
Start display at page:

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

Transcription

1 Queue ADT Cinda Heeren / Geoffrey Tien 1

2 PA1 testing Your code must compile with our private test file Any non-compiling submissions will receive zero Note that only functions that are called will be compiled Add full coverage of function calls to the given main.cpp Write your own full coverage tests in a custom main.cpp Add a target in the makefile to any new executable Write a function stub for any function that you are unable to complete A mostly empty implementation that returns a default value of the appropriate type Cinda Heeren / Geoffrey Tien 2

3 Queues Assume that we want to store data for a print queue for a student printer Student ID Time File name The printer is to be assigned to file in the order in which they are received A fair algorithm Example: Print queue Cinda Heeren / Geoffrey Tien 3

4 Classes for print queues To maintain the print queue we would require at least two classes Request class Collection class to store requests The collection class should support the desired behaviour FIFO (First In First Out) The ADT is a queue Cinda Heeren / Geoffrey Tien 4

5 Queues In a queue items are inserted at the back (end/tail) and removed from the front (head) Queues are FIFO (First In First Out) data structures fair data structures Applications include: Server requests Instant messaging servers queue up incoming messages Database requests Print queues Operating systems often use queues to schedule CPU jobs The waiting list for this course! (it s presumably fair) Various algorithm implementations Cinda Heeren / Geoffrey Tien 5

6 Queue operations A queue should implement at least the first two of these operations: enqueue insert item at the back of the queue dequeue remove an item from the front peek return the item at the front of the queue without removing it isempty check if the queue does not contain any items Like stacks, it is assumed that these operations will be implemented efficiently That is, in constant time Cinda Heeren / Geoffrey Tien 6

7 Queue implementation Consider using an array as the underlying structure for a queue, we could Make the back of the queue the current size of the array, much like the stack implementation Initially make the front of the queue index 0 Inserting an item is easy Using an array What to do when items are removed? Either move all remaining items down slow Or increment the front index wastes space Cinda Heeren / Geoffrey Tien 7

8 Circular arrays Trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end of the array wraps around to the start of the array Cinda Heeren / Geoffrey Tien 8

9 The modulo operator The mod operator (%) calculates remainders: 1%5 = 1, 2%5 = 2, 5%5 = 0, 8%5 = 3 The mod operator can be used to calculate the front and back positions in a circular array Thereby avoiding comparisons to the array size The back of the queue is: (front + num) % arrlength where num is the number of items in the queue After removing an item, the front of the queue is: (front + 1) % arrlength Member attributes: int front; int arrlength; int* arr; int num; Cinda Heeren / Geoffrey Tien 9

10 Array queue example Queue q; q.enqueue(6); 0 01 front num Insert item at (front + num) % queue.length, then increment num Cinda Heeren / Geoffrey Tien 10

11 Array Queue Example Queue q; q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue(); q.dequeue(); 01 2 front 6 num Insert item at (front + num) % queue.length, then increment num Remove item at front, then decrement num and make front = (front + 1) % queue.length Cinda Heeren / Geoffrey Tien 11

12 Array Queue Example Queue q; q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue(); q.dequeue(); q.enqueue(9); q.enqueue(5); 2 front 5 num Insert item at (front + num) % queue.length, then increment num Remove item at front, then decrement num and make front = (front + 1) % queue.length 9 Need to check that the back of the queue does not overtake the front Cinda Heeren / Geoffrey Tien 12

13 Array queue resizing Suppose we have an array-based queue and we have performed some enqueue and dequeue operations Then we perform more enqueues to fill the array How should we resize the array to allow for more enqueue operations? front???????????? Cinda Heeren / Geoffrey Tien 13

14 Queue Implementation Using a Linked List Removing items from the front of the queue is straightforward Items should be inserted at the back of the queue in constant time So we must avoid traversing through the list Use a second node pointer to keep track of the node at the back of the queue Requires a little extra administration Member attributes: Node* front; Node* back; int num; Cinda Heeren / Geoffrey Tien 14

15 List Queue Example Queue q; q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.dequeue(); front back Cinda Heeren / Geoffrey Tien 15

16 Exercise Given a Queue class implemented with a (dynamic) circular array of 8 int elements and an integer size member, the following block of code is executed: Queue qa; qa.enqueue(34); qa.enqueue(29); qa.enqueue(11); qa.dequeue(); qa.enqueue(47); qa.dequeue(); Queue qb = qa; qa.enqueue(6); qb.enqueue(52); qb.dequeue(); qb.dequeue(); qa.dequeue(); Shallow Copy, Deep Copy What will be output as the contents of each queue qa and qb if: qb is created as a shallow copy of qa? qb is created as a deep copy of qb? Cinda Heeren / Geoffrey Tien 16

17 Recursion Cinda Heeren / Geoffrey Tien 17

18 Rabbits! What happens when you put a pair of rabbits in a field? More rabbits! Let s model the rabbit population, with a few assumptions: Newly-born rabbits take one month to reach maturity and mate Each pair of rabbits produces another pair of rabbits one month after mating Rabbits never die...and recursion Cinda Heeren / Geoffrey Tien 18

19 More rabbits... How many rabbit pairs are there after 5 months? Month 1: start 1 pair Month 2: first pair are now mature and mate 1 pair Month 3: first pair give birth to a pair of babies original pair + baby pair = 2 pairs Month 4: first pair give birth to another pair of babies, pair born in month 3 are now mature 3 pairs Month Month 5: the 3 pairs from month 4, and two new pairs 5 pairs Month 6: the 5 pairs from month 5, and three new pairs 8 pairs And so on... Cinda Heeren / Geoffrey Tien 19

20 Fibonacci series The n th number in the Fibonacci series, fib(n), is: 0 if n = 0, and 1 if n = 1 fib(n 1) + fib(n 2) for any n > 1 e.g. what is fib(23) Easy if we only knew fib(22) and fib(21) The answer is fib(22) + fib(21) What happens if we actually write a function to calculate Fibonacci numbers like this? Cinda Heeren / Geoffrey Tien 20

21 Calculating the Fibonacci series Let s write a function just like the formula fib(n) = 0 if n = 0, 1 if n = 1, otherwise fib(n) = fib(n 1) + fib(n 2) int fib(int n) { if (n <= 1) return max(0, n); else return fib(n-1) + fib(n-2); } The function calls itself Cinda Heeren / Geoffrey Tien 21

22 Recursive functions The Fibonacci function is recursive A recursive function calls itself Each call to a recursive method results in a separate call to the method, with its own input Recursive functions are just like other functions The invocation (e.g. parameters, etc.) is pushed onto the call stack And removed from the call stack when the end of a method or a return statement is reached Execution returns to the previous method call Cinda Heeren / Geoffrey Tien 22

23 Recursive function anatomy Recursive functions do not use loops to repeat instructions But use recursive calls, in if statements Recursive functions consist of two or more cases, there must be at least one Base case, and Recursive case Cinda Heeren / Geoffrey Tien 23

24 Recursion cases The base case is a smaller problem with a known solution This problem s solution must not be recursive Otherwise the function may never terminate There can be more than one base case And base cases may be implicit The recursive case is the same problem with smaller input The recursive case must include a recursive function call There can be more than one recursive case Cinda Heeren / Geoffrey Tien 24

25 Analysis of fib(5) int fib(int n) { if (n <= 1) return max(0, n); else return fib(n-1) + fib(n-2); } 3 5 fib(5) 2 fib(4) fib(3) fib(3) fib(2) fib(2) fib(1) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) 1 fib(1) 0 fib(0) Later in the course we will explain how this is an extremely inefficient way to compute the Fibonacci series Cinda Heeren / Geoffrey Tien 25

26 Example Base cases Target is found, or the end of the array is reached Recursive case Recursive linear search Target not found, search subarray starting from next element // Recursive linear search int reclinsearch(int arr[], int next, int sz, int x) { if (next >= sz) // end of array reached return -1; else if (x == arr[next]) // target found return next; else // not found, search from different starting index return reclinsearch(arr, next + 1, sz, x); } Cinda Heeren / Geoffrey Tien 26

27 Readings for this lesson Carrano & Henry Chapter (Queues) Chapter 2 (Recursion) Next class: Carrano & Henry, Chapter 15.1 (Trees) Cinda Heeren / Geoffrey Tien 27

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

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1 Queues ADT description Implementations Cinda Heeren / Geoffrey Tien 1 Queues Assume that we want to store data for a print queue for a student printer Student ID Time File name The printer is to be assigned

More information

Tree traversals. Review: recursion Tree traversals. October 05, 2017 Cinda Heeren / Geoffrey Tien 1

Tree traversals. Review: recursion Tree traversals. October 05, 2017 Cinda Heeren / Geoffrey Tien 1 Tree traversals Review: recursion Tree traversals Cinda Heeren / Geoffrey Tien 1 Rabbits! What happens when you put a pair of rabbits in a field? More rabbits! Let s model the rabbit population, with a

More information

Recursion. February 02, 2018 Cinda Heeren / Geoffrey Tien 1

Recursion. February 02, 2018 Cinda Heeren / Geoffrey Tien 1 Recursion February 02, 2018 Cinda Heeren / Geoffrey Tien 1 Function calls in daily life How do you handle interruptions in daily life? You're at home, working on PA1 You stop to look up something in the

More information

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

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queues October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queue ADT Queue ADT should support at least the first two operations: enqueue insert an item to the back of the queue dequeue remove an item from

More information

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling Recursion Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling With reference to the call stack Compute the result of simple recursive algorithms Understand

More information

Arrays and Linked Lists

Arrays and Linked Lists Arrays and Linked Lists Abstract Data Types Stacks Queues Priority Queues and Deques John Edgar 2 And Stacks Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every

More information

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1 Abstract Data Types Stack January 26, 2018 Cinda Heeren / Geoffrey Tien 1 Abstract data types and data structures An Abstract Data Type (ADT) is: A collection of data Describes what data are stored but

More information

What Can You Use a Queue For?

What Can You Use a Queue For? Cmpt-225 Queues Queues A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First Out) data structures fair data structures

More information

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

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let

More information

Queues. Queue ADT Queue Implementation Priority Queues

Queues. Queue ADT Queue Implementation Priority Queues Queues Queue ADT Queue Implementation Priority Queues Queue A restricted access container that stores a linear collection. Very common for solving problems in computer science that require data to be processed

More information

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

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 is available.

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Queues ArrayQueue Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 10, 2014 Abstract These lecture notes are meant to be looked

More information

Hash Tables. Hash functions Open addressing. March 07, 2018 Cinda Heeren / Geoffrey Tien 1

Hash Tables. Hash functions Open addressing. March 07, 2018 Cinda Heeren / Geoffrey Tien 1 Hash Tables Hash functions Open addressing Cinda Heeren / Geoffrey Tien 1 Hash functions A hash function is a function that map key values to array indexes Hash functions are performed in two steps Map

More information

Stack and Queue. Stack:

Stack and Queue. Stack: Stack and Queue Stack: Abstract Data Type A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations

More information

Hash Tables. Lower bounds on sorting Hash functions. March 05, 2018 Cinda Heeren / Geoffrey Tien 1

Hash Tables. Lower bounds on sorting Hash functions. March 05, 2018 Cinda Heeren / Geoffrey Tien 1 Hash Tables Lower bounds on sorting Hash functions Cinda Heeren / Geoffrey Tien 1 Detour back to sorting Lower bounds on worst case Selection sort, Insertion sort, Quicksort, Merge sort Worst case complexities

More information

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

! A data type for which: ! An ADT may be implemented using various. ! Examples: 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

More information

More Data Structures (Part 1) Stacks

More Data Structures (Part 1) Stacks More Data Structures (Part 1) Stacks 1 Stack examples of stacks 2 Top of Stack top of the stack 3 Stack Operations classically, stacks only support two operations 1. push 2. pop add to the top of the stack

More information

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1 Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed

More information

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal Queues COL 106 Slides by Amit Kumar, Shweta Agrawal The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out (FIFO) scheme Insertions are at the rear

More information

LIFO : Last In First Out

LIFO : Last In First Out Introduction Stack is an ordered list in which all insertions and deletions are made at one end, called the top. Stack is a data structure that is particularly useful in applications involving reversing.

More information

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science Queues Lesson 4 Outline What is a queue? Straight queue Circular Queue Sequential Implementation Linked Implementation Application: Topological sort Deques Final Notes Outline What is a queue? Straight

More information

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

Priority Queue ADT. Revised based on textbook author s notes. Priority Queue ADT Revised based on textbook author s notes. Priority Queues Some applications require the use of a queue in which items are assigned a priority. higher priority items are dequeued first.

More information

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

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples: Ch. 8: ADTs: Stacks and Queues Abstract Data Type A data type for which: CS 8 Fall Jill Seaman - only the properties of the data and the operations to be performed on the data are specific, - not concerned

More information

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

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero). Suppose we have a circular array implementation of the queue,with ten items in the queue stored at data[2] through data[11]. The current capacity of an array is 12. Where does the insert method place the

More information

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

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016 Queues Gaddis 18.4, 18.6 Molly A. O'Neil CS 2308 :: Spring 2016 The Queue ADT A queue is an abstract data type that stores a collection of elements of the same type The elements of a queue are accessed

More information

12 Abstract Data Types

12 Abstract Data Types 12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define

More information

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 13 Merging using Queue ADT and Queue types In the

More information

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

CSE 373 Data Structures and Algorithms. Lecture 2: Queues CSE 373 Data Structures and Algorithms Lecture 2: Queues Queue ADT queue: A list with the restriction that insertions are done at one end and deletions are done at the other First-In, First-Out ("FIFO

More information

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

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi SCJ2013 Data Structure & Algorithms Queue Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi Course Objectives At the end of the lesson students are expected to be able to: Understand queue concepts and

More information

Answers to review questions from Chapter 2

Answers to review questions from Chapter 2 Answers to review questions from Chapter 2 1. Explain in your own words the difference between a method and a program. A method computes a value or performs some operation on behalf of the code for a program.

More information

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. Week 9. elements of the same type. Stacks and Queues Week 9 Gaddis: Chapter 18 (8th ed.) Gaddis: Chapter 19 (9th ed.) CS 5301 Fall 2018 Jill Seaman Introduction to the Stack Stack: a data structure that holds a collection of elements of

More information

CS6202 - PROGRAMMING & DATA STRUCTURES I Unit IV Part - A 1. Define Stack. A stack is an ordered list in which all insertions and deletions are made at one end, called the top. It is an abstract data type

More information

C++ Object-Oriented Programming

C++ Object-Oriented Programming C++ Object-Oriented Programming Templates Deep copy Deep delete September 26, 2017 Cinda Heeren / Geoffrey Tien 1 PA1 LinkedList In PA1, you are asked to implement a linked list whose nodes store Kebab

More information

Stacks and Queues. CSE 373 Data Structures Lecture 6

Stacks and Queues. CSE 373 Data Structures Lecture 6 Stacks and Queues CSE 373 Data Structures Lecture 6 Readings and References Reading Sections 3.3 and 3.4 10/11/02 Stacks and Queues - Lecture 6 2 Stacks A list for which Insert and Delete are allowed only

More information

csci 210: Data Structures Stacks and Queues

csci 210: Data Structures Stacks and Queues csci 210: Data Structures Stacks and Queues 1 Summary Topics Stacks and Queues as abstract data types ( ADT) Implementations arrays linked lists Analysis and comparison Applications: searching with stacks

More information

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY Computer Science Foundation Exam Dec. 19, 2003 COMPUTER SCIENCE I Section I A No Calculators! Name: KEY SSN: Score: 50 In this section of the exam, there are Three (3) problems You must do all of them.

More information

1 P age DS & OOPS / UNIT II

1 P age DS & OOPS / UNIT II UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular

More information

Linked Memory. Pointers Linked Lists. January 19, 2018 Cinda Heeren / Geoffrey Tien 1

Linked Memory. Pointers Linked Lists. January 19, 2018 Cinda Heeren / Geoffrey Tien 1 Linked Memory Pointers Linked Lists January 19, 2018 Cinda Heeren / Geoffrey Tien 1 Addresses and pointers Every storage location in memory (RAM) has an address associated with it The address is the location

More information

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

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18 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

More information

CE204 Data Structures and Algorithms Part 2

CE204 Data Structures and Algorithms Part 2 CE204 Data Structures and Algorithms Part 2 14/01/2018 CE204 Part 2 1 Abstract Data Types 1 An abstract data type is a type that may be specified completely without the use of any programming language.

More information

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

CMSC 132: Object-Oriented Programming II. Stack and Queue CMSC 132: Object-Oriented Programming II Stack and Queue 1 Stack Allows access to only the last item inserted. An item is inserted or removed from the stack from one end called the top of the stack. This

More information

Your Topic Goes Here Queue

Your Topic Goes Here Queue Your Subtopics Go Here Your Topic Goes Here Queue Compiled By: Kiran Joshi 1 Queue Roadmap. 1. Definition 2. Queue examples 3. Working of Queue 4. Applications 2 Definition * A data structure of ordered

More information

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out Stack and Queue 1 Stack Data structure with Last-In First-Out (LIFO) behavior In Out C B A B C 2 Typical Operations Pop on Stack Push isempty: determines if the stack has no elements isfull: determines

More information

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1)

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1) lecture12: Largely based on slides by Cinda Heeren CS 225 UIUC 27th June, 2013 Announcements mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1) Source: http://i.dailymail.co.uk/i/pix/2010/08/04/article-1300471-0ab0a1d2000005dc-451

More information

CS 216 Exam 1 Fall SOLUTION

CS 216 Exam 1 Fall SOLUTION CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT

More information

CSC 1052 Algorithms & Data Structures II: Queues

CSC 1052 Algorithms & Data Structures II: Queues CSC 1052 Algorithms & Data Structures II: Queues Professor Henry Carter Spring 2018 Recap Recursion solves problems by solving smaller version of the same problem Three components Applicable in a range

More information

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

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type Ch. 18: ADTs: Stacks and Queues CS 2308 Fall 2011 Jill Seaman Lecture 18 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

More information

Queue rear tail front head FIFO

Queue rear tail front head FIFO The Queue ADT Objectives Examine queue processing Define a queue abstract data type Demonstrate how a queue can be used to solve problems Examine various queue implementations Compare queue implementations

More information

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,

More information

DEEPIKA KAMBOJ UNIT 2. What is Stack?

DEEPIKA KAMBOJ UNIT 2. What is Stack? What is Stack? UNIT 2 Stack is an important data structure which stores its elements in an ordered manner. You must have seen a pile of plates where one plate is placed on top of another. Now, when you

More information

STACKS AND QUEUES. Problem Solving with Computers-II

STACKS AND QUEUES. Problem Solving with Computers-II STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()

More information

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER

More information

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

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) Unit - 2 By: Gurpreet Singh Dean Academics & H.O.D. (C.S.E. / I.T.) Yamuna Institute of Engineering & Technology, Gadholi What is a Stack? A stack is a

More information

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Queues. CITS2200 Data Structures and Algorithms. Topic 5 CITS2200 Data Structures and Algorithms Topic 5 Queues Implementations of the Queue ADT Queue specification Queue interface Block (array) representations of queues Recursive (linked) representations of

More information

.:: UNIT 4 ::. STACK AND QUEUE

.:: UNIT 4 ::. STACK AND QUEUE .:: UNIT 4 ::. STACK AND QUEUE 4.1 A stack is a data structure that supports: Push(x) Insert x to the top element in stack Pop Remove the top item from stack A stack is collection of data item arrange

More information

Information Science 2

Information Science 2 Information Science 2 - Basic Data Structures- Week 02 College of Information Science and Engineering Ritsumeikan University Today s class outline l Basic data structures: Definitions and implementation

More information

ADTs: Stacks and Queues

ADTs: Stacks and Queues Introduction to the Stack ADTs: 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

More information

Queues Fall 2018 Margaret Reid-Miller

Queues Fall 2018 Margaret Reid-Miller Queues 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Writing methods various classes that implement Lists. Methods using Lists and Big-O w/ ArrayList or LinkedLists Prove

More information

Data Structure. Recitation VII

Data Structure. Recitation VII Data Structure Recitation VII Recursion: Stack trace Queue Topic animation Trace Recursive factorial Executes factorial(4) Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4)

More information

Linked Memory. Pointers Linked Lists. September 21, 2017 Cinda Heeren / Geoffrey Tien 1

Linked Memory. Pointers Linked Lists. September 21, 2017 Cinda Heeren / Geoffrey Tien 1 Linked Memory Pointers Linked Lists September 21, 2017 Cinda Heeren / Geoffrey Tien 1 Releasing Dynamic Memory When a function call is complete its stack memory is released and can be re-used Dynamic memory

More information

infix expressions (review)

infix expressions (review) Outline infix, prefix, and postfix expressions queues queue interface queue applications queue implementation: array queue queue implementation: linked queue application of queues and stacks: data structure

More information

Problem 1: Building and testing your own linked indexed list

Problem 1: Building and testing your own linked indexed list CSCI 200 Lab 8 Implementing a Linked Indexed List In this lab, you will be constructing a linked indexed list. You ll then use your list to build and test a new linked queue implementation. Objectives:

More information

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution 1. (40 points) Write the following subroutine in x86 assembly: Recall that: int f(int v1, int v2, int v3) { int x = v1 + v2; urn (x + v3) * (x v3); Subroutine arguments are passed on the stack, and can

More information

CSEN 301 Data Structures and Algorithms

CSEN 301 Data Structures and Algorithms CSEN 301 Data Structures and Algorithms Lecture 5: Queues: Implementation and usage Prof. Dr. Slim Abdennadher slim.abdennadher@guc.edu.eg German University Cairo, Department of Media Engineering and Technology

More information

Recursive Definitions

Recursive Definitions Recursion Objectives Explain the underlying concepts of recursion Examine recursive methods and unravel their processing steps Explain when recursion should and should not be used Demonstrate the use of

More information

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)

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) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading for this lecture: Carrano, Chapter 2 Copy constructor, destructor, operator=

More information

Basic Data Structures

Basic Data Structures Basic Data Structures Some Java Preliminaries Generics (aka parametrized types) is a Java mechanism that enables the implementation of collection ADTs that can store any type of data Stack s1

More information

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014 University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014 Midterm Examination Instructor: Ladan Tahvildari, PhD, PEng, SMIEEE Date: Tuesday,

More information

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1 CMSC 341 Linked Lists, Stacks and Queues CMSC 341 Lists, Stacks &Queues 1 Goal of the Lecture To complete iterator implementation for ArrayList Briefly talk about implementing a LinkedList Introduce special

More information

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50 Lists, Stacks, and Queues (Lists, Stacks, and Queues ) Data Structures and Programming Spring 2016 1 / 50 Abstract Data Types (ADT) Data type a set of objects + a set of operations Example: integer set

More information

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

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) An interesting definition for stack element The stack element could be of any data type struct stackelement int etype; union int ival;

More information

Announcements Queues. Queues

Announcements Queues. Queues Announcements. Queues Queues A queue is consistent with the general concept of a waiting line to buy movie tickets a request to print a document crawling the web to retrieve documents A queue is a linear

More information

Postfix (and prefix) notation

Postfix (and prefix) notation Postfix (and prefix) notation Also called reverse Polish reversed form of notation devised by mathematician named Jan Łukasiewicz (so really lü-kä-sha-vech notation) Infix notation is: operand operator

More information

Linked Structures. See Section 3.2 of the text.

Linked Structures. See Section 3.2 of the text. Linked Structures See Section 3.2 of the text. First, notice that Java allows classes to be recursive, in the sense that a class can have an element which is itself an object of that class: class Person

More information

Data Structures and Algorithms. Roberto Sebastiani

Data Structures and Algorithms. Roberto Sebastiani Data Structures and Algorithms Roberto Sebastiani roberto.sebastiani@disi.unitn.it http://www.disi.unitn.it/~rseba - Week 05 - B.S. In Applied Computer Science Free University of Bozen/Bolzano academic

More information

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

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page: CSE 326 Team CSE 326: Data Structures Instructor: Steve Seitz TAs: Winter 2009 Steve Seitz Lecture 1 Eric McCambridge Brent Sandona Soyoung Shin Josh Barr 2 Today s Outline Introductions Administrative

More information

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

double d0, d1, d2, d3; double * dp = new double[4]; double da[4]; All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers

More information

Basic Data Structures 1 / 24

Basic Data Structures 1 / 24 Basic Data Structures 1 / 24 Outline 1 Some Java Preliminaries 2 Linked Lists 3 Bags 4 Queues 5 Stacks 6 Performance Characteristics 2 / 24 Some Java Preliminaries Generics (aka parametrized types) is

More information

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue Module 4: Queue ADT Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Queue ADT Features (Logical View) A List that

More information

CMSC Introduction to Algorithms Spring 2012 Lecture 7

CMSC Introduction to Algorithms Spring 2012 Lecture 7 CMSC 351 - Introduction to Algorithms Spring 2012 Lecture 7 Instructor: MohammadTaghi Hajiaghayi Scribe: Rajesh Chitnis 1 Introduction In this lecture we give an introduction to Data Structures like arrays,

More information

III Data Structures. Dynamic sets

III Data Structures. Dynamic sets III Data Structures Elementary Data Structures Hash Tables Binary Search Trees Red-Black Trees Dynamic sets Sets are fundamental to computer science Algorithms may require several different types of operations

More information

Motivation for Queues

Motivation for Queues CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 178] Motivation for Queues Some examples of first-in, first-out (FIFO) behavior: æ waiting in line to check out at a store æ cars on

More information

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving In Computer Science, an abstract data type (ADT) is a mathematical model for a certain data type or structures. This doesn t mean that the ADTs cannot be programmed. But that we must first understand them

More information

UNIT-2 Stack & Queue

UNIT-2 Stack & Queue UNIT-2 Stack & Queue 59 13. Stack A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards

More information

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Introduction In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station etc. The person who come first, he/she

More information

Lecture 6 CS2110 Spring 2013 RECURSION

Lecture 6 CS2110 Spring 2013 RECURSION Lecture 6 CS2110 Spring 2013 RECURSION Recursion 2 Arises in three forms in computer science Recursion as a mathematical tool for defining a function in terms of its own value in a simpler case Recursion

More information

Unit #2: Recursion, Induction, and Loop Invariants

Unit #2: Recursion, Induction, and Loop Invariants Unit #2: Recursion, Induction, and Loop Invariants CPSC 221: Algorithms and Data Structures Will Evans 2012W1 Unit Outline Thinking Recursively Recursion Examples Analyzing Recursion: Induction and Recurrences

More information

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Chapter 5: Stacks, ueues and Deques Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,

More information

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow. Assignment 1 Check your mailbox on Thursday! Grade and feedback published by tomorrow. COMP250: Queues, deques, and doubly-linked lists Lecture 20 Jérôme Waldispühl School of Computer Science McGill University

More information

CS 211: Recursion. Chris Kauffman. Week 13-1

CS 211: Recursion. Chris Kauffman. Week 13-1 CS 211: Recursion Chris Kauffman Week 13-1 Front Matter Today P6 Questions Recursion, Stacks Labs 13: Due today 14: Review and evals Incentive to attend lab 14, announce Tue/Wed End Game 4/24 Mon P6, Comparisons

More information

Queue. COMP 250 Fall queues Sept. 23, 2016

Queue. COMP 250 Fall queues Sept. 23, 2016 Queue Last lecture we looked at a abstract data type called a stack. The two main operations were push and pop. I introduced the idea of a stack by saying that it was a kind of list, but with a restricted

More information

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types 1 Data Structures and Algorithms Chapter 5 and Abstract Data Types Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press

More information

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures Computer Science 210 Data Structures Siena College Fall 2018 Topic Notes: Linear Structures The structures we ve seen so far, Vectors/ArrayLists and linked list variations, allow insertion and deletion

More information

CS24 Week 4 Lecture 2

CS24 Week 4 Lecture 2 CS24 Week 4 Lecture 2 Kyle Dewey Overview Linked Lists Stacks Queues Linked Lists Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track

More information

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu

More information

CS 225. April 6 Disjoint Sets Implementation. Data Structures. Wade Fagen-Ulmschneider

CS 225. April 6 Disjoint Sets Implementation. Data Structures. Wade Fagen-Ulmschneider CS 225 Data Structures April 6 Disjoint Sets Implementation Wade Fagen-Ulmschneider Disjoint Sets 2 5 9 7 0 1 4 8 3 6 5 7 4 3 9 2 0 8 6 1 0 4 1 2 3 4 5 6 7 8 5-1 -1-1 3-1 8 9 4 5 Disjoint Sets Find 1 2

More information

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list) ASSIGNMENT 2 SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list) Date: 30/9/2015 Due Date: 30/10/2015 Early Bird Date: 23/10/2015 QUESTION

More information

LECTURE OBJECTIVES 6-2

LECTURE OBJECTIVES 6-2 The Queue ADT LECTURE OBJECTIVES Examine queue processing Define a queue abstract data type Demonstrate how a queue can be used to solve problems Examine various queue implementations Compare queue implementations

More information

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion? CH. 5 RECURSION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OVERVIEW Recursion is an algorithmic

More information