I want to implement a method that deletes all occurences of a certain value given as argument.
I created 2 temporary elements and referenced them to the head and I will be browsing the list using them. I was able to write a whole code but I’m not getting any result, I don’t know where the problem is, neither I am getting an error.
Any help would be appreciated.
This is the method (my approach):
public void deleteAllOccurrences(int value) { Element cur = this.head; Element prev = null; //deleting head while(cur != this.rear && cur.data == value) { cur.next = this.head; cur = this.head; } while(cur != this.rear) { while(cur != this.rear && cur.data != value) { prev = cur; cur = cur.next; } if(cur == this.rear) return; prev.next = cur.next; cur = prev.next; } }
An this is the whole class maybe you need to check something:
public class CircularLinkedList { class Element{ int data; // int type used as example Element next; // reference of the successor Element(int value) { this.data = value; this.next = this; } } private Element head = null; private Element rear = null; public CircularLinkedList() { this.head = this.rear = null; } public boolean isEmpty() { return head == null; } public boolean findValue(int value) { Element cur = this.head; while(cur != null) { if (cur.data == value) return true; cur = cur.next; } return false; } public int countValue(int value) { int c = 0; // counter Element cur = this.head; if(cur == null) return 0; do { if(cur.data == value) c++; cur = cur.next; }while (cur != this.head); return c; } @Override public String toString() { String str = ""; Element cur = this.head; if(cur == null) return "The list is empty"; do { str += cur.data + " | "; cur = cur.next; }while (cur != this.head); return str; } public void insert(int value) { Element tmp = new Element (value); //special case: empty list if(this.head == null) { this.head = tmp; }else { // general case tmp.next = this.head.next; this.head.next = tmp; } } public void deleteAtHead() { if(this.head == null) { return; } else { if(this.head != this.rear) { this.head = this.head.next; this.rear.next = this.head; } else { this.head = this.rear = null; } } } public boolean delete(int value) { Element cur = this.head; if(this.head.data == value) { //if the node to be deleted is head node while(cur.next != this.head) { //iterate till the last node i.e. the node which is pointing to head cur = cur.next; } cur.next = cur.next.next; // update current node pointer to next node of head this.head = this.head.next; //update head node return true; } else { // if node to be deleted is other than head node Element prev = cur; // track previous node from current (node) while(cur.data != value) { // find the node prev = cur; cur = cur.next; } prev.next = cur.next; //updating next field of previous node to next of current node.current node deleted return true; } } public void deleteEven() { // if(this.head == null) // return; // // //case of deleting the head // if(this.head.data % 2 == 0) { // this.head.next = this.head; // this.rear.next = this.head; // if(this.head == null) // this.rear = null; // } // // Element cur = this.head; // Element prev = cur; // while(cur != this.head) { // prev = cur; // cur = cur.next; // } // prev.next = cur.next; if(this.head == null) return; Element cur = this.head; while(cur != this.rear) { if(cur.data % 2 == 0) this.delete(cur.data); cur = cur.next; } } public void deleteLastOccurence(int value) { Element cur = this.head; Element prev = null; Element tmp = null; if(this.head == null) return; if(this.head.data == value) { this.head = null; return; } while(cur != this.rear) { if(cur.next != null && cur.next.data == value) { prev = cur; tmp = cur.next; } cur = cur.next; } prev.next = tmp.next; } public void deleteAllOccurrences(int value) { Element cur = this.head; Element prev = null; //deleting head while(cur != this.rear && cur.data == value) { cur.next = this.head; cur = this.head; } while(cur != this.rear) { while(cur != this.rear && cur.data != value) { prev = cur; cur = cur.next; } if(cur == this.rear) return; prev.next = cur.next; cur = prev.next; } } // public CircularLinkedList union(CircularLinkedList a, CircularLinkedList b) { // // } // // // public CircularLinkedList inter(CircularLinkedList a, CircularLinkedList b) { // // } public int countOddNbrs() { if(this.head == null) return 0; int c = 0; Element cur = this.head; do { if(cur.data % 2 != 0) c++; cur = cur.next; }while(cur != this.head); return c; } // public int findLastOccurence(int value) { // // } public static void main(String[] args) { CircularLinkedList list = new CircularLinkedList(); list.insert(8); list.insert(2); list.insert(4); list.insert(3); list.insert(10); list.insert(5); list.insert(-8); list.insert(4); System.out.println(list); // System.out.println(list.findValue(2)); // working // list.delete(2); // working // System.out.println(list); // System.out.println(list.countOddNbrs()); //working // list.deleteEven(); // not working // System.out.println(list); // list.deleteAtHead(); // not working // System.out.println(list); // list.deleteLastOccurence(4); //not working // System.out.println(list); list.deleteAllOccurrences(4); System.out.println(list); } }
Advertisement
Answer
your method is running to an infinite loop. Try something like this:
public void deleteAllOccurrences(int value) { Element cur = this.head; Element next = null; if (cur.data == value) { cur = cur.next; this.head = cur; } do { next = cur.next; if (next.data == value) { cur.next = next.next; } cur = next; } while (cur != this.head); }
You should handle the corner cases too when the head is the removable element.