COMP 103 Linked Stack and Linked Queue.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 24 Lists, Stacks, and Queues
Linked Lists Linear collections.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
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.
5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size.
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.
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
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.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 Stacks II CS Data Structures I COSC 2006
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
Information and Computer Sciences University of Hawaii, Manoa
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Linked Structures.
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,
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Queue FIFO (First In First Out) Java Doc peek, element offer poll, add
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS 367 Introduction to Data Structures Lecture 5.
Computer Science 209 Software Development Inheritance and Composition.
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.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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.
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.
Stack: a Linked Implementation
CSE 373 Implementing a Stack/Queue as a Linked List
Queue FIFO (First In First Out) Java Doc peek, element poll, remove
QueueStack CS1020.
Week 3 - Friday CS221.
CSE 373: Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
THURSDAY, OCTOBER 17 IN LAB
Java Collections Framework
ADT list.
Chapter 4 Queues.
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks, Queues, and Deques
Ordered Structures Wellesley College CS230 Lecture 10
Lecture 16 Stacks and Queues CSE /26/2018.
Presentation transcript:

COMP 103 Linked Stack and Linked Queue

RECAP-TODAY RECAP TODAY Linked data structures: Linked Node LinkedList class TODAY Linked Stack Linked Queue Other linked collections

A Linked Stack Implement a Stack using a linked list. Which end is the top of the stack? pop() push(“A”) Why is this a bad idea? data M J P C X TOP O(n) A H

A Linked Stack Make the top of the Stack be the head of the list. TOP pop() push(“A”) data M TOP J P C X A H

Implementing LinkedStack Implementing a stack using LinkedNode as underlying data structure (instead of array) public class LinkedStack <E> extends AbstractCollection<E> { private LinkedNode<E> data = null; public LinkedStack() {  } // constructor public int size() { … } public boolean empty() { … } public E peek() { … } // Usually called top public E pop() { … } public void push(E item) { … } public int search(Object o) { … } // plus others inherited from AbstractCollection, such as toArray() and others... } data

More LinkedStack methods public boolean empty() { return data == null; } public E peek() { if (data == null) throw new EmptyStackException(); return data.get(); public E pop() { if (data == null) throw new EmptyStackException(); E ans = data.get(); data = data.next(); return ans; public void push(E item) { if (item == null) throw new IllegalArgumentException(); data = new LinkedNode(item, data); data M H data does 3 jobs...

Iterator (for any list) Iterating down a stack. for (String str: myStack) System.out.println(str) myStack M X data P J C iterator node H

Iterator public Iterator <E> iterator() {     return new LinkedNodeIterator(data); } private class LinkedNodeIterator <E> implements Iterator <E> { private LinkedNode<E> node;     // node containing next item public LinkedNodeIterator (LinkedNode <E> node) { this.node = node; public boolean hasNext () { return (node != null); public E next () { if (node == null) throw new NoSuchElementException(); E ans = node.get(); node = node.next(); return ans; public void remove () { throw new UnsupportedOperationException();

A Linked Queue #1 Put the front of the queue at the end of the list offer(“A”) O(1) poll() O(n) back M A J P C H X Front

A Linked Queue #2 Put the front of the Queue at the start of the list. poll() O(1) offer(“A”) O(n) is too Slow!! Front M J P C X H Back A

A Better Linked Queue Keep pointers to both ends! poll() O(1) offer(“A”) O(1) M P Front Back X J C H Back A

Implementing LinkedQueue public class LinkedQueue <E> implements AbstractQueue <E> { private LinkedNode<E> front = null; private LinkedNode<E> back = null; public LinkedQueue() {  } public int size() { … public boolean isEmpty() { … public E peek() { … public E poll() { … public void offer(E item) { … public Iterator <E> iterator() { … M Front Back A

LinkedQueue public boolean isEmpty() { return front == null; } public int size () { if (front == null)   return 0; else   return front.size(); public E peek() { if (front == null) return null; else return front.get(); Front Back Front Back M A

LinkedQueue three cases: 0 items, 1 item, >1 items public E poll() { if (front == null) return null; E ans = front.get(); front = front.next(); if (front == null) back = null; return ans; } Front Back three cases: 0 items, 1 item, >1 items Front Back M Front Back A A

LinkedQueue three cases: >1 item, 1 item, 0 items public boolean offer(E item) { if (item == null) return false; if (front == null) {     back = new LinkedNode(item, null);     front = back; } else {     back.setNext(new LinkedNode(item, null));     back= back.next(); } return true; } Front Back M A three cases: >1 item, 1 item, 0 items Front Back Front Back A

Linked Stacks and Queues Use a “header node” Keep link to head node, and maybe last node of linked list Important to choose the right end. Easy to add or remove from head of a linked list Easy to add to last node of linked list (if have pointer to tail too) Hard to remove the last node of linked list (do you see why?) Linked Stack and Queue: all main operations are O(1) Can combine Stack and Queue giving Dequeue. addFirst, addLast, removeFirst (or pushLeft, pushRight, popLeft) How can we make all operations fast? See the java “LinkedList” class.

Other linked collections Set Can store sorted or unsorted What is the cost of add, remove, contains, size, equals in each case? Bag Same as set, but add a count to each node. Map Same as set, but store value as well as key in each node Or store pointed to a key-value pair What is the cost of get, put, remove?