QueueStack CS1020.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
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.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queues.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
TCSS 342, Winter 2005 Lecture Notes
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Data Structures - Queues
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
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,
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’
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013Amihai Savir & Ilya Mirsky
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CS2852 Week 5, Class 2 Today Queue Applications Circular Queue Implementation Testing SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Linked Data Structures
Building Java Programs
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.
Stacks and Queues.
Week 4 - Friday CS221.
Heaps And Priority Queues
Stack and Queue APURBO DATTA.
Stacks and Queues.
Building Java Programs
Topic 16 Queues "FISH queue: n.
Topic 16 Queues Adapted from Mike Scott’s materials.
Queues A queue is a data structure that is similar in spirit to a fair line. As you can see in this photo, the first dog in line can expect to be the first.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Lecture 21 Stacks and Queues Richard Gesick.
Topic 16 Queues "FISH queue: n.
Stacks and Queues.
Building Java Programs
Building Java Programs
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks and Queues CLRS, Section 10.1.
slides created by Alyssa Harding
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.
More Data Structures (Part 1)
Lecture 16 Stacks and Queues CSE /26/2018.
slides created by Alyssa Harding
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks and Queues.
Lecture 9: Stack and Queue
Data Structures & Programming
Presentation transcript:

QueueStack CS1020

Manage a “big queue of stacks”: Create a stack with number X. Insert an integer Y to a stack with number X. Note that you must simulate a Queue, and therefore, can only access the first stack in the big queue. Merge the first two stacks in the big queue. Print the integer at the top of the stack in the front of the big queue. Problem Description 2

FIFO = First-In First-Out Queues are “one-way”. Only insert from the back, can only take the head. (FIFO) FIFO = First-In First-Out Big Queue of Stacks 1 4 5 3 22 3 8 9 5 17 100 1 4 5 3 7 Dequeue Enqueue Elaborate on how Queue is an interface and using LinkedList is one way of using that interface since LinkedList in java implements the Queue Interface. Head Tail Queue<E> = new LinkedList<E>(); http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html

<<interface>> Queue<E> //see Java API + add(E) : boolean + peek() : E + poll() : E … Java Queue Interface

<<interface>> Queue<E> //see Java API add(E) + add(E) : boolean + peek() : E + poll() : E … Queue Java Queue Interface

<<interface>> Queue<E> //see Java API poll() + add(E) : boolean + peek() : E + poll() : E … Queue Java Queue Interface

Stack Class for this problem Class Stack<E> Stack<E> index: int elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean Implement your own stack… Stack Class for this problem

Stack Class for this problem Class Stack<E> Stack<E> index: int elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean 1 3 5 Implement your own stack… 4 1 Stack Class for this problem

Stack Class for this problem push(10) Class Stack<E> Stack<E> index: int elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean 1 3 5 4 1 Stack Class for this problem

Stack Class for this problem peek() Class Stack<E> Returns 10 Stack<E> index: int elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean 1 3 5 Elaborate on how peek returns 10 but the top of the stack stays inside the stack. 4 1 Stack Class for this problem

Stack Class for this problem pop() Class Stack<E> Returns 10 Stack<E> index: int elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean 1 3 5 Elaborate on how pop returns 10 and remove the top element 4 1 Stack Class for this problem

Stack Class for this problem Class Stack<E> public int getSize() { return elements.size(); } public int getIndex() { return this.index; public boolean isEmpty() { return this.getSize() == 0; Stack<E> index: int elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean Stack Class for this problem

QueueStack Class QueueStack QueueStack bigQueue : LinkedList<Stack<Integer>> // TODO… :) QueueStack

QueueStack public class QueueStack { private Queue<Stack<Integer>> bigQueue; public QueueStack() { this.bigQueue = new LinkedList<Stack<Integer>>(); } //TODO :) class Stack<E> { QueueStack

Big Queue of Stacks Dequeue Enqueue X Head Tail Query 1 : CREATE x 15

Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary M Tail Dequeue Enqueue M 4 5 3 1 N 3 1 X 5 Q 8 20 17 Head Tail Temporary M Tail Head Query 2 : INSERT y x 16

Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary N M Tail Dequeue Enqueue N 3 1 X 5 Q 8 20 17 Head Tail Temporary N M Tail Head Query 2 : INSERT y x 17

Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary N M Tail Dequeue Enqueue X 5 Q 8 20 17 Head Tail Temporary N M Tail Head Query 2 : INSERT y x 18

Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary N M Tail Dequeue Enqueue X 5 Q 8 20 17 Y Head Tail Temporary N M Tail Head Query 2 : INSERT y x 19

Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary X N M Dequeue Enqueue X 5 Y Q 8 20 17 Head Tail Temporary X N M Tail Head Query 2 : INSERT y x 20

Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary Q X N M Dequeue Enqueue Q 8 20 17 Head Tail Temporary Q X N M Tail Head Query 2 : INSERT y x 21

Query 2 : INSERT y x Big Queue of Stacks Head Tail Temporary Q X N M Dequeue Enqueue M 4 5 3 1 N 3 1 X 5 Y Q 8 20 17 Head Tail Temporary Q X N M Tail Head Query 2 : INSERT y x 22

private void insert(int toBeInserted, int stackIndex) { Queue<Stack<Integer>> temp = new LinkedList<Stack<Integer>>(); int queueSize = bigQueue.size(); for (int i = 0; i < queueSize; i++) { } bigQueue = temp; Search for stack with index stackIndex and push the element. As you search, enqueue all stacks to a temporary queue and continue until all stacks are in the temporary queue. Query 2 : INSERT y x 23

Query 3 : MERGE Big Queue of Stacks Tail Head 3 2 1 9 Dequeue Enqueue 4 5 2 8 20 17 1 5 9 9 8 Head Tail Query 3 : MERGE 24

Query 3 : MERGE Big Queue of Stacks Tail Head 3 2 1 9 Dequeue Enqueue 8 20 17 1 5 9 9 8 4 3 5 5 3 4 Head Tail Query 3 : MERGE 25

Query 3 : MERGE Big Queue of Stacks Tail Head 2 1 9 Dequeue Enqueue 8 20 17 1 5 9 9 8 4 5 3 Head Tail private void merge() { Stack<Integer> firstStack = //poll or peek? Why? Stack<Integer> secondStack = //poll or peek? Why? int size = firstStack.getSize(); for (int k = 0; k < size; k++) { //TODO :) } Query 3 : MERGE 26

Query 4 : PRINT Big Queue of Stacks Tail Head 2 1 9 Dequeue Enqueue 27 8 3 5 4 20 17 1 5 9 9 8 Head Tail Query 4 : PRINT 27

Query 4 : PRINT Big Queue of Stacks Tail Head 2 1 9 Dequeue Enqueue 8 3 5 4 20 17 1 5 9 9 8 Head Tail private void print() { Stack<Integer> head = bigQueue.peek(); //why peek instead of poll? //TODO :) } Query 4 : PRINT 28