Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void deleteNode(int location) {
- if (head == null) {
- System.out.println("The CSLL does not exist! ");
- return;
- } else if (location == 0) { //delete element from the beginning
- head = head.next; //more than one element
- tail.next = head;
- size--;
- if (size == 0) { // only one element in the CSLL
- tail = null;
- head.next = null;
- head = null;
- }
- } else if (location >= size) { //delete element from the end
- Node tempNode = head;
- for (int i = 0; i < size - 1; i++) {
- tempNode = tempNode.next;
- }
- // go to the end
- if (tempNode == head) { // has only one element
- head.next = null;
- tail = head = null;
- size--;
- return;
- } //else we have more than one element
- tempNode.next = head;
- tail = tempNode;
- size--;
- //we added at the end
- } else {
- //deleting node from any location
- Node tempNode = head; //looping from the start
- for (int i = 0; i < location - 1; i++) { //before location
- tempNode = tempNode.next; //
- }
- tempNode.next = tempNode.next.next;
- size--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement