+ All Categories
Home > Documents > CMSC 341

CMSC 341

Date post: 31-Dec-2015
Category:
Upload: odette-hooper
View: 16 times
Download: 1 times
Share this document with a friend
Description:
Linked Lists, Stacks and Queues Textbook Sections 3.5 - 3.7. CMSC 341. 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. Empty Linked List. - PowerPoint PPT Presentation
26
July 2011 CMSC 341 Lists, Stacks &Queues 1 CMSC 341 Linked Lists, Stacks and Queues Textbook Sections 3.5 - 3.7
Transcript
Page 1: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 1

CMSC 341

Linked Lists, Stacks and Queues

Textbook Sections 3.5 - 3.7

Page 2: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 2

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

Page 3: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 3

Empty Linked List

An empty double linked list with sentinel nodes.

Page 4: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 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).

Page 5: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 5

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?

Page 6: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 6

Implementation for MyLinkedList1. 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; }

Page 7: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 7

2. Data Fields and Accessorsprivate 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;

}

Page 8: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 8

3. Constructor(s)

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++; }

Page 9: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 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<AnyType> p = getNode( idx ); AnyType oldVal = p.data; p.data = newVal; return oldVal; } public AnyType remove( int idx ) { return remove( getNode( idx ) ); }

Page 10: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 10

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;

}

Page 11: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 11

6. addBefore Method

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++;

}

Page 12: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 12

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( ); }

Page 13: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 13

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…

Page 14: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 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…

Page 15: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 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

Page 16: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 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

Page 17: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 17

Stack ADT

Basic operations are push, pop, and top

Stack Model

Page 18: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 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).

Page 19: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 19

Client (Stack user)

Stack (adapter)

List (adaptee)

theStack.push( 10 )

theList.add(0, 10 ) ;

Adapter Model for Stack

Page 20: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 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

Page 21: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 21

Queue ADT

Basic Operations are enqueue and dequeue

Page 22: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 22

Client (Queue user)

List (adaptee)

theQ.enqueue( 10 )

theList.add(theList.size() -1, 10 )

Queue (adapter)

Adapter Model for Queue

Page 23: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 23

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

Page 24: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 24

Circular Queue Data• A fixed size array• Control Variables

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.

Initialized to 0

back

the array index last item that was enqueued

Initialized to -1

Page 25: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 25

Circular Queue Psuedocodevoid 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;

}

Page 26: CMSC 341

July 2011 CMSC 341 Lists, Stacks &Queues 26

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( )


Recommended