Data Structures in Java 120301 Elementary Data Structures

  • Slides: 39
Download presentation
Data Structures in Java 12/03/01

Data Structures in Java 12/03/01

Elementary Data Structures n Stack n n Queue n n container of elements that

Elementary Data Structures n Stack n n Queue n n container of elements that are inserted and removed first-in first-out (FIFO) Linked List n n n 2/26/02 container of elements that are inserted and removed last-in first-out (LIFO) chain of elements can grow dynamically often used to implement other data structures © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Stack n n Last-in, First-out (LIFO) structure Operations n n push: add element into

Stack n n Last-in, First-out (LIFO) structure Operations n n push: add element into the stack pop: remove & return topmost element empty: check if the stack has no elements Sample uses n 2/26/02 “Back” button of a browser, “Undo” operation, function/method calls © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Array Implementation for a Stack of ints public class Array. Stack { int store[];

Array Implementation for a Stack of ints public class Array. Stack { int store[]; int top; static final int MAX = 100; public Array. Stack() { store = new int[MAX]; top = 0; } //. . . } 12 24 2/26/02 37 17 top 4 top = index of the first FREE slot . . . © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Array. Stack class, continued public class Array. Stack { //. . . public boolean

Array. Stack class, continued public class Array. Stack { //. . . public boolean empty() { return (top == 0); } public void push(int entry) { if (top < MAX) store[top++] = entry; } //. . . } 12 24 2/26/02 37 17 // in the code that uses // the stack … stack. push( 95 ); Remember: top = index of the first FREE slot top 45 95 . . . © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Array. Stack class, continued public class Array. Stack { //. . . public int

Array. Stack class, continued public class Array. Stack { //. . . public int pop() throws Exception { if ( empty() ) throw new Exception(); else return store[--top]; } } 12 24 2/26/02 37 17 // in the code that uses // the stack … int x = stack. pop(); // x gets 95, // slot 4 is now free Remember: top = index of the first FREE slot top 54 95 Store[4] still contains 95, but it’s now considered “free”. . © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Using the Stack try { Array. Stack st = new Array. Stack(); st. push(

Using the Stack try { Array. Stack st = new Array. Stack(); st. push( 1 ); st. push( 3 ); st. push( 2 ); System. out. println( st. pop() ); st. push( 5 ); System. out. println( st. pop() ); } catch ( Exception e ) { System. out. println( “pop() on empty stack” ); } 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Problems with Array Implementation n n MAX needs to be specified Consequences n n

Problems with Array Implementation n n MAX needs to be specified Consequences n n n stack may fill up (when top == MAX) memory is wasted if actual stack consumption is way below maximum Need a more “dynamic” implementation 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Linked List Implementation n Stack as a sequence of nodes top 5 2/26/02 1

Linked List Implementation n Stack as a sequence of nodes top 5 2/26/02 1 3 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 2 null Slide 12/03/01

Integer Node public class Int. Node { int data; Int. Node next; // set

Integer Node public class Int. Node { int data; Int. Node next; // set public & get methods for data and next void set. Data( int data ). . . int get. Data(). . . void set. Next( Int. Node next ). . . Int. Node get. Next(). . . } 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Linked List as a Data Structure n Operations on a linked list n n

Linked List as a Data Structure n Operations on a linked list n n n insert a node somewhere in the list get next node delete a node from the list The Int. Node class supports the Linked List structure Linked List Implementation of a Stack: n 2/26/02 an example of a data structure implemented through another data structure © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Linked. Stack Class public class Linked. Stack { Int. Node top; public Linked. Stack()

Linked. Stack Class public class Linked. Stack { Int. Node top; public Linked. Stack() { top = null; } public boolean empty() { return (top == null); } //. . . } top 5 2/26/02 1 3 2 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University null Slide 12/03/01

Push Operation using a List // in the code // that uses the //

Push Operation using a List // in the code // that uses the // stack. push( 7 ); temp top public class Linked. Stack { //. . . public void push( int entry ) { Int. Node temp = new Int. Node(); temp. set. Data( entry ); temp. set. Next( top ); top = temp; } //. . . } X 7 2/26/02 5 1 3 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 2 null Slide 12/03/01

Pop Operation using a List // in the code // that uses the //

Pop Operation using a List // in the code // that uses the // stack int x = stack. pop(); temp top 7 Garbage Collected 7 2/26/02 X 5 public class Linked. Stack { //. . . public int pop() throws Exception { if ( empty() ) { throw new Exception(); } else { int temp = top. get. Data(); top = top. get. Next(); return temp; } } } 1 3 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University 2 null Slide 12/03/01

Separating Interface from Implementation n Better if there is a Stack interface that both

Separating Interface from Implementation n Better if there is a Stack interface that both Array. Stack and Linked. Stack implements public interface Stack { public void push( int entry ); public int pop() throws Exception; public boolean empty(); } 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Stack Implementations public class Array. Stack implements Stack {. . . } public class

Stack Implementations public class Array. Stack implements Stack {. . . } public class Linked. Stack implements Stack {. . . } 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

A Stack. Factory Class n Use a separate class that produces Stack objects public

A Stack. Factory Class n Use a separate class that produces Stack objects public class Stack. Factory { public static Stack create. Stack() { return new Array. Stack(); // or return new Linked. Stack(); } } n Advantage: n n 2/26/02 if you want to change your implementation, you just need to change Stack. Factory you don’t need to change all calls to new Array. Stack in all your code! © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Sample Code (application that uses a stack) Stack st; st = Stack. Factory. create.

Sample Code (application that uses a stack) Stack st; st = Stack. Factory. create. Stack(); // above statement does not know or care // how the stack is implemented st. push(1); st. push(3); st. push(2); System. out. println( st. pop() ); //. . . 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

A Generic Stack n Make the stack data an instance of Object n n

A Generic Stack n Make the stack data an instance of Object n n n Array implementation: n n can work with any kind of object, not just int you can use Integer class for int data (same for other primitive types) store is an Object[], instead of int[] Linked List implementation: n n 2/26/02 use Object. Node (not just Int. Node) more code later © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Linked Stack of Objects top null 2/26/02 © 2002 Luis F. G. Sarmenta and

Linked Stack of Objects top null 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Java’s Stack Class n n Good news: the java. util package already has a

Java’s Stack Class n n Good news: the java. util package already has a Stack (of objects) class Methods available n n 2/26/02 javap java. util. Stack others besides push, pop, and empty © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Queue n n First-in, First-out (FIFO) structure Operations n n enqueue: insert element at

Queue n n First-in, First-out (FIFO) structure Operations n n enqueue: insert element at rear dequeue: remove & return front element empty: check if the queue has no elements Sample use n 2/26/02 handling requests and reservations © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

The Queue Interface public interface Queue { public void enqueue( Object entry ); public

The Queue Interface public interface Queue { public void enqueue( Object entry ); public Object dequeue() throws Exception; public boolean empty(); } 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Array Implementation of Queues n An Object array and two integers n n front:

Array Implementation of Queues n An Object array and two integers n n front: index of first element in queue rear: index of first FREE element in queue front 0 rear 4. . . 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Array. Queue public class Array. Queue implements Queue { Object store[]; int front, rear;

Array. Queue public class Array. Queue implements Queue { Object store[]; int front, rear; static final int MAX = 100; public Array. Queue() { store = new Object[MAX]; front = rear = 0; } //. . . } 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Check for Empty and Enqueue public class Array. Queue implements Queue { //. .

Check for Empty and Enqueue public class Array. Queue implements Queue { //. . . public boolean empty() { // queue is empty if first element // is the same as the first free element return ( front == rear ); } public void enqueue( Object o ) { // put data in first free slot // move rear index to next slot if ( rear < MAX ) store[rear++] = o; } //. . . rear front } 4 0 . . . 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Dequeue Operation public class Array. Queue implements Queue { //. . . public Object

Dequeue Operation public class Array. Queue implements Queue { //. . . public Object dequeue() throws Exception { if ( empty() ) { throw new Exception(); } else { return store[front++]; } rear } front } 4 0 . . . 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Dequeue that allows GC public class Array. Queue implements Queue { // … public

Dequeue that allows GC public class Array. Queue implements Queue { // … public Object dequeue() throws Exception { if ( empty() ) { throw new Exception(); } else { Object data = store[front]; // get front data // set pointer to null, so it can be garbage-collected // later (when caller code doesn’t let’s go of pointer store[front] = null; front++; return data; } } rear front } 0 4 . . . 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Circular Array n n n Suppose many enqueue operations followed by many dequeue operations

Circular Array n n n Suppose many enqueue operations followed by many dequeue operations Result: rear approaches MAX but the queue is not really full Solution: Circular Array n 2/26/02 allow rear (and front) to “wrap around” the array (if rear = MAX-1, incrementing rear means resetting it to 0) © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Circular Array, continued n When is the array full? n n n Solution: Reserve

Circular Array, continued n When is the array full? n n n Solution: Reserve a slot n n n Simple answer: when (rear == front) Problem: this is the same condition as empty full: empty: when ( (rear+1) % MAX == front) (one free slot left) when ( rear == front ) Note: “wastes” a slot n n n alternative: have a boolean field called has. Elements full: when ( has. Elements && (rear == front)) But not really better n n 2/26/02 has. Elements takes up extra space too Also, need to take care of has. Elements in enqueue and dequeue © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Revised Enqueue public class Array. Queue implements Queue { //. . . public void

Revised Enqueue public class Array. Queue implements Queue { //. . . public void enqueue( Object o ) { if ( (rear + 1) % MAX != front ) { store[rear] = o; rear = (rear + 1) % MAX; } } //. . . } 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Revised Dequeue public class Array. Queue implements Queue { //. . . public Object

Revised Dequeue public class Array. Queue implements Queue { //. . . public Object dequeue() throws Exception { if ( empty() ) { throw new Exception(); } else { Object data = store[front]; store[front] = null; front = (front + 1) % MAX; return data; } } © 2002 Luis F. G. Sarmenta and John Paul Vergara, } 2/26/02 Ateneo de Manila University Slide 12/03/01

Using the Queue q = new Array. Queue(); int result; // the following should

Using the Queue q = new Array. Queue(); int result; // the following should be enclosed in a try-catch stmt q. enqueue( new Integer(5) ); q. enqueue( new Integer(2) ); result = ((Integer)q. dequeue()). int. Value(); System. out. println(result); q. enqueue( new Integer(3) ); result = ((Integer)q. dequeue()). int. Value(); System. out. println( result ); 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Linked List Implementation front rear null 2/26/02 © 2002 Luis F. G. Sarmenta and

Linked List Implementation front rear null 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

The Object. Node Class public class Object. Node { Object data; Object. Node next;

The Object. Node Class public class Object. Node { Object data; Object. Node next; public void set. Data( Object o ) { data = o; } Object get. Data() { return data; } void set. Next( Object. Node p ) { next = p; } Object. Node get. Next() { return next; } } 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Enqueue front rear null 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul

Enqueue front rear null 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Dequeue front rear null return this object 2/26/02 © 2002 Luis F. G. Sarmenta

Dequeue front rear null return this object 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

The Linked. Queue Class public class Linked. Queue implements Queue { Object. Node front;

The Linked. Queue Class public class Linked. Queue implements Queue { Object. Node front; Object. Node rear; public Linked. Queue() { front = rear = null; } // other Queue methods } 2/26/02 © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01

Exercise: Snake game n Run the game n n n Note that it uses

Exercise: Snake game n Run the game n n n Note that it uses an instance of the Queue interface currently, it uses Array. Queue, as defined in lecture Complete the Linked. Queue class n need to worry about n n n the “empty-queue” case and the case where the queue has only 1 element Modify Queue. Factory class to create a Linked. Queue instead of Array. Queue Test your Linked. Queue by running Snake Enhance/Fix Snake n 2/26/02 e. g. , add a border, add special points, etc. © 2002 Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University Slide 12/03/01