Stacks and Queues In this section of notes you will learn about two additional data structures as well as the consequences of different implementations.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
CHAPTER 7 Queues.
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.
Data Structure Dr. Mohamed Khafagy.
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
James Tam Data structures in Java Data Structures In Java In this section of notes you will learn about two common types of data structures: Queues Stacks.
The Heap ADT In this section of notes you will learn about a new abstract data type, the heap, as well how heaps can be used.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
Lists Lists as an abstract data type (ADT)
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.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Stack Queue.
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
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.”
CSE 373 Data Structures and Algorithms Lecture 2: 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.
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
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 Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Linked Data Structures
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Stack and Queue APURBO DATTA.
Queue, Deque, and Priority Queue Implementations
Queues.
Queues: Implemented using Arrays
ADT list.
Stacks: Implemented using Linked Lists
Stacks and Queues 1.
Stacks CS-240 Dick Steflik.
Lecture 16 Stacks and Queues CSE /26/2018.
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Data Structures & Programming
Presentation transcript:

Stacks and Queues In this section of notes you will learn about two additional data structures as well as the consequences of different implementations

Stacks A list where additions and deletions are made at only one end of the list. The last element to be added is the first element to be removed (LIFO). Top of stack

Common Stack Operations Push Pop Peek Check if empty Clear

Push Operation Adding an item to the top of the stack 10 Top of stack 5 2 4 5

Push Operation “7” has been added to the stack and this new item becomes the top of the stack. Before push After push 7 Top of stack 10 Top of stack 10 5 5 2 2 4 4 5 5

Pop Operation Removing an item from the top of the stack 10 Top of stack 5 2 4 5

Pop Operation “10” has been removed and “5” becomes the new top of the stack. Before pop After pop 10 Top of stack 5 5 Top of stack 2 2 4 4 5 5

Peek Operation Examine the item at the top of the stack without removing it 10 Top of stack 5 2 4 5

Implementing A Stack As An Array: Class Driver The full example can be found in the directory: /home/331/tamj/examples/stacksQueues/arrayStack class Driver { public static void main (String [] args) MyStack tamjStack = new MyStack (4); System.out.println(ms.peek()); tamjStack.push(4); tamjStack.push(3); tamjStack.push(2); tamjStack.push(1); tamjStack.push(0); while (tamjStack.isEmpty() == false) System.out.println(tamjStack.pop());

Implementing A Stack As An Array: Class MyStack public class MyStack { private int [] stack; private int topOfStack; private static final int DEFAULT_MAX_SIZE = 100; public MyStack () stack = new int[DEFAULT_MAX_SIZE]; topOfStack = -1; } public MyStack (int maxSize) stack = new int[maxSize];

Implementing A Stack As An Array: Class MyStack (2) public boolean isFull () { if (topOfStack >= (stack.length-1)) return true; else return false; } public boolean isEmpty () if (topOfStack < 0)

Implementing A Stack As An Array: Class MyStack (3) public void push (int newValue) { if (isFull() == true) System.out.println("Stack is full, unable to add element"); return; } topOfStack++; stack[topOfStack] = newValue;

Implementing A Stack As An Array: Class MyStack (4) public int peek () { if (isEmpty() == false) return stack[topOfStack]; else return -1; }

Implementing A Stack As An Array: Class MyStack (5) public int pop () { int top; if (isEmpty() == false) top = stack[topOfStack]; stack[topOfStack] = -1; topOfStack--; } else top = -1; return top;

Implementing A Stack As An Array: Class MyStack (6) public void clear () { while (topOfStack >= 0) stack[topOfStack] = -1; topOfStack--; }

Implementing A Stack As A Linked List: Class Driver The full example can be found in the directory: /home/331/tamj/examples/stacksQueues/linkedStack class Driver { public static void main (String [] args) int temp; MyStack tamjStack = new MyStack (); System.out.println(tamjStack.peek()); tamjStack.push(); while (tamjStack.isEmpty() == false) System.out.println(tamjStack.pop()); }

Implementing A Stack As A Linked List: Class MyStack public class MyStack { private Node topNode; private int currentDataValue = 10; public MyStack () topNode = null; } public boolean isEmpty () if (topNode == null) return true; else return false;

Implementing A Stack As A Linked List: Class MyStack (2) public void push () { Node temp = new Node (currentDataValue, topNode); currentDataValue += 10; topNode = temp; } public int peek () Node top; if (isEmpty() == false) top = topNode; return top.data; return -1;

Implementing A Stack As A Linked List: Class MyStack (3) public int pop () { Node top; if (isEmpty() == false) top = topNode; topNode = topNode.next; return top.data; } return -1;

Implementing A Stack As A Linked List: Class MyStack (4) public void clear () { while (topNode != null) topNode = topNode.next; }

Implementing A Stack As A Linked List: (Inner) Class Node private class Node { private int data; private Node next; private Node () data = 0; next = null; } private Node (int startingData) data = startingData;

Implementing A Stack As A Linked List: (Inner) Class Node (2) private Node (Node startingNext) { data = 0; next = startingNext; } private Node (int startingData, Node nextNode) data = startingData; next = nextNode; public String toString () String s = new String (); s = s + data; return s;

A Real Life Application Of Stacks Web navigation using the “back” button. Question: You start out at my CPSC home page and then you go to my 233 course page. Next you press back to go to my home page and then follow the link to my 331 course page. How many clicks of the back button are needed to get back to my 233 course page?

Start At My CPSC Home Page null TOS

Click On The Link To My 233 Course Page null Home TOS

Click “Back”: Return To My Home Pages null TOS

Click On The Link To My 331 Course Page null Home TOS

Queues A list where additions occur only at one end of the list and deletions occur only at the other end. The first element that was added to the queue is the first element to be removed (FIFO). Front: Exit queue Back: Enter queue

Common Operations On Queues Enqueue (add) Dequeue (remove) Peek Check if empty Clear

Array Implementation Of A Queue 8 5 3 f b 8 5 3 1 f b 8 5 3 1 64 f b 5 3 1 64 f b 5 3 1 64 13 f b

The Array Implementation Is “Circular” 1 2 3 4 5 6 7 f b [0] [1] [2] [3] [4] [5] [6] [7] 1 2 3 4 f b b 1 2 3 4 5 6 7 f [0] [1] [2] [3] [4] [5] [6] [7] 5 1 2 3 4 b f

Array Implementation Of A Queue The challenges: Detecting when the queue is empty. Detecting when the queue is full.

First Approach: Queue Is When “Back” Refers To The Last Element Queue full when back = last element (length - 1) 8 5 3 1 7 2 f b

Problem With The First Approach Queue is full? 5 3 1 7 f b 3 1 7 f b 3 1 7 f b 3 1 7 f b

Second Approach: Employ A Counter Enqueue operation: increment the counter Dequeue operation: decrement the counter When the counter equals the zero the queue is empty When the counter equals the length of the list the queue is full

Second Approach: Employ A Counter f b 11 Counter = 1 f b 11 17 Counter = 2 f b 17 Counter = 1 f b

General Problem With Circular Queues Confusing the “queue empty” and “queue full” conditions. 100 67 f b 67 f b f b Aha! When “f” is just ahead of “b” then the queue is empty.

General Problem With Circular Queues Confusing the “queue empty” and “queue full” conditions. 67 17 99 85 b f 67 17 11 99 85 b f Is the queue empty or full???

Third Approach: Reserve An Array Element The index of the unused element lies between the front and the back indices (one greater than the back index and one less than the front index). The full condition occurs when all elements but one are occupied or when: The front index is two elements ahead of the back index or The back index is two ahead of the front index (front is two behind the back) 67 17 99 85 b f 67 17 1 85 f b

Third Approach: Reserve An Array Element General formula to detect when the queue is full: Front index = (back index + 2) % queue.length

Third Approach: Reserve An Array Element The empty condition occurs when all array elements are unoccupied or when: The front index is one element “ahead” of the back index. 1 f b b f OR 1 f b f b

Third Approach: Reserve An Array Element General formula to detect when the queue is empty: Front index = (back index + 1) % queue.length

Implementing A Stack As An Array: Class Driver The full example can be found in the directory: /home/331/tamj/examples/stacksQueues/arrayQueue class Driver { public static void main (String [] args) MyQueue tamjQueue = new MyQueue (4); System.out.println(tamjQueue.peek()); tamjQueue.enqueue(4); tamjQueue.enqueue(3); tamjQueue.enqueue(2); tamjQueue.dequeue();

Implementing A Stack As An Array: Class Driver (2) tamjQueue.enqueue(4); while (tamjQueue.isEmpty() == false) { System.out.println(tamjQueue.dequeue()); }

Implementing A Stack As An Array: Class MyQueue public class MyQueue { private int [] queue; private int front; private int back; private static final int DEFAULT_MAX_SIZE = 100; public MyQueue () queue = new int [DEFAULT_MAX_SIZE]; front = 0; back = queue.length - 1; }

Implementing A Stack As An Array: Class MyQueue (2) public MyQueue (int maxSize) { queue = new int[maxSize]; front = 0; back = queue.length - 1; } public boolean isFull () if (front == (back+2) % queue.length) return true; else return false;

Implementing A Stack As An Array: Class MyQueue (3) public boolean isEmpty () { if (front == ((back + 1) % queue.length)) return true; else return false; }

Implementing A Stack As An Array: Class MyQueue (4) public void enqueue (int newValue) { if (isFull() == true) System.out.println("Queue is full, unable to add element"); return; } back = (back + 1) % queue.length; queue[back] = newValue;

Implementing A Stack As An Array: Class MyQueue (5) public int dequeue () { int first; if (isEmpty() == false) first = queue[front]; queue[front] = -1; front = (front + 1) % queue.length; } else first = -1; return first;

Implementing A Stack As An Array: Class MyQueue (6) public int peek () { if (isEmpty() == false) return queue[front]; else return -1; } public void clear () while (isEmpty() == false) queue[front] = -1; front = (front + 1) % queue.length;

Implementing A Stack As An Array: Class MyQueue (7) front = 0; back = queue.length; }

Implementing A Queue As A Linked List: Class Driver The full example can be found in the directory: /home/331/tamj/examples/stacksQueues/linkedQueue class Driver { public static void main (String [] args) int temp; MyQueue tamjQueue = new MyQueue (); System.out.println(tamjQueue.peek()); tamjQueue.enqueue();

Implementing A Queue As A Linked List: Class Driver (2) tamjQueue.enqueue(); tamjQueue.dequeue(); System.out.println(tamjQueue.peek()); tamjQueue.clear(); }

Implementing A Queue As A Linked List: Class MyQueue public class MyQueue { private Node front; private Node back; private int currentDataValue = 10; public MyQueue () front = null; back = null; }

Implementing A Queue As A Linked List: Class MyQueue (2) public boolean isEmpty () { if (front == null) return true; else return false; }

Implementing A Queue As A Linked List: Class MyQueue (3) public void enqueue () { Node temp = new Node (currentDataValue); if (isEmpty() == false) back.next = temp; else front = temp; back = temp; currentDataValue += 10; }

Implementing A Queue As A Linked List: Class MyQueue (5) public int peek () { Node temp; if (isEmpty() == false) temp = front; return temp.data; } return -1;

Implementing A Queue As A Linked List: Class MyQueue (6) public int dequeue () { Node temp; if (isEmpty() == false) temp = front; front = front.next; return temp.data; } return -1;

Implementing A Queue As A Linked List: Class MyQueue (7) public void clear () { front = null; back = null; } // Plus class Node must also be defined as an inner class of class MyQueue.

A Real Life Application Of Queues Printing on a printer

You Should Now Know What is a stack? What is a queue? What are the implications of array vs. a linked list implementations?

Sources Of Lecture Material Data Structures and Abstractions with Java by Frank M. Carrano and Walter Savitch Data Abstraction and Problem Solving with Java by Frank M. Carrano and Janet J. Prichard CPSC 331 course notes by Marina L. Gavrilova http://pages.cpsc.ucalgary.ca/~marina/331/