Linked list example:
// This version uses the "null" values >
/*
*/
package javaapplication4;
/**
*/
public class JavaApplication4 {
public static class MyLinkedList {
private class Node {
private int value;
private Node next;
private Node(int value) {
this.value = value;
this.next = null;
}
}
private Node first = null;
private Node last = null;
private int size = 0;
/**
* The linked list constructor
*/
public MyLinkedList() {
this.size = 0;
this.first = null;
}
/**
* This function appends the new element of the list
* (to the end of the list)
* @param i element to be added
*/
public void add(int i) {
Node n = new Node(i);
if (size == 0) {
this.first = n;
this.last = n;
} else {
// the append itself
this.last.next = n;
this.last = n;
}
size++;
}
// add last - another version
public void addlast(int i) {
Node n = new Node(i);
if (size == 0) {
this.first = n;
this.last = n;
} else {
Node curr = this.first;
while (curr.next != null) {
curr = curr.next;
}
curr.next = n;
this.last = n;
}
size++;
}
/**
* Returns element at the index "i"
* @param i the index
* @return the element at the index
*/
public int get(int i) {
if (i >= size) {
throw new IndexOutOfBoundsException("Outside list, index: "
+ i + ", the maximim is:" + this.size-1);
}
if (i < 0) {
throw new IllegalArgumentException("Index must be >= 0");
}
Node curr = first;
for (int j=0; j < i; j++) {
curr = curr.next;
}
return curr.value;
}
/**
* This will erase element on the index "i"
* @param i index of the element to be deleted
*/
public int remove(int i) {
int lr = 0;
if (i >= size) { throw new IndexOutOfBoundsException("Outside list, index: "
+ i + ", the maximim is:" + this.size-1); }
if (i < 0) { throw new IllegalArgumentException("Index less then 0");
}
if (i == 0) {
lr = first.value;
first = first.next;
} else {
Node curr = first;
for (int j=0; j < i-1; j++) { //find the previou one
curr = curr.next;
}
lr = curr.value;
curr.next = curr.next.next; //skip the required one
if (i == size-1) { //... if it was the last
last = curr;
}
}
size--;
return lr;
}
// remove last:
public int removelast() {
int lr = 0;
Node curr = first;
while (curr.next.next != null) {
curr = curr.next;
}
lr = curr.next.value;
curr.next = null;
this.last = curr;
size--;
return lr; //a temporaly variable must be used,
// otherwise value would be already erased (a feature of Java)
}
/**
* Just getter to read the size of the list
* @return the list size
*/
public int size() {
return this.size;
}
/**
* A new ToString returning a text representation of the list
*
* @return a String containing all the elements
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Node curr = first;
while (curr != null)
{
builder.append(curr.value);
builder.append(", ");
curr = curr.next;
}
return builder.toString();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MyLinkedList l = new MyLinkedList();
l.addlast(0);
l.add(5);
l.addlast(10);
l.add(15);
l.addlast(20);
System.out.println("Removed: "+l.removelast());
l.addlast(31);
l.add(33);
l.add(35);
l.addlast(37);
l.addlast(39);
//print the list using modified ToString method:
System.out.println(l);
System.out.println(l.get(2)); //20
l.remove(1); //sample of use
System.out.println(l.get(2)); //30
l.remove(0);
System.out.println(l);
System.out.println(l.size()); //3
}
}