Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
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.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
Stacks and Queues. 2 Stack and Queue ADT’s You should understand How are they different philosophically from arrays What are they used for How do you.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Data Structure Dr. Mohamed Khafagy.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
CS Data Structures II Review COSC 2006 April 14, 2017
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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.
Lists CSE1303 Part A Data Structures and Algorithms.
Stacks, Queues, and Deques
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Objectives of these slides:
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Stacks and Queues Introduction to Computing Science and Programming I.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
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,
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
Stacks And Queues Chapter 18.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CS 367 Introduction to Data Structures Lecture 5.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Elementary Data Structures
Review Array Array Elements Accessing array elements
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
Queues Queues Queues.
CMSC 341 Lecture 5 Stacks, Queues
Lecture 2: Implementing ADTs
Stacks, Queues, and Deques
Arrays and Linked Lists
Linked List (Part I) Data structure.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II

Data structures  Arrays, linked lists, … are best for data that represents real objects.  Stacks and Queues are used to complete a task and are soon after discarded  A major difference is Stacks and Queues only allow a single item to be added or removed at a time.  Stack: LIFO  Queue: FIFO CS2336: Computer Science II

Stack  Implement Stack using linkedlists (see the demo)  Each operation takes constant time  Also we need extra space  Objects (16*2 bytes overhead)  Two references(item and next) 8 bytes  Total ~40 bytes  For N items we may have 40N bytes  So there is better implementation available  Specially that stacks are use in the loop some algorithms it is important to use the faster and better implementation  Use array to store items CS2336: Computer Science II

Stacks  Stacks are simpler than queues  We show them as an Array of String[]  It holds all of the values that are push to the stack  We monitor  The size of the stack  The size of the array  The top of stack  It is last in first out  When it is empty then its value is “-1” CS2336: Computer Science II

Stacks: array implementation  A “push” method to put an item into stack  Check if stack is not full  if “true” then put the input item to the top of the array  Increment the array size by one  A “pop” method to remove the last information out of stack  Check if the stack is not empty  If “true” then we make the top of stack unavailable (“-1”)  Just like memory, when you delete an item it is still there but it is not available, we simulate that  Decrement the top of stack  A “peek” method to see what is at the top of stack, but not remove it  Also “PuchMany”, “PopAll”, “isEmpty, size… CS2336: Computer Science II

Stack example top10 CS2336: Computer Science II s.Push (10); top15 10 s.Push (15); top10 s.Remove(); Stack s = new Stack(10)

Stack: resizing-array implementation Problem: requiring the user to provide a size for the array! How to grow array?  First try: increase and decrease size of array by 1 for any push and pop  Expensive  Need to create a new array size one bigger and copy all items  Inserting first N items take proportional to 1+2+…+n ~ O(n 2 )  How do the resizing but ensure that resizing happens infrequently? CS2336: Computer Science II

Stack: resizing-array implementation Alternative: When array is full, create a new array of twice the size, and copy items.  Repeated doubling technique  Inserting first N item is (worst case O(n) )  One array access per push therefore N array accesses for N items ~ O(n)  K array accesses to double the size (2+4+8+…+N) ~ O(n)  Ignoring the cost to create new array! CS2336: Computer Science II

Stack: resizing-array implementation How to shrink array? First Try:  Push : double size of the array when it is full  Pop : halve size of the array when array is one-half full  TOO Expensive in worst case  Consider push-pop-push-pop.. Sequence when array if full. Efficient solution:  Push : double size of the array when it is full  Pop : halve size of the array when array is one-quarter full (between 25% and 100% full)  We don’t call resize more often CS2336: Computer Science II

Stack: Resizing-array vs. linked list Tradeoffs: Which one is better?  Linked List Implementation  Every operation takes constant time in worst case  Uses extra time and space to deal with the links  Slower  Resizing-array Implementation  Every operation takes constant time in average.  Less space  Faster In internet switch that ratio of packets/sec is high you don’t want to have some operations that they get suddenly very slow! So you can use linked list If you only care about the total amount of time then you can use resizing- array CS2336: Computer Science II

Queue  Queues are complex than Stacks  We show them as a linkedlist first and then array of String[] and then resizing-array  We monitor:  The size of queue  The front  It is first in first out  The end (rear)  The number of items CS2336: Computer Science II

Queue: array implementation  An “insert” method  Check if queue is not full  If “true” we add that input to the end of the queue  Increment the end of the queue pointer  Increment the size of the queue  Increment the number of items  A “remove” method  Check if the queue is not empty  If “true” we remove an item from the front of the queue  Increment the front pointer  Decrement number of items  A “peek” method to see what is at the front of queue, but not remove it CS2336: Computer Science II

Queue example 10 rear front CS2336: Computer Science II Insert(10) 1510 rearfront Insert(15) rearfront Insert(11) 1115 rearfront remove() Peek() 11 rear front remove()

Queue: resizing-array implementation  Update front and rear modulo the capacity  Add resizing array the same as stack  Leave that as an exercise CS2336: Computer Science II

Priority Queue  add items in order from high to low  A “priorityInsert” method  If the input is the first item just insert it in the queue  Call the “insert” method  If the new input is greater that the one that it is inside the array the we have to shift them (see example) CS2336: Computer Science II

Priority queue example 10 rear front CS2336: Computer Science II priorityInsert(10) 1015 rearfront priorityInsert(15) rearfront priorityInsert(11) priorityInsert(19) rearfront remove() 1011 rearfront remove() Peek()