static Node C(int key, Node left, Node right) { return new Node(key, left, right); } static Node X(int key) { return C(key, null, null); } static Node L(int key, Node left) { return C(key, left, null); } static Node R(int key, Node right) { return C(key, null, right); } public static void main(String[] args) { Node n = C(8, C(3, X(1), C(6, X(4), X(7) ) ), R(10, L(14, X(13) ) ) ); Node current = n.getLeftMost(); while (current != null) { System.out.print(current.key + " "); current = current.getNext(); } }
Node item = l.getHeadNode(); while( item != null ) { // copy the item a.insertLast( item ); item = item.getNext(); }
get(int i) { Node current = head while (i > 0) { current = current.getNext() i-- } return current }
public int size() { int count = 0; if (base != null) { Node temp = base; do { temp = temp.getNext(); count++; } while (temp != base); } return count; }
public void DisplayLinkedList() { Node curr = list; while(curr != null) { System.out.println(curr); curr = curr.getNext(); } }
public void inspectQueue() { Node current = head; while(current != null) { System.out.println(n.getData()); current = current.getNext(); } }
public String print() { StringBuilder str = new StringBuilder(); Node curr = first; while (true) { str.append(curr.getData()); curr = curr.getNext(); if (curr == first) break; } return str.toString(); }
Node currentNode = n1; // start at your first node while(currentNode != null) { // do logic, for now lets print the value of the node System.out.println(currentNode.getData()); // proceed to get the next node in the chain and continue on our loop currentNode = currentNode.getNext(); }
public void reverse() { Node before = null; Node current = first; while (current != null) { Node next = current.getNext(); current.setNext(before); //// before = current; current = next; } first = before; }
public void inspectQueue() { Node n = head; while (n != null) { System.out.println(n.getData()); n = n.getNext(); } }
public void Search(String data){ Node tmp = head ; while(tmp != null ){ if(tmp.getData().equalsIgnoreCase(data)){ System.out.println("Your input is in the list"); } tmp = tmp.getNext(); } }
linkedList list = new linkedList(); list.add("foo"); list.add("bar"); Node tmp = lst.getHead(); while(tmp != null){ System.out.println(tmp.getData()); tmp = tmp.getNext(); }
// returns the number of nodes in the list public int Count(){ return CountHelper( head ); } // helper recursive function // returns the number of nodes in the list after and including "node". // call with head of the list to get the count of all nodes. private int CountHelper( Node node ) { if( node == null ) return 0; else return 1 + CountHelper( node.getNext() ); }
SLinkedList unionSet = null; // need a new SLinkedList() here Node cursor; for(cursor = head.getNext(); cursor != null; cursor = cursor.getNext()) { unionSet.addLast(cursor.getElement()); // NPE because unionSet is null }
public static void product(Node curr, int value) { if (curr == null) { return; } else { int data = value * curr.getData(); // compute current product curr.setData(data); // update Node product(curr.getNext(), data); // make recursive call } }
protected void insertNodeAfter(Node currentNode, Node newNode) { Node displacedNode = currentNode.getNext(); currentNode.setNext(newNode); newNode.setNext(displacedNode) displacedNode.setPrev(newNode); newNode.setPrev(currentNode); }