Wednesday, September 3, 2014

Simple Linked List Using Java

public class List {
List next;
int data;

List(int d) {
data = d;
next = null;
}

void appendData(int d) {
List l = new List(d);
List n = this;
while(n.next != null) {
n = n.next; 
}
n.next = l;
}

void printData() {
List n = this;
for(; n.next != null; n = n.next)
System.out.println(n.data);
System.out.println(n.data);
System.out.println("-END-");
}

void appendMiddle(int d, int position) {
List n = this;
for(int i=1; i<position; i++)
n=n.next;
List l = new List(d);
l.next = n.next;
n.next = l;
}

void deleteEnd() {
List n = this;
for(; n.next.next!=null; n=n.next) {}
n.next = null;
}

void deleteMiddle(int possition) {
List n = this;
for(int i=1; i<possition; i++)
n=n.next;
List a = n.next;
n.next = n.next.next;
a = null;
}
}
UA-39217154-2