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.

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

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
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
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
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++)
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.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
CS Data Structures II Review COSC 2006 April 14, 2017
COMP 103 Linked Stack and Linked Queue.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
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.
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’
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.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
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.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
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.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Iteration Abstraction SWE Software Construction Fall 2009.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
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 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.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Linked Data Structures
Cpt S 122 – Data Structures Abstract Data Types
CSE 373 Implementing a Stack/Queue as a Linked List
The List ADT Reading: Textbook Sections 3.1 – 3.5
Tirgul 12 Data Structures (2) 1.
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Iteration Abstraction
Queue, Deque, and Priority Queue Implementations
Linked Lists, Stacks and Queues Textbook Sections
Deques, Stacks and Queues
The List ADT Reading: Textbook Sections 3.1 – 3.5
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
ADT list.
Doubly Linked List Implementation
Data Structures and Algorithms
Lecture 16 Stacks and Queues CSE /26/2018.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Doubly Linked List Implementation
Presentation transcript:

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  MyLinkedList class  Node class  LinkedListIterator class  Sentinel nodes at head and tail

8/3/2007 UMBC CMSC 341 LSQ 3 Empty Linked List An empty double linked list with sentinel nodes.

8/3/2007 UMBC CMSC 341 LSQ 4 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).

8/3/2007 UMBC CMSC 341 LSQ 5 Nested classes Considered part of the outer class, thus no issues of visibility. No reference to the outer class. If a nested (static) class has public accessibility, then it can be instantiated without the outer class. Making an inner class private means 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?

8/3/2007 UMBC CMSC 341 LSQ 6 Implementation for MyLinkedList 1. Class declaration and nested Node class public class MyLinkedList implements Iterable { // static key word makes Node a nested class private static class Node { public Node( AnyType d, Node p, Node n ) { data = d; prev = p; next = n; } public AnyType data; public Node prev; public Node next; }

8/3/2007 UMBC CMSC 341 LSQ 7 2. Data Fields and Accessors private int theSize; //used to help iterator detect changes in List private int modCount = 0; private Node beginMarker; //head node private Node endMarker; //tail node public int size( ){ return theSize; } public boolean isEmpty( ){ return size( ) == 0; }

8/3/2007 UMBC CMSC 341 LSQ 8 3. Constructor(s) public MyLinkedList( ) { clear( ); } // Changes the size of this collection to zero. public void clear( ) { beginMarker = new Node ( null, null,null ); endMarker = new Node ( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; }

8/3/2007 UMBC CMSC 341 LSQ 9 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 p = getNode( idx ); AnyType oldVal = p.data; p.data = newVal; return oldVal; } public AnyType remove( int idx ) { return remove( getNode( idx ) ); }

8/3/2007 UMBC CMSC 341 LSQ getNode Method private Node getNode( int idx ) { Node p; if( 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; }

8/3/2007 UMBC CMSC 341 LSQ addBefore Method private void addBefore(Node p, AnyType x) { Node newNode = new Node ( x, p.prev, p ); newNode.prev.next = newNode; p.prev = newNode; theSize++; modCount++; }

8/3/2007 UMBC CMSC 341 LSQ remove and iterator Methods private AnyType remove( Node 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 iterator( ) { return new LinkedListIterator( ); }

8/3/2007 UMBC CMSC 341 LSQ 13 8a. LinkedListIterator class import java.util.*; private class LinkedListIterator implements Iterator { private Node 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…

8/3/2007 UMBC CMSC 341 LSQ 14 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…

8/3/2007 UMBC CMSC 341 LSQ 15 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

8/3/2007 UMBC CMSC 341 LSQ 16 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

8/3/2007 UMBC CMSC 341 LSQ 17 Stack ADT Basic operations are push, pop, and top Stack Model

8/3/2007 UMBC CMSC 341 LSQ 18 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).

8/3/2007 UMBC CMSC 341 LSQ 19 Client (Stack user) Stack (adapter) List (adaptee) theStack.push( 10 ) theList.add(0, 10 ) ; Adapter Model for Stack

8/3/2007 UMBC CMSC 341 LSQ 20 Queues Restricted List  only add to head  only remove from tail Examples  line waiting for service  jobs waiting to print Implement as an adapter of List

8/3/2007 UMBC CMSC 341 LSQ 21 Queue ADT Basic Operations are enqueue and dequeue

8/3/2007 UMBC CMSC 341 LSQ 22 Client (Queue user) List (adaptee) theQ.enqueue( 10 ) theList.add(theList.size() -1, 10 ) Queue (adapter) Adapter Model for Queue