assignment
This commit is contained in:
30
assignments/AssigmentS6/DequeInterface.java
Normal file
30
assignments/AssigmentS6/DequeInterface.java
Normal file
@ -0,0 +1,30 @@
|
||||
/** An interface for the ADT deque.
|
||||
@author Frank M. Carrano
|
||||
*/
|
||||
public interface DequeInterface<T>
|
||||
{
|
||||
/** Adds a new entry to the front/back of this deque.
|
||||
@param newEntry An object to be added. */
|
||||
public void addToFront(T newEntry);
|
||||
public void addToBack(T newEntry);
|
||||
|
||||
/** Removes and returns the front/back entry of this deque.
|
||||
@return The object at the front/back of the deque.
|
||||
@throws EmptyQueueException if the deque is empty before the
|
||||
operation. */
|
||||
public T removeFront();
|
||||
public T removeBack();
|
||||
|
||||
/** Retrieves the front/back entry of this deque.
|
||||
@return The object at the front/back of the deque.
|
||||
@throws EmptyQueueException if the deque is empty. */
|
||||
public T getFront();
|
||||
public T getBack();
|
||||
|
||||
/** Detects whether this deque is empty.
|
||||
@return True if the deque is empty, or false otherwise. */
|
||||
public boolean isEmpty();
|
||||
|
||||
/* Removes all entries from this deque. */
|
||||
public void clear();
|
||||
} // end DequeInterface
|
17
assignments/AssigmentS6/EmptyQueueException.java
Normal file
17
assignments/AssigmentS6/EmptyQueueException.java
Normal file
@ -0,0 +1,17 @@
|
||||
/**
|
||||
A class of runtime exceptions thrown when an attempt is made to access
|
||||
or remove the front of a queue
|
||||
@author Hyunryung Kim
|
||||
*/
|
||||
public class EmptyQueueException extends RuntimeException
|
||||
{
|
||||
public EmptyQueueException()
|
||||
{
|
||||
this(null);
|
||||
} // end default constructor
|
||||
|
||||
public EmptyQueueException(String message)
|
||||
{
|
||||
super(message);
|
||||
} // end constructor
|
||||
} // end EmptyQueueException
|
156
assignments/AssigmentS6/LinkedDeque.java
Normal file
156
assignments/AssigmentS6/LinkedDeque.java
Normal file
@ -0,0 +1,156 @@
|
||||
/**
|
||||
A class that implements a deque of objects by using
|
||||
a chain of doubly linked nodes.
|
||||
@author Frank M. Carrano
|
||||
*/
|
||||
public final class LinkedDeque<T> implements DequeInterface<T>
|
||||
{
|
||||
private DLNode firstNode; // References node at front of queue
|
||||
private DLNode lastNode; // References node at back of queue
|
||||
|
||||
public LinkedDeque()
|
||||
{
|
||||
firstNode = null;
|
||||
lastNode = null;
|
||||
} // end default constructor
|
||||
|
||||
/** Adds a new entry to the front/back of this deque.
|
||||
@param newEntry An object to be added. */
|
||||
public void addToFront(T newEntry)
|
||||
{
|
||||
DLNode newNode = new DLNode(null, newEntry, firstNode);
|
||||
|
||||
if (isEmpty())
|
||||
lastNode = newNode;
|
||||
else
|
||||
firstNode.setPreviousNode(newNode);
|
||||
firstNode = newNode;
|
||||
} // end addToFront
|
||||
|
||||
public void addToBack(T newEntry)
|
||||
{
|
||||
DLNode newNode = new DLNode(lastNode, newEntry, null);
|
||||
|
||||
if (isEmpty())
|
||||
firstNode = newNode;
|
||||
else
|
||||
lastNode.setNextNode(newNode);
|
||||
// end if
|
||||
lastNode = newNode;
|
||||
} // end addToBack
|
||||
|
||||
/** Removes and returns the front/back entry of this deque.
|
||||
@return The object at the front/back of the deque.
|
||||
@throws EmptyQueueException if the deque is empty before the
|
||||
operation. */
|
||||
public T removeFront()
|
||||
{
|
||||
T front = getFront(); // Might throw an EmptyQueueException
|
||||
assert firstNode != null;
|
||||
|
||||
firstNode = firstNode.getNextNode();
|
||||
|
||||
if (firstNode == null)
|
||||
lastNode = null;
|
||||
else
|
||||
firstNode.setPreviousNode(null);
|
||||
return front;
|
||||
} // end removeFront
|
||||
|
||||
public T removeBack()
|
||||
{
|
||||
T back = getBack(); // Might throw an EmptyQueueException
|
||||
assert lastNode != null;
|
||||
lastNode = lastNode.getPreviousNode();
|
||||
|
||||
if (lastNode == null)
|
||||
firstNode = null;
|
||||
else
|
||||
lastNode.setNextNode(null);
|
||||
return back;
|
||||
} // end removeBack
|
||||
|
||||
/** Retrieves the front/back entry of this deque.
|
||||
@return The object at the front/back of the deque.
|
||||
@throws EmptyQueueException if the deque is empty. */
|
||||
public T getFront()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new EmptyQueueException();
|
||||
else
|
||||
return firstNode.getData();
|
||||
} // end getFront
|
||||
|
||||
public T getBack()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new EmptyQueueException();
|
||||
else
|
||||
return lastNode.getData();
|
||||
} // end getBack
|
||||
|
||||
/** Detects whether this deque is empty.
|
||||
@return True if the deque is empty, or false otherwise. */
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return (firstNode == null && lastNode == null);
|
||||
} // end isEmpty
|
||||
|
||||
/* Removes all entries from this deque. */
|
||||
public void clear()
|
||||
{
|
||||
firstNode = null;
|
||||
lastNode = null;
|
||||
} // end clear
|
||||
|
||||
// @author Frank M. Carrano, Timothy M. Henry
|
||||
// @version 5.0 */
|
||||
private class DLNode
|
||||
{
|
||||
private T data; // Entry in bag
|
||||
private DLNode previous; // Link to previous node
|
||||
private DLNode next; // Link to next node
|
||||
|
||||
private DLNode(T dataPortion)
|
||||
{
|
||||
this(null, dataPortion, null);
|
||||
} // end constructor
|
||||
|
||||
private DLNode(DLNode previousDLNode, T dataPortion, DLNode nextDLNode)
|
||||
{
|
||||
data = dataPortion;
|
||||
previous = previousDLNode;
|
||||
next = nextDLNode;
|
||||
} // end constructor
|
||||
|
||||
private T getData()
|
||||
{
|
||||
return data;
|
||||
} // end getData
|
||||
|
||||
private void setData(T newData)
|
||||
{
|
||||
data = newData;
|
||||
} // end setData
|
||||
|
||||
private DLNode getNextNode()
|
||||
{
|
||||
return next;
|
||||
} // end getNextDLNode
|
||||
|
||||
private void setNextNode(DLNode nextDLNode)
|
||||
{
|
||||
next = nextDLNode;
|
||||
} // end setNextDLNode
|
||||
|
||||
private DLNode getPreviousNode()
|
||||
{
|
||||
return previous;
|
||||
} // end getPreviousDLNode
|
||||
|
||||
private void setPreviousNode(DLNode previousDLNode)
|
||||
{
|
||||
previous = previousDLNode;
|
||||
} // end setPreviousDLNode
|
||||
} // end DLNode
|
||||
} // end LinkedQueue
|
54
assignments/AssigmentS6/LinkedDequeDemo.java
Normal file
54
assignments/AssigmentS6/LinkedDequeDemo.java
Normal file
@ -0,0 +1,54 @@
|
||||
public class LinkedDequeDemo {
|
||||
public static void main(String[] args) {
|
||||
// Create a new LinkedDeque
|
||||
LinkedDeque<String> deque = new LinkedDeque<>();
|
||||
|
||||
System.out.println("=== Testing LinkedDeque Operations ===");
|
||||
System.out.println("Initial deque is empty: " + deque.isEmpty());
|
||||
|
||||
// Add elements to front and back
|
||||
System.out.println("\nAdding elements to front and back:");
|
||||
deque.addToFront("Front 1");
|
||||
deque.addToBack("Back 1");
|
||||
deque.addToFront("Front 2");
|
||||
deque.addToBack("Back 2");
|
||||
|
||||
// Display front and back
|
||||
System.out.println("Front element: " + deque.getFront());
|
||||
System.out.println("Back element: " + deque.getBack());
|
||||
|
||||
// Remove elements
|
||||
System.out.println("\nRemoving elements:");
|
||||
System.out.println("Removed from front: " + deque.removeFront());
|
||||
System.out.println("Removed from back: " + deque.removeBack());
|
||||
|
||||
// Display again
|
||||
System.out.println("\nAfter removal:");
|
||||
System.out.println("Front element: " + deque.getFront());
|
||||
System.out.println("Back element: " + deque.getBack());
|
||||
|
||||
// Test isEmpty
|
||||
System.out.println("\nDeque is empty: " + deque.isEmpty());
|
||||
|
||||
// Clear the deque
|
||||
System.out.println("\nClearing the deque...");
|
||||
deque.clear();
|
||||
System.out.println("Deque is empty after clear: " + deque.isEmpty());
|
||||
|
||||
// Test exception handling
|
||||
System.out.println("\nTesting exception handling:");
|
||||
try {
|
||||
System.out.println("Attempting to get front of empty deque...");
|
||||
System.out.println(deque.getFront());
|
||||
} catch (EmptyQueueException e) {
|
||||
System.out.println("Caught EmptyQueueException: " + e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
System.out.println("Attempting to remove from back of empty deque...");
|
||||
System.out.println(deque.removeBack());
|
||||
} catch (EmptyQueueException e) {
|
||||
System.out.println("Caught EmptyQueueException: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user