Linked Lists, Stacks and Queues Textbook Sections

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
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Double-Linked Lists and Circular Lists
The List ADT Textbook Sections
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.
CHAPTER 7 Queues.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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
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.
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.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
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.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
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.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
“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.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
Cpt S 122 – Data Structures Abstract Data Types
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
CSE 373 Implementing a Stack/Queue as a Linked List
The List ADT Reading: Textbook Sections 3.1 – 3.5
Data Structures and Algorithms
Stacks.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queue, Deque, and Priority Queue Implementations
Stacks, Queues, and Deques
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
Deques, Stacks and Queues
The List ADT Reading: Textbook Sections 3.1 – 3.5
CSC 143 Queues [Chapter 7].
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,
Programming II (CS300) Chapter 07: Linked Lists and Iterators
ADT list.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Doubly Linked List Implementation
Data Structures and Algorithms
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lecture 16 Stacks and Queues CSE /26/2018.
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Doubly Linked List Implementation
Data Structures & Programming
Presentation transcript:

Linked Lists, Stacks and Queues Textbook Sections 3.5 - 3.7 CMSC 341 Linked Lists, Stacks and Queues Textbook Sections 3.5 - 3.7 July 2011 CMSC 341 Lists, Stacks &Queues

Implementing A Linked List To create a doubly linked list as seen below MyLinkedList class Node class LinkedListIterator class Sentinel nodes at head and tail July 2011 CMSC 341 Lists, Stacks &Queues

Empty Linked List An empty double linked list with sentinel nodes. July 2011 CMSC 341 Lists, Stacks &Queues

Inner classes Inner class objects require the construction of an outer class object before they are instantiated. Compiler adds an implicit reference to outer class in an inner class (MyArrayList.this). Good for when you need several inner objects to refer to exactly one outer object (as in an Iterator object). July 2011 CMSC 341 Lists, Stacks &Queues

Nested classes Considered part of the outer class, thus no issues of visibility. Making an inner class private means that only the outer class may access the data fields within the nested class. Is Node a prime candidate for nested or inner class? public or private? Private nested class…. July 2011 CMSC 341 Lists, Stacks &Queues

Implementation for MyLinkedList Class declaration and nested Node class public class MyLinkedList<AnyType> implements Iterable<AnyType> { // Node is a nested class private static class Node<AnyType> public Node( AnyType d, Node<AnyType> p, Node<AnyType> n ) { data = d; prev = p; next = n; } public AnyType data; public Node<AnyType> prev; public Node<AnyType> next; } July 2011 CMSC 341 Lists, Stacks &Queues

2. Data Fields and Accessors private int theSize; //used to help iterator detect changes in List private int modCount = 0; private Node<AnyType> beginMarker; //head node private Node<AnyType> endMarker; //tail node public int size( ){ return theSize; } public boolean isEmpty( ){ return size( ) == 0; July 2011 CMSC 341 Lists, Stacks &Queues

3. Constructor(s) July 2011 CMSC 341 Lists, Stacks &Queues public MyLinkedList( ) { clear( ); } // Changes the size of this collection to zero. public void clear( ) { beginMarker = new Node<AnyType>( null, null,null ); endMarker = new Node<AnyType>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } July 2011 CMSC 341 Lists, Stacks &Queues

4. More Accessors and Mutators public boolean add( AnyType x ) { add( size( ), x ); return true; } public void add( int idx, AnyType x ) { addBefore( getNode( idx ), x ); } public AnyType get( int idx ) { return getNode( idx ).data; } public AnyType set( int idx, AnyType newVal ) { Node<AnyType> p = getNode( idx ); AnyType oldVal = p.data; p.data = newVal; return oldVal; } public AnyType remove( int idx ) { return remove( getNode( idx ) ); } July 2011 CMSC 341 Lists, Stacks &Queues

5. getNode Method private Node<AnyType> getNode( int idx ) { Node<AnyType> p; if( idx < 0 || idx > size( ) ) throw new IndexOutOfBoundsException( ); if( idx < size( ) / 2 ) { p = beginMarker.next; for( int i = 0; i < idx; i++ ) p = p.next; } else { p = endMarker; for( int i = size( ); i > idx; i-- ) p = p.prev; } return p; /** * Gets the Node at position idx, which must range from 0 to size( ). * @param idx index of node being obtained. * @return internal node corresponding to idx. * @throws IndexOutOfBoundsException if idx is not between 0 and size(). */ July 2011 CMSC 341 Lists, Stacks &Queues

6. addBefore Method July 2011 CMSC 341 Lists, Stacks &Queues private void addBefore(Node<AnyType> p, AnyType x) { Node<AnyType> newNode = new Node<AnyType>( x, p.prev, p ); newNode.prev.next = newNode; p.prev = newNode; theSize++; modCount++; } /** * Adds an item to this collection, at specified position p. * Items at or after that position are slid one position higher. * @param p Node to add before. * @param x any object. * @throws IndexOutOfBoundsException if idx is not between 0 and size(),. */ July 2011 CMSC 341 Lists, Stacks &Queues

7. remove and iterator methods private AnyType remove( Node<AnyType> p ) { p.next.prev = p.prev; p.prev.next = p.next; theSize--; modCount++; return p.data; } //required by the Iterable interface public java.util.Iterator<AnyType> iterator( ) { return new LinkedListIterator( ); } /** * Removes the object contained in Node p. * @param p the Node containing the object. * @return the item was removed from the collection. */ July 2011 CMSC 341 Lists, Stacks &Queues

8a. LinkedListIterator class private class LinkedListIterator implements Iterator<AnyType> { private Node<AnyType> current = beginMarker.next; //used to check for modifications to List private int expectedModCount = modCount; private boolean okToRemove = false; public boolean hasNext( ) return current != endMarker; } //continues on next slide… July 2011 CMSC 341 Lists, Stacks &Queues

8b. LinkedListIterator class public AnyType next( ) { if( modCount != expectedModCount ) throw new ConcurrentModificationException( ); if( !hasNext( ) ) throw new NoSuchElementException( ); AnyType nextItem = current.data; current = current.next; okToRemove = true; return nextItem; } //continues on next slide… July 2011 CMSC 341 Lists, Stacks &Queues

8c. LinkedListIterator class public void remove( ){ if( modCount != expectedModCount ) throw new ConcurrentModificationException( ); if( !okToRemove ) throw new IllegalStateException( ); MyLinkedList.this.remove(current.prev); okToRemove = false; ++expectedModCount; } // end of remove Method } // end of LinkedListIterator class }//end of MyLinkedList class July 2011 CMSC 341 Lists, Stacks &Queues

Stacks A restricted list where insertions and deletions can only be performed at one location, the end of the list (top). LIFO – Last In First Out Laundry Basket – last thing you put in is the first thing you remove Plates – remove from the top of the stack and add to the top of the stack July 2011 CMSC 341 Lists, Stacks &Queues

Stack ADT Basic operations are push, pop, and top Stack Model July 2011 CMSC 341 Lists, Stacks &Queues

Adapting Lists to Implement Stacks Adapter Design Pattern Allow a client to use a class whose interface is different from the one expected by the client Do not modify client or class, write adapter class that sits between them In this case, the List is an adapter for the Stack. The client (user) calls methods of the Stack which in turn calls appropriate List method(s). July 2011 CMSC 341 Lists, Stacks &Queues

Adapter Model for Stack Client (Stack user) Stack (adapter) List (adaptee) theStack.push( 10 ) theList.add(0, 10 ) ; July 2011 CMSC 341 Lists, Stacks &Queues

Queues Restricted List Examples Implement as an adapter of List only add to head only remove from tail Examples line waiting for service jobs waiting to print Implement as an adapter of List Implementing a queue could use similar approach to stack want efficient access to both ends could use circular doubly-linked lists July 2011 CMSC 341 Lists, Stacks &Queues

Queue ADT Basic Operations are enqueue and dequeue July 2011 CMSC 341 Lists, Stacks &Queues

Adapter Model for Queue Client (Queue user) theQ.enqueue( 10 ) Queue (adapter) theList.add(theList.size() -1, 10 ) List (adaptee) July 2011 CMSC 341 Lists, Stacks &Queues

Circular Queue Adapter pattern may be impractical Overhead for creating, deleting nodes Max size of queue is often known A circular queue is a fixed size array Slots in array reused after elements dequeued July 2011 CMSC 341 Lists, Stacks &Queues

Circular Queue Data A fixed size array Control Variables July 2011 arraySize the fixed size (capacity) of the array currentSize the current number of items in the queue Initialized to 0 front the array index from which the next item will be dequeued. back the array index last item that was enqueued Initialized to -1 July 2011 CMSC 341 Lists, Stacks &Queues

Circular Queue Psuedocode void enqueue( Object x ) { if currentSize == arraySize, throw exception // Q is full back = (back + 1) % arraySize; array[ back ] = x; ++currentSize; } Object dequeue( ) { if currentSize == 0, throw exception // Q is empty --currentSize; Object x = array[ front ]; front = (front + 1) % arraySize return x; July 2011 CMSC 341 Lists, Stacks &Queues

Circular Queue Example 0 1 2 3 4 5 Trace the contents of the array and the values of currentSize, front and back after each of the following operations. 1. enqueue( 12 ) 7. enqueue( 42 ) 2. enqueue( 17 ) 8. dequeue( ) 3. enqueue( 43 ) 9. enqueue( 33 ) 4. enqueue( 62 ) 10. enqueue( 18 ) 5. dequeue( ) 11. enqueue( 99 ) 6. dequeue( ) July 2011 CMSC 341 Lists, Stacks &Queues