Stack and Queue.

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
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§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.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
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.
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.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
 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
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues.
Stacks, Queues & Deques CSC212.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Data Structures from Cormen, Leiserson, Rivest & Stein.
Implementing and Using Stacks
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
Stacks, Queues, and Deques
Objectives of these slides:
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ICOM 4035 – Data Structures Lecture 9 – Stack ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And 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’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Stacks And Queues Chapter 18.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
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,
Stacks and Queues MR. Mohammad Alqahtani.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Data Structures Using C, 2e
Queues.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Stacks, Queues, and Deques
ADT list.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Stacks and Queues CSE 373 Data Structures.
Stacks, Queues, and Deques
Presentation transcript:

Stack and Queue

Stack From Two Queues Describe how to implement the stack ADT using two queues The solution should only use the standard queue operations: ENQUEUE, DEQUEUE, IS-EMPTY What will be the running times of the PUSH and POP operations?

Stack From Two Queues – Solution We will call the two queues Q1 and Q2 Additionally, we will hold a variable called curQueue, which points to the queue that holds the last value pushed to our stack Initialization: curQueue  Q1

Stack From Two Queues – Solution (continued) IS-EMPTY return IS-EMPTY (curQueue) PUSH (val) ENQUEUE (curQueue, val) TOP val  POP return val

Stack From Two Queues – Solution (continued) POP otherQueue  (curQueue = Q1)? Q2: Q1 while (!IS-EMPTY (curQueue)) temp  DEQUEUE (curQueue) if (!IS-EMPTY (curQueue)) // Not last? ENQUEUE (otherQueue, temp) curQueue  otherQueue return temp

Stack From Two Queues – Solution Complexity Since PUSH is simply implemented as a single ENQUEUE operation, its running time is O(1) For POP operations, we always need to transfer the complete stack contents from one queue to the other, therefore performing a POP takes O(n) time

Two Stacks in a Single Array Describe how to implement two stacks inside a single array The total number of elements in both stacks is limited by the array length Therefore, just splitting the array into two equally-sized parts will not do All stack operations should run in O(1)

Two Stacks in a Single Array – Solution Concept We are given an array A with n elements, numbered 0 to n – 1 We define two markers t1 and t2 that point to the next free cell in stacks S1 and S2 respectively Initialization: t1 = 0 t2 = n – 1

Two Stacks in a Single Array – Solution Concept S1 grows up from the beginning of the array, S2 grows down from its end We know that the array is full when the two markers switch places (t1 > t2) The complexity is O(1) for all operations The array usage is optimal

Two Stacks in a Single Array – Solution Visualization 7 3 2 9 5 6 t1 t2

Two Stacks in a Single Array – Solution Details S1.IS-EMPTY if (t1 = 0) return true else return false S2.IS-EMPTY if (t2 = n - 1)

Two Stacks in a Single Array – Solution Details (continued) S1.PUSH (val) if (t1 > t2) error "stack overflow" A[t1] = val t1++ S2.PUSH (val) A[t2] = val t2--

Two Stacks in a Single Array – Solution Details (continued) S1.POP if (t1 = 0) error "stack underflow" t1-- return A[t1] S2.POP if (t2 = n - 1) t2++ return A[t2]

Two Stacks in a Single Array – Solution Details (continued) S1.TOP if (t1 = 0) error "stack is empty" return A[t1] S2.TOP if (t2 = n - 1) return A[t2]

Evaluating Arithmetic Expressions Fully parenthesized arithmetic expressions can be evaluated using Dijkstra's two-stack algorithm Uses two separate stacks – one for the operators and another for the operands See Java code in: Evaluate.java.html

Deque (or Double-Ended Queue) A deque is a generalization of the Queue ADT, that supports reading and writing from both ends of the queue We would like to support an input-restricted deque : Deletion can be made from both ends Input can only be made at one end

Deque We note that an input-restricted deque is a combination of a queue and a stack We use a standard queue, and modify it to also support PUSH and POP The tail (or rear) of the queue will also be used as the stack top

Deque Implementing PUSH is now trivial – simply call ENQUEUE POP is very similar to DEQUEUE, accept that it returns tail instead of head The same IS-EMPTY function will work for both the stack and the queue Check if head = tail

Deque ENQUEUE PUSH POP DEQUEUE 9 5 6 2 3 head (front) tail (rear)