Skip to content

Instantly share code, notes, and snippets.

@jakswa
Created February 8, 2010 20:42
Show Gist options
  • Save jakswa/298554 to your computer and use it in GitHub Desktop.
Save jakswa/298554 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
//Jake Swanson
//Doubly linked list example
class Node { // Node.java
//tests node class
public static void main(String [] args) {
final int LIST_LENGTH = 10;
//variables for test
Node [] nodeArray = new Node[LIST_LENGTH];
for (int i = nodeArray.length-1;i>=0;i--) nodeArray[i] = new Node((int)(Math.random() * 100), (i!=nodeArray.length-1)?nodeArray[i+1]:null);
for (int i = 1;i<nodeArray.length;i++) nodeArray[i].setPrev(nodeArray[i-1]);
Node head = nodeArray[0], curr = head;
Scanner myscan = new Scanner(System.in);
Node.printA(head); //see starting list
//testing insert
Node forInsert = new Node((int)(Math.random()*100));
System.out.print("\nnew node of value " + forInsert.getItem() + "\ntell me where to insert: ");
int pos = myscan.nextInt();
head = Node.insertByVal(head, forInsert, pos);
Node.printA(head); //see updates
//testing deletion
System.out.print("\ntell me a node value to delete: ");
pos = myscan.nextInt();
head = Node.deleteByVal(head, pos);
Node.printA(head); //see updates
Node.printR(head); //see reverse
}
//##### NODE CLASS ###
//doubly linked list
//getters and setters
private int item; private Node next, prev;
public Node(int newItem) { this(newItem, null,null); } // end constructor
public Node(int newItem, Node nextNode) { this(newItem, nextNode, null); } // end constructor
public void setItem(int newItem) { item = newItem; } // end setItem
public int getItem( ) { return item;} // end getItem
public void setNext(Node nextNode) { next = nextNode; } // end setNext
public Node getNext( ) { return next; } // end getNext
//doubly linked
public Node(int newItem, Node nextt, Node prevv) { item = newItem; next = nextt; prev = prevv; }
public void setPrev(Node prevv) { prev = prevv; }
public Node getPrev() { return prev; }
//Print Ahead
public static void printA(Node head) { while (head != null) {
System.out.print(head.getItem() + ((head.getNext() != null)?" ":"\n"));
head = head.getNext(); }
}
//Print reverse. Gets tail if not gotten.
public static void printR(Node head) {
System.out.print("rev: ");
for (;head.getNext() != null; head = head.getNext()); // moves to end of list if not there
while (head != null) {
System.out.print(head.getItem() + ((head.getPrev() != null)?" ":"\n"));
head = head.getPrev();
}
}
//insert node by value
public static Node insertByVal(Node head, Node input, int val) { //inserts node at index. moves current node there ahead by one.
if (head.getItem() == val) { //special case, insert at head
input.setNext(head);
head.setPrev(input);
head = input;
return head;
}
Node bob = head; // all other cases
for (; bob.getNext() != null && bob.getNext().getItem() != val;bob= bob.getNext()); //puts bob at needed spot
input.setNext(bob.getNext()); //rest of this does insertion
if (input.getNext() != null) input.getNext().setPrev(input); //test for end of list
bob.setNext(input);
input.setPrev(bob);
return head;
}
//delete a value from list
public static Node deleteByVal(Node head, int val) {
Node bob = head;
if (head.getItem() == val) {
head.getNext().setPrev(null); //special case, delete head
return head.getNext(); //return the 2nd item as the new 'head'
} else {
for (; bob.getNext().getItem() != val;bob = bob.getNext()) //positions bob
if (bob.getNext().getNext() == null ) { //this runs if it reaches end of list
System.out.println("item not present");
return head;
}
}
bob.setNext(bob.getNext().getNext());
if(bob.getNext() != null) bob.getNext().setPrev(bob); //tests for null (at end of list)
return head;
}
} // end class Node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment