/*
* Basal linked list class
*
* Constructor :
* LinkedList()
*
* Including method :
* add(int, Object), addCount(int), addToHead(Object), addToTail(Object),
* count(), get(int), getHead(), getNode(int), getTail(), isEmpty(),
* remove(int), removeFromHead(), removeFromTail(), setHead(Node),
* set(Tail(Node), toString()
*
* Gary Wong
*
*/
LinkedList.java
Node.java
public class LinkedList{
protected Node head, tail; // Two Node store which is head & tail.
protected int count; // Store the count value.
// Constructor of LinkedList
public LinkedList(){
head = tail = null; // Initialization head & tail to null.
count = 0; // Initialization count to zero.
}
// isEmpty method.
public int isEmpty(){
return count == 0; // if count is zero return true.
}
// addToHead method.
public void addToHead(Object item){
if(isEmpty()){
// if it's empty list set head & tail to same new node.
head = tail = new Node(item,null);
}else{
/* Create new Node, the new node next node point to current head.
* Then set head to new Node.
*/
head = new Node(item,head);
}
// Increases count value.
count++;
}
// addToTail method.
public void addToTail(Object item){
if(isEmpty()){
// if it's empty list set head & tail to same new node.
head = tail = new Node(item,null);
}else{
/* Create new Node, the new node next node is null.
* Set currentTail next node to new Node.
* Then set tail to new Node.
*/
tail.setNextNode(new Node(item,null));
tail = tail.getNextNode();
}
// Increases count value.
count++;
}
// removeFromHead method.
public Object removeFromHead(){
if(isEmpty()){
// if it's empty list return null data.
return null;
}else{
// Store current head data to removeData
Object removeData = head.getData();
if(head == tail){
// Set head & tail to null.
head = tail = null;
// Set count to zero.
count = 0;
}else{
// Set head to current head next node.
head = head.getNextNode();
// Decrease count value
count--;
}
// return remove node data.
return removeData;
}
}
// removeFromTail method.
public Object removeFromTail(){
if(isEmpty()){
// if it's empty list return null data.
return null;
}else{
// Store current tail data to removeData
Object removeData = tail.getData();
if(head == tail){
// Set head & tail to null.
head = tail = null;
// Set count to zero.
count = 0;
}else{
// Use getNode method found the tail previous node.
getNode(count()).setNextNode(null);
// Set tail to previous node.
tail = getNode(count()-1);
// Decrease count value
count--;
}
// return remove node data.
return removeData;
}
}
// count method.
public int count(){
// return count value.
return count;
}
// getHead method.
public Node getHead(){
// return head node.
return head;
}
// getTail method.
public Node getTail(){
// return tail node.
return tail;
}
// add method.
public void add(int insertPoint, Object item){
if(insertPoint == 1){
// if insert point is 1, then add new node to head.
addToHead(item);
// Not increases count value, because in addToHead method will do.
}else if(insertPoint >= count()){
// if insert point more than list count, then add new node to tail.
addToTail(item);
// Not increases count value, because in addToTail method will do.
}else{
// if insert point between head & tail,
// Use getNode method found the insert point previous node.
// Create new node & new node next node is insert point node.
// Set previous node next node to new node.
getNode(insertPoint-1).setNextNode(new Node(item, getNode(insertPoint)));
// Increases count value.
count++;
}
}
// get method.
public Object get(int index){
// Use getNode method get the target node, then return target node data.
return (getNode(index) == null)? null : getNode(index).getData();
}
// getNode method.
public Node getNode(int index){
if(isEmpty() || index > count() || index < 1){
// if it's empty list or target location not exist, return null.
return null;
}else if(index == 1 ){
// if target location is 1, return head node.
return head;
}else if(index == count()){
// if target location equal list count, return tail node.
return tail;
}else{
// if target location between head & tail,
// Create currentNode point to head.
Node currentNode = head;
// Use for loop found the target node.
for(int i = 2; i <= index; i++){
currentNode = currentNode.getNextNode();
}
// return target node.
return currentNode;
}
}
// remove method.
public Object remove(int index){
if(isEmpty() || index > count()){
// if it's empty list or target location not exist, return null.
return null;
}else if(index == 1 ){
// if target location is 1, use removeFromHead method.
return removeFromHead();
}else if(index == count()){
// if target location equal list count, use removeFromTail method.
return removeFromTail();
}else{
// Create removeData store target node data.
Object removeData = get(index);
// Use getNode method to getting target next node.
// Use getNode method to getting target previous node.
// Set target target previous next node to target next node.
getNode(index-1).setNextNode(getNode(index+1));
// Decrease count value
count--;
// return remove node data.
return removeData;
}
}
// toString method.
public String toString(){
// Create output variable store data prepare output, initialize to [.
String output = "[";
// Use for loop put all data between head to tail previous node into output.
for(int i = 1; i < count(); i++){
output += get(i) + ", ";
}
// return output and tail data with closing symbol ].
return output + tail.getData() + "]";
}
}
List Node Object
public class Node {
private Object nodeData;
private Node nextNode;
public Node(){
this(null,null);
}
public Node(Object nodeData){
this(nodeData, null);
}
public Node(Object nodeData, Node nextNode){
this.nodeData = nodeData;
this.nextNode = nextNode;
}
public Object getData(){
return nodeData;
}
public Node getNextNode(){
return nextNode;
}
public void setData(Object newData){
this.nodeData = newData;
}
public void setNextNode(Node nextNode){
this.nextNode = nextNode;
}
}
0 comments:
Post a Comment