This is a Linked List object which can take any kind of input as a Node and organize it as an arrayList would organize it.
The Object inherits from the interfaces Stack and Queue, written by professor Adam Smith, provided at the bottom!
Enjoy!
/**
* Linked List object which implements the Stack and Queue interfaces, allowing users to create
* a linked list of Nodes
*
* @version 1.0
* @author AidanTakami
*
*/
public class LinkedList implements Queue, Stack{
//Fields
public Node head;
public Node tail;
public int size;
//*************************
//LinkedList
//*************************
/**
* The constructor for the LinkedList which takes the head and the tail Node
* @param Node head
* @param Node tail
* @since 1.0
*/
public LinkedList(Node head, Node tail){
this.head = head;
this.tail = tail;
this.head.setNext(this.tail);
size = 2;
}
/**
* adds the E value to the head of the LinkedList
*
* @param value the value to be added to the tail
* @since 1.0
*/
public void addtoHead(E value){
Node newNode = new Node(value);
newNode.setNext(head);
head = newNode;
size++;
}
/**
* adds the E value to the tail of the LinkedList
*
* @param value the value to be added to the tail
* @since 1.0
*/
public void addToTail(E value){
Node newNode = new Node(value);
tail.setNext(newNode);
tail = newNode;
size++;
}
/**
* Checks to see if the specified value is contained
*
* @param value the value to be checked for within the LinkedList
* @return boolean whether or not the value is contained
* @since 1.0
*/
public boolean contains(E value){
//if head == value, return true
if(head.getData().equals(value)) return true;
Node inspect = head.getPointer();
//otherwise
for(int rep = 1; rep 0){
Node oldHead = head;
head = oldHead.getPointer();
size--;
return (E )oldHead.getData();
}
else return null;
}
/**
* toString method for the LinkedList
*/
@Override
public String toString(){
//creates string to be returned
String listString = (" " + head.toString());
//creates node to be changed and cycle through LinkedList
Node subject = head;
//loop which adds each next node to the String
for(int rep = 0; rep " + subject + " ");
}
}
//returns the String
return listString;
}
//*************************
//Queue Methods
//*************************
/**
* sets the head to the 2nd item in LinkedList and then returns the data
* of the previous head
*
* @since 1.0
*/
public E dequeue(){
Node oldHead = head;
head = oldHead.getPointer();
size--;
return (E )oldHead.getData();
}
/**
* Adds a Node to the end of the queue and sets that as the new tail
*
* @param Object value the object to be added to the end
* @since 1.0
*/
public void enqueue(Object value){
Node addOn = new Node(value);
tail.setNext(addOn);
tail = tail.getPointer();
size++;
}
/**
* Peeks at the head of the Queue or Stack
*
* @since 1.0
*/
public E peek(){
return (E )head.getData();
}
//*************************
//Stack Methods
//*************************
/**
* removes an item from the head of the Stack, and returns it
*
* @return the value removed
*/
public E pop(){
Node oldHead = head;
head = head.getPointer();
size--;
return (E )oldHead.getData();
}
/**
* adds an item to the top of the Stack
*
* @param value the value to be added
*/
public void push(Object value){
Node newHead = new Node(value);
newHead .setNext(head);
head = newHead;
size++;
}
//*************************
//Node Object
//*************************
/**
* Node object which contains a constructor method and a toString.
* Node holds an E value as well as a pointer to the next node in a LinkedList.
*
* @author AidanTakami
* @param
* @since 1.0
*/
public static class Node{
//Field
E data;
Node nextNode = null;
/**
* Constructor for the Node object. takes the data which will be stored in the Node.
* @param data
* @since 1.0
*/
public Node(E data){
this.data = data;
}
/**
* toString Method for the Node object.
*
* @since 1.0
*/
@Override
public String toString(){
return "(" + data + ")";
}
/*
* Returns the Node nextNode of the Node, which is the next Node in the sequence
*/
private Node getPointer(){
return nextNode;
}
/*
* Returns the data of the Node
*/
private E getData(){
return data;
}
/*
* Sets the nextNode of the node, only to be accessible by the LinkedList object
*/
private void setNext(Node next){
nextNode = next;
}
}
/**
* This is an interface for a Queue. An object which implements this interface
* is thus a "first in, first out", or "FIFO" structure. It can enqueue to the
* end, dequeue from the front, or peek at the front.
*
* @author Adam Smith
* @version 1.0
*/
public interface Queue {
/**
* Removes an item from the front of the queue.
*
* @return the value removed
*/
public E dequeue();
/**
* Adds an item to the end of the queue.
*
* @param value the value to be enqueued
*/
public void enqueue(E value);
/**
* Checks item at the front of the queue, without altering the queue.
*
* @return the value checked
*/
public E peek();
}
/**
* This is an interface for a Stack. An object which implements this interface
* is thus a "first in, last out", or "FILO" structure. It can push to the top,
* pop from the top, or peek at the top.
*
* @author Adam Smith
* @version 1.0
*/
public interface Stack {
/**
* Checks item at the top of the stack, without altering the stack.
*
* @return the value checked
*/
public E peek();
/**
* Removes an item from the top of the stack.
*
* @return the value removed
*/
public E pop();
/**
* Adds an item to the top of the stack.
*
* @param value the value to be enqueued
*/
public void push(E value);
}