public void deleteitem(int element) { Node temp; // ref:http://web.cse.ohio-state.edu/~reeves/CSE2421sp13/lab3linklistCOD temp = head; while (temp != null) // while LL is not empty { if (element == temp.data) { if (temp == head) // deleting the first node { head = head.next; temp.next.prev = null; temp.next = null; } else if (end == temp) // is it end { temp.prev.next = null; end = temp.prev; temp.prev = null; } else // if not first then { // while(head.next != temp) // { temp.prev.next = temp.next; temp.next.prev = temp.prev; temp.next = null; temp.prev = null; // } } } temp = temp.next; // traversing through the LL by pointing to next ref } }
public void final_add(int element) { // tried using bubble sort after adding the new node but didnot worked so checked manually Node n; Node temp_node; temp_node = head; if (head == null) { n = new Node(element, null, null); head = n; end = n; } else if (head == end) { if (element <= head.data) { n = new Node(element, temp_node, temp_node.prev); temp_node.prev = n; head = n; } else { n = new Node(element, temp_node.next, temp_node); temp_node.next = n; end = n; } } else if (head != end) { if (element <= head.data) { n = new Node(element, temp_node, temp_node.prev); temp_node.prev = n; head = n; } else if (element > end.data) { n = new Node(element, end.next, end); end.next = n; end = n; } else { while (temp_node != null) { if (element <= temp_node.data) { n = new Node(element, temp_node, temp_node.prev); temp_node.prev.next = n; temp_node.prev = n; } temp_node = temp_node.next; } } } }
public void deletingfirst() { Node temp; temp = head; if (head == end) { end = null; head = null; } else if (head != end) { head = temp.next; temp.next.prev = null; temp.next = null; temp.prev = null; } }