Double linked list

Run Settings
LanguageJava
Language Version
Run Command
// add a method remove() to the linked list that deletes a node to the specified index. class Node{ Node next; int data; Node prev; Node(int data){ this.data = data; next = null; } } class LinkedList { Node tail; Node head; int length=0; void append(int value) { Node node = head; Node new_node = new Node(value); // System.out.println("data is="+node); if(node==null){ prepend(value); }else{ while(node.next!=null){ node = node.next; } node.next = new_node; new_node.next = null; length++; } //return this; } void prepend(int value) { Node lastNode = new Node(value); lastNode.next = head; head = lastNode; length++; } void printList() { System.out.println("The list is printed"); Node node = head; while(node!=null){ System.out.println(node.data); node = node.next; } //return array; } void insert(int index,int value){ //Check for proper parameters; if(index>length){ System.out.println("index not found"); } else{ Node new_node = new Node(value); Node prevNode = traverseToIndex(index-1); Node nextNode = traverseToIndex(index); prevNode.next = new_node; new_node.next = nextNode; length++; //return this.append(value); } } Node traverseToIndex(int index) { //Check parameters int counter = 0; Node currentNode = head; while(counter != index){ currentNode = currentNode.next; counter++; } return currentNode; } void reversinglist(int index){ int counter =0; Node node = head; while(counter<index){ node = node.next; counter++; } node.next=null; } void remove(int index) { // Check Parameters Node old_node = traverseToIndex(index-1); Node new_node = traverseToIndex(index+1); old_node.next = new_node; length--; //return this.printList(); } void reverse_linkedlist(){ Node first = head; Node second = first.next; second.next = first; System.out.println("the second data is==="+second); // while(second!=null){ // Node temp = second.next; // second.next = first; // first = second; // second = temp; // //System.out.println("The data is ==="+second); // } } public static void main(String[] args){ LinkedList myLinkedList = new LinkedList(); myLinkedList.append(5); myLinkedList.append(16); myLinkedList.prepend(1); //myLinkedList.printList(); //System.out.println("the length is =="+myLinkedList.size()); myLinkedList.insert(2, 99); myLinkedList.insert(3, 88); myLinkedList.insert(10,101); //myLinkedList.remove(2); myLinkedList.printList(); myLinkedList.reverse_linkedlist(); myLinkedList.printList(); } }
Editor Settings
Theme
Key bindings
Full width
Lines