Stacks and Queues CSC220 Data Structure Winter 2004-5.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

CS201: Data Structures and Discrete Mathematics I Linked Lists, Stacks and Queues.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
Data Structures and Algorithms (60-254)
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.
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.
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.”
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
 Balancing Symbols 3. Applications
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Stacks, Queues & Deques CSC212.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
TCSS 342, Winter 2005 Lecture Notes
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.”
Objectives of these slides:
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Stack and Queue.
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.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
Final Exam Review CS Total Points – 60 Points Writing Programs – 50 Points Tracing Algorithms, determining results, and drawing pictures – 50.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
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.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Stack and Queues Part 2. Priority Queues Priority Queues (cont’) A priority queue is a more specialized data structure than a stack or a queue. However,
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.
Algorithms and Data Structures Lecture VI
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Stacks Chapter 3 Objectives Upon completion you will be able to
 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.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
1 Data Organization Example 1: A simple text editor –Store the text buffer as a list of lines. –How would we implement the UNDO operation? Example 2: Parsing.
Queues.
G64ADS Advanced Data Structures
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
COSC160: Data Structures: Lists and Queues
September 29 – Stacks and queues
Chapter 15 Lists Objectives
Objectives In this lesson, you will learn to: Define stacks
Queues Queues Queues.
structures and their relationships." - Linus Torvalds
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.
Stacks and Queues 1.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Stacks and Queues CSE 373 Data Structures.
structures and their relationships." - Linus Torvalds
Presentation transcript:

Stacks and Queues CSC220 Data Structure Winter

Abstract Data Type Abstract Data Type as a design tool Concerns only on the important concept or model No concern on implementation details. Stack & Queue is an example of ADT An array is not ADT.

What is the difference? Stack & Queue vs. Array –Arrays are data storage structures while stacks and queues are specialized DS and used as programmer’s tools. Stack – a container that allows push and pop Queue - a container that allows enqueue and dequeue No concern on implementation details. In an array any item can be accessed, while in these data structures access is restricted. They are more abstract than arrays.

Questions? Array is not ADT Is Linked list ADT? Is Binary-tree ADT? Is Hash table ADT? What about graph?

Stacks 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 mechanism is called Last-In-First-Out (LIFO). A Stack Applet example

Stack operations Placing a data item on the top is called “pushing”, while removing an item from the top is called “popping” it. push and pop are the primary stack operations. Some of the applications are : microprocessors, some older calculators etc.

Example of Stack codes First example stack ADT and implementation C:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms\ReaderFiles\Chap04\Stack\stack.java push and pop operations are performed in O(1) time.

Example of Stack codes Reversed word What is it? ABC -> CBA C:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms\Re aderFiles\Chap04\Reverse\reverse.java

Example of Stack codes BracketChecker (balancer) A syntax checker (compiler) that understands a language containing any strings with balanced brackets ‘{‘ ‘[‘ ‘(‘ and ‘)’, ‘]’, ‘}’ –S -> Bl S1 Br –S1 -> Bl string Br –Bl -> ‘{‘ | ‘[‘ | ‘(‘ –Br -> ‘)’, | ‘]’, | ‘}’ C:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms\ReaderFile s\Chap04\Brackets\brackets.java

Queues Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. This mechanism is called First-In-First-Out (FIFO). Placing an item in a queue is called “insertion or enqueue”, which is done at the end of the queue called “rear”. Removing an item from a queue is called “deletion or dequeue”, which is done at the other end of the queue called “front”. Some of the applications are : printer queue, keystroke queue, etc.

Circular Queue When a new item is inserted at the rear, the pointer to rear moves upwards. Similarly, when an item is deleted from the queue the front arrow moves downwards. After a few insert and delete operations the rear might reach the end of the queue and no more items can be inserted although the items from the front of the queue have been deleted and there is space in the queue.

Circular Queue To solve this problem, queues implement wrapping around. Such queues are called Circular Queues. Both the front and the rear pointers wrap around to the beginning of the array. It is also called as “Ring buffer”. Items can inserted and deleted from a queue in O(1) time.

Queue Example

Queue sample code C:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms \ReaderFiles\Chap04\Queue\queue.javaC:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms \ReaderFiles\Chap04\Queue\queue.java

Various Queues Normal queue (FIFO) Circular Queue (Normal Queue) Double-ended Queue (Deque) Priority Queue

Deque It is a double-ended queue. Items can be inserted and deleted from either ends. More versatile data structure than stack or queue. E.g. policy-based application (e.g. low priority go to the end, high go to the front) In a case where you want to sort the queue once in a while, What sorting algorithm will you use?

Priority Queues More specialized data structure. Similar to Queue, having front and rear. Items are removed from the front. Items are ordered by key value so that the item with the lowest key (or highest) is always at the front. Items are inserted in proper position to maintain the order. Let’s discuss complexity

Priority Queue Example

Priority Queues Used in multitasking operating system. They are generally represented using “heap” data structure. Insertion runs in O(n) time, deletion in O(1) time. C:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms \ReaderFiles\Chap04\PriorityQ\priorityQ.ja vaC:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms \ReaderFiles\Chap04\PriorityQ\priorityQ.ja va

Parsing Arithmetic Expressions * 5 ((2 + 4) * 7) + 3* (9 – 5)) Infix vs postfix Why do we want to do this transformation? * * * +

Infix to postfix Read ch from input until empty –If ch is arg, output = output + arg –If ch is “(“, push ‘(‘; –If ch is op and higher than top push ch –If ch is “)” or end of input, output = output + pop() until empty or top is “(“ –Read next input C:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms\Read erFiles\Chap04\Postfix\postfix.javaC:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms\Read erFiles\Chap04\Postfix\postfix.java

Postfix eval * 3 -> * + Algorithm –While input is not empty –If ch is number, push (ch) –Else Pop (a) Pop(b) Eval (ch, a, b) C:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms\Read erFiles\Chap04\Postfix\postfix.javaC:\Documents and Settings\box\My Documents\CS\CSC\220\ReaderPrograms\Read erFiles\Chap04\Postfix\postfix.java

Quick XML Review XML – Wave of the future

Another Real world example Art Gittleman