COSC160: Data Structures: Lists and Queues

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Data Structure HKOI training /4/2010 So Pak Yeung.
Stack & Queues COP 3502.
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.
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.
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.”
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
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.”
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
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.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Linear Data Structures
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.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
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,
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.
Review Array Array Elements Accessing array elements
COSC160: Data Structures Linked Lists
Data Structures Using C, 2e
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
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.
Chapter 12 – Data Structures
Lecture 7 Queues Stacks Trees.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
September 29 – Stacks and queues
Program based on queue & their operations for an application
12 C Data Structures.
Chapter 15 Lists Objectives
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks, Queues, and Deques
DATA STRUCTURE QUEUE.
ITEC 2620M Introduction to Data Structures
Tonga Institute of Higher Education
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 Prof. Michael Tsai 2017/02/21.
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues.
Lecture 2: Stacks and Queues
Stacks and Queues CSE 373 Data Structures.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Lecture 9: Stack and Queue
Chapter 4 Stacks and Queues
Data Structures & Programming
Presentation transcript:

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

Outline Queues FIFO Queues LIFO Queues (Stacks) Usage Implementations LIFO Queues (Stacks) Applications / Examples

Queues Queue is a linear list where data is added and removed at the “ends” Refers to conceptual behavior (not implementation) Specifically when (where) data enters the queue, and when (where) data exits the queue Access is generally limited to the top or the bottom of the queue. Types of queues are differentiated by operations performed (behaviors) Common Queues First In First Out (FIFO) Applications: traversals, memory buffers, resource sharing, simulations, … Last In First Out (LIFO) Stack Applications: parsers, Runtime environments, traversals, …

FIFO Queue First In First Out Operations Implementation Details Describes how data is added and removed First come first serve Examples where used: Check-out line at store When entities use a shared resource Memory Buffer Operations Insert (item) // inserts item to back of queue Remove () // removes item at front of queue Implementation Details Array or chain structure Time Complexity implications

FIFO Queue: Add and Remove Enqueue 4 (add to back) Enqueue 5 (add to back) Dequeue (remove from front) Enqueue 2 (add to back) Dequeue (remove from front)

FIFO Queue: Array Implementation Considerations Front of Queue Should front of list always be the front of the array Requires shifting after every dequeue Circular, dynamic array is efficient Keep index to front (and back) Observations: Since no internal elements in the queue need to be accessed. Both insert (enqueue) and remove (dequeue) operations are efficient. Time Complexity?

FIFO Queue: Linked List Implementation Memory Requirements? Time? Enqueue Dequeue

LIFO Queue: aka stack Last In First Out Operations Describes how data is added and removed Last to be added is first to be removed Examples where used: Push down plates at cafeteria Parsing / grammar analysis Tree or maze traversals Operations Insert (to top) PUSH( item ) Remove (from top) POP () View top PEEK() Implementation Details Array or chain structure Time Complexity implications

LIFO Queue (Stack): Push and Pop

Stack: Array Implementations Observations Must allocate size initially No need to keep a base index Maintain a top index

Stack: Linked List Implementations Observations Must maintain pointer to top Size changes dynamically

Queue Implementation Summary Both Insert and Remove operations for both Queue types are efficient Chaining Implementation permits size to change so that “extra” space is not necessarily allocated. This comes at the cost of one extra pointer per item stored. Array Implementation does not require an extra pointer per item stored, but may use space unnecessarily and may infrequently, require and O(n) reallocation if the Queue is full and an insert is performed.

Application of FIFO Queue Shared Resource Queue

Applications of FIFO queues Shortest Distance (from a to b) on a grid example FIFO := initialize empty fifo queue //Square a is at grid location (0,0) a.distanceThusFar := 0 FIFO.enqueue(a) while ( ! FIFO.isEmpty() ) x := FIFO.dequeue() for each neighboring square n of square x if square was not previously encountered OR if n.distanceThusFar > x.distanceThusFar + 1 n.distanceThusFar := x.distanceThusFar + 1 if n == b // done FIFO.clear() return n.distanceThusFar FIFO.enqueue(n); and mark square n as encountered a 1 2 3 4 5 * 6 7 b a * b a 1 * b

Application of Stacks Symbol matching (for syntax) EG: Are all the ‘(‘ and ‘{‘ properly matched with ‘)’ and ‘}’ function f ( arg ) { while (arg > 0) {x = arg --;} } Symbol matching algorithm Scan input code (character array) from left to right for i from 0 to inputLength - 1 if input[ i ] == ‘(‘ OR input[ i ] == ‘{‘ , then stack.push(input[ i ]) else if input[ i ] == ‘)‘ OR input[ i ] == ‘}‘ then c : = stack.pop() if c is ‘(‘ and input[ i ] is ‘)’ OR if c is ‘{‘ and input[ i ] is ‘}’ //The symbols are matched!, do nothing else // the symbols are unmatched throw a syntax error if traversal complete and stack.isEmpty() == false, then Throw error, no matching close symbol found