1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.

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

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.
1 Queues (Continued) Applications for a queue Linked queue implementation Array queue implementation Circular array queue implementation Reading L&C 3.
Topic 9 The Queue ADT.
CHAPTER 7 Queues.
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Data Structures II Review COSC 2006 April 14, 2017
COMP 103 Linked Stack and Linked Queue.
1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3.
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.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2,
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
TCSS 342, Winter 2005 Lecture Notes
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
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 3 Introduction to Collections – Stacks Modified
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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’
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.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Linked Structures, LinkedStack
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
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 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Click to edit Master text styles Stacks Data Structure.
Java Collections Framework The client view. The Big Picture.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Stack: a Linked Implementation
The List ADT.
QueueStack CS1020.
Stacks.
Queues Queue Concept Queue Design Considerations
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Building Java Programs
structures and their relationships." - Linus Torvalds
THURSDAY, OCTOBER 17 IN LAB
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Java Collections Framework
Example: LinkedSet<T>
More Data Structures (Part 1)
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Presentation transcript:

1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT Interface Queue Design Considerations The java.util.Queue interface Reading L&C 3 rd : 4.4, 4.6, nd :

2 Array Stack Implementation We can use an array of elements as a stack The top is the index of the next available element in the array top Object of type T null T [ ] stack

3 Array Stack Implementation push – O(n) public void push (T element) { if (size() == stack.length) expandCapacity(); stack [top++] = element; } worst case: O(n) How about the average case: almost constant time.

4 Array Stack Implementation pop() – O(1) public T pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); T result = stack[--top]; stack[top] = null; // removes “stale” reference return result; } The “stale” reference stored in stack[top] would prevent garbage collection on the object when the caller sets the returned reference value to null – ties up resources

5 Linked Stack Implementation We can use the same LinearNode class that we used for LinkedSet implementation We change the attribute name to “top” to have a meaning consistent with a stack Object of type T topLinearNode next; T element; Object of type T LinearNode next; T element; null count Object of type T

6 Linked Stack Implementation push – O(1) public void push (T element) { LinearNode temp = new LinearNode (element); temp.setNext(top); top = temp; count++; } Note the difference between the stack push method and the LinkedSet add method –There is no check for already present or not

7 Linked Stack Implementation pop – O(1) public T pop () throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); T result = top.getElement(); top = top.getNext(); count--; return result; } Note the difference between the stack pop method and the LinkedSet remove method –There is no specified target as a parameter

8 The java.util.Stack Class The java.util.Stack class is part of the Java collections API. It is a subclass of the java.util.Vector class which is a subclass of java.util.AbstractList java.util.Stack inherits some methods from its superclasses that are inappropriate for a stack collection – a bad design decision! A good programmer will avoid using those methods on a Stack object

9 The java.util.Stack Class java.util.Stack has a search method that returns the distance from the top of the stack to the first occurrence of that object on the stack, e.g. search returns 1 for the top element of the stack If the object is not found, search returns -1 The search method is O(n) Does this method violate the principles of a stack as a data structure?

10 Queue Abstract Data Type A queue is a linear collection where the elements are added to one end and removed from the other end The processing is first in, first out (FIFO) The first element put on the queue is the first element removed from the queue Think of a line of people waiting to be served at RMV.

11 A Conceptual View of a Queue Rear of Queue (or Tail) Adding an Element Removing an Element Front of Queue (or Head)

12 Queue Terminology We enqueue an element on a queue to add one We dequeue an element off a queue to remove one We can also examine the first element without removing it We can determine if a queue is empty or not and how many elements it contains (its size) The L&C QueueADT interface supports the above operations and some typical class operations such as toString()

13 Queue ADT Interface > QueueADT + enqueue(element : T) : void + dequeue () : T + first() : T + isEmpty () : bool + size() : int + toString() : String

14 Queue Design Considerations Although a queue can be empty, there is no concept for it being full. An implementation must be designed to manage storage space For first and dequeue operation on an empty queue, this implementation will throw an exception Other implementations could return a value null that is equivalent to “nothing to return”

15 Queue Design Considerations No iterator method is provided That would be inconsistent with restricting access to the first element of the queue If we need an iterator or other mechanism to access the elements in the middle or at the end of the collection, then a queue is not the appropriate data structure to use

16 The java.util.Queue Interface The java.util.Queue interface is in the Java Collections API (extends Collection) However, it is only an interface and you must use an implementing class LinkedList is the most commonly used implementing class For a queue of type objects: Queue myQueue = new LinkedList ();

17 The java.util.Queue Interface The names of the methods are different Enqueue is done using: boolean offer(T element) // returns false if full Dequeue is done using either: T poll() // returns null value if empty T remove() // throws an exception if empty Peek is done using either: T peek() // returns null value if empty T element() // throws an exception if empty