ADT - Data Structures Web view@return the node that is this nodeÕs right child */ public...
Transcript of ADT - Data Structures Web view@return the node that is this nodeÕs right child */ public...
KING SAUD UNIVERSITY
COLLEGE OF COMPUTER & INFORMATION SCIENCES
Information Technology Department
8
IT 212: Data Structures with JAVA
ADT
Specification
This document is extracted from the text book:
Data Structures and Abstractions with Java
THIRD EDITION
Frank M. Carrano
Prepared by Samira Lahmar
Second Semester 1427-1428/2007
Edited by Nadia Al-Ghreimil
Second Semester 1430-1431
Edited by Mashael AlDuwais
Second Semester 1435-1436
This copy belongs to: __________________________________
NO modifications to this document are allowed.
DO NOT write on this document anything other than your Name.
Private Class Node
private class Node
{
private T data; // entry in the sturucture
private Node next; // link to next node
private Node(T dataPortion)
{
data = dataPortion;
next = null;
} // end constructor
private Node(T dataPortion, Node linkPortion)
{
data = dataPortion;
next = linkPortion;
} // end constructor
private T getData()
{
return data;
} // end getData
private void setData(T newData)
{
data = newData;
} // end setData
private Node getNextNode()
{
return next;
} // end getNextNode
private void setNextNode(Node nextNode)
{
next = nextNode;
} // end setNextNode
} // end Node
Bag
BagInterface
/** An interface that describes the operations of a bag of objects.
public interface BagInterface < T >
{
/** Gets the current number of entries in this bag.
@return the integer number of entries currently in the bag */
public int getCurrentSize ();
/** Sees whether this bag is full.
@return true if the bag is full, or false if not */
public boolean isFull ();
/** Sees whether this bag is empty.
@return true if the bag is empty, or false if not */
public boolean isEmpty ();
/** Adds a new entry to this bag.
@param newEntry the object to be added as a new entry
@return true if the addition is successful, or false if not */
public boolean add (T newEntry);
/** Removes one unspecified entry from this bag, if possible.
@return either the removed entry, if the removal was successful, or null */
public T remove ();
/** Removes one occurrence of a given entry from this bag, if possible.
@param anEntry the entry to be removed
@return true if the removal was successful, or false if not */
public boolean remove (T anEntry);
/** Removes all entries from this bag. */
public void clear ();
/** Counts the number of times a given entry appears in this bag.
@param anEntry the entry to be counted
@return the number of times anEntry appears in the bag */
public int getFrequencyOf (T anEntry);
/** Tests whether this bag contains a given entry.
@param anEntry the entry to locate
@return true if the bag contains anEntry, or false otherwise */
public boolean contains (T anEntry);
/** Creates an array of all entries that are in this bag.
@return a newly allocated array of all the entries in the bag */
public T [] toArray ();
} // end BagInterface
Using Array to implement ADT bag
/** A class of bags whose entries are stored in a fixed-size array.
public class ArrayBag < T > implements BagInterface < T > {
private final T [] bag; // array of bag entries
private static final int DEFAULT_CAPACITY = 25;
// default size of bag
private int numberOfEntries;//current number of entries in bag
public ArrayBag ()
{
this(DEFAULT_INITIAL_CAPACITY);
} // end default constructor
/ Implementations of the public methods declared in BagInterface go here.
// Locates a given entry within the array bag.
// Returns the index of the entry, if located,
// or -1 otherwise.
private int getIndexOf(T anEntry)
{
//implementation goes here
}
// Removes and returns the entry at a given index within the array bag.
// If no such entry exists, returns null.
private T removeEntry(int givenIndex)
{
//implementation goes here
}
} // end of class ArrayBag
A linked implementation of ADT bag
/** A class of bags whose entries are stored in a chain of linked nodes. The bag is never full.
public class LinkedBag < T > implements BagInterface < T > {
private Node firstNode; // reference to first node
private int numberOfEntries; // current number of entries in bag
public LinkedBag ()
{
firstNode = null;
numberOfEntries = 0;
} // end default constructor
/ Implementations of the public methods declared in BagInterface go here.
private class Node // private inner class
{
.
} // end Node
} // end of class LinkedBag
Stack
StackInterface
/**
An interface for the ADT stack.
*/
public interface StackInterface
{
/** Adds a new entry to the top of this stack.
@param newEntry an object to be added to the stack */
public void push(T newEntry);
/** Removes and returns this stacks top entry.
@return either the object at the top of the stack or, if the
stack is empty before the operation, null */
public T pop();
/** Retrieves this stacks top entry.
@return either the object at the top of the stack or null if
the stack is empty */
public T peek();
/** Detects whether this stack is empty.
@return true if the stack is empty */
public boolean isEmpty();
/** Removes all entries from this stack */
public void clear();
} // end StackInterface
Using array to implement ADT Stack
/**
A class of stacks whose entries are stored in an array.
*/
public class ArrayStack implements StackInterface
{
private T[] stack; // array of stack entries
private int topIndex; // index of top entry
private static final int DEFAULT_INITIAL_CAPACITY = 50;
public ArrayStack()
{
this(DEFAULT_INITIAL_CAPACITY);
} // end default constructor
public ArrayStack(int initialCapacity)
{
// the cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempStack = (T[])new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
} // end constructor
// Implementations of public methods go here.
// Doubles the size of the array stack if it is full
private void ensureCapacity()
{
} // end ensureCapacity
} // end ArrayStack
A linked implementation of ADT Stack
/**
A class of stacks whose entries are stored in a chain of nodes.
*/
public class LinkedStack implements StackInterface
{
private Node topNode; // references the first node in the chain
public LinkedStack()
{
topNode = null;
} // end default constructor
// Implementations of public methods go here.
private class Node
{
.
} // end Node
} // end LinkedStack
Queue
QueueInterface
/**
An interface for the ADT queue.
*/
public interface QueueInterface
{
/* Adds a new entry to the back of this queue.
@param newEntry an object to be added */
public void enqueue(T newEntry);
/* Removes and returns the entry at the front of this queue.
@return either the object at the front of the queue or, if the
queue is empty before the operation, null */
public T dequeue();
/* Retrieves the entry at the front of this queue.
@return either the object at the front of the queue or, if the
queue is empty, null */
public T getFront();
/* Detects whether this queue is empty.
@return true if the queue is empty, or false otherwise */
public boolean isEmpty();
/* Removes all entries from this queue. */
public void clear();
} // end QueueInterface
Using array to implement ADT Queue
/**
A class that implements the ADT queue by using an array with one unused location.
*/
public class ArrayQueue implements QueueInterface
{
private T[] queue; /* circular array of queue entries and one unused location*/
private int frontIndex;
private int backIndex;
private static fi