CSE 373 Data Structures Lecture 6

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Stacks, Queues, and Linked Lists
CS201: Data Structures and Discrete Mathematics I Linked Lists, Stacks and Queues.
Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
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.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
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.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
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.
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Lecture 7 Queues Stacks Trees.
COSC160: Data Structures: Lists and Queues
Chapter 15 Lists Objectives
CS 1114: Implementing Search
CSE 373: Data Structures and Algorithms
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Queues.
Stacks and Queues.
Stacks, Queues, and Deques
Lesson Objectives Aims
אחסון (אירגון) מידע DATA DATA DATA Link Link Link … …
CSC 143 Queues [Chapter 7].
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues CLRS, Section 10.1.
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.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Data Structures – Week #4
Stacks and Queues.
Lecture 9: Stack and Queue
Presentation transcript:

CSE 373 Data Structures Lecture 6 Stacks and Queues CSE 373 Data Structures Lecture 6

Stacks and Queues - Lecture 6 Readings Reading Sections 3.3 and 3.4 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Stack ADT A list for which Insert and Delete are allowed only at one end of the list (the top) the implementation defines which end is the "top" LIFO – Last in, First out Push: Insert element at top Pop: Remove and return top element (aka TopAndPop) IsEmpty: test for emptyness a tray stack 12/26/03 Stacks and Queues - Lecture 6

An Important Application of Stacks Parsing phase in compilers yields the reverse Polish (postfix) notation: ab+c*d+ (traversal of a binary tree in postorder; see Lecture 7) parse tree d + (a+b)*c+d c * a + b 12/26/03 Stacks and Queues - Lecture 6

Another Important Application of Stacks Call stack in run time systems When a function (method, procedure) is called the work area (local variables, copies of parameters, return location in code) for the new function is pushed on to the stack. When the function returns the stack is popped. So, calling a recursive procedure with a depth of N requires O(N) space. 12/26/03 Stacks and Queues - Lecture 6

Two Basic Implementations of Stacks Linked List Push is InsertFront Pop is DeleteFront (Top is “access” the element at the top of the stack) IsEmpty is test for null Array The k items in the stack are the first k items in the array. 12/26/03 Stacks and Queues - Lecture 6

Linked List Implementation Stack of blobs null node a blob Pointer to next node Pointer to blob 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Array Implementation Stack of blobs A bottom top 1 2 3 4 5 6 7 8 9 10 11 12 4 12 holder = blob pointer array size = number in stack maxsize = max size of stack 12/26/03 Stacks and Queues - Lecture 6

Push and Pop (array impl.) IsEmpty(A : blobstack pointer) : boolean { return A.size = 0 } IsFull(A : blobstack pointer) : boolean { return A.size = A.maxsize; Pop(A : blobstack pointer) : blob pointer { // Precondition: A is not empty // A.size := A.size – 1; return A.holder[A.size + 1]; Push(A : blobstack pointer, p : blob pointer): { // precondition: A is not full// A.size := A.size + 1; A.holder[A.size] := p; 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Linked Lists vs Array Linked list implementation + flexible – size of stack can be anything + constant time per operation - Call to memory allocator can be costly Array Implementation + Memory preallocated + constant time per operation. - Not all allocated memory is used Overflow possible - Resizing can be used but some ops will be more than constant time. 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Insert at one end of List, remove at the other end Queues are “FIFO” – first in, first out Primary operations are Enqueue and Dequeue A queue ensures “fairness” customers waiting on a customer hotline processes waiting to run on the CPU 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Queue ADT Operations: Enqueue - add an entry at the end of the queue (also called “rear” or “tail”) Dequeue - remove the entry from the front of the queue IsEmpty IsFull may be needed 12/26/03 Stacks and Queues - Lecture 6

A Sample of Applications of Queues File servers: Users needing access to their files on a shared file server machine are given access on a FIFO basis Printer Queue: Jobs submitted to a printer are printed in order of arrival Phone calls made to customer service hotlines are usually placed in a queue 12/26/03 Stacks and Queues - Lecture 6

Pointer Implementation Q front rear null Header Not always there front rear 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 List Implementation IsEmpty(Q : blobqueue pointer) : boolean { return Q.front = Q.rear } Dequeue(Q : blobqueue pointer) : blob pointer { // Precondition: Q is not empty // B : blob pointer; B := Q.front.next; Q.front.next := Q.front.next.next; return B; Enqueue(Q : blobqueue pointer, p : blob pointer): { Q.rear.next := new node; Q.rear := Q.rear.next; Q.rear.value := p; 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Array Implementation Circular array front rear Q 0 1 2 3 4 5 6 7 8 9 10 11 4 2 12 holder = blob pointer array size = number in queue front = index of front of queue maxsize = max size of queue rear = (front + size) mod maxsize 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Wrap Around front Q rear 0 1 2 3 4 5 6 7 8 9 10 11 4 10 12 rear = (front + size) mod maxsize = (10 + 4) mod 12 = 14 mod 12 = 2 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Enqueue front Q rear 0 1 2 3 4 5 6 7 8 9 10 11 4 10 12 p 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Enqueue front Q rear 0 1 2 3 4 5 6 7 8 9 10 11 5 10 12 p 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Enqueue Enqueue(Q : blobqueue pointer, p : blob pointer) : { // precondition : queue is not full // Q.holder[(Q.front + Q.size) mod Q.maxsize] := p; Q.size := Q.size + 1; } Constant time! 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Dequeue front Q rear 0 1 2 3 4 5 6 7 8 9 10 11 4 10 12 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Dequeue Q rear front 0 1 2 3 4 5 6 7 8 9 10 11 3 11 12 return 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Try Dequeue Define the circular array implementation of Dequeue 12/26/03 Stacks and Queues - Lecture 6

Stacks and Queues - Lecture 6 Solution to Dequeue Dequeue(Q : blobqueue pointer) : blob pointer { // precondition : queue is not empty // p : blob pointer p := Q.holder[Q.front]; Q.front := (Q.front + 1) mod Q.maxsize; Q.size := Q.size - 1; return p; } 12/26/03 Stacks and Queues - Lecture 6