diff --git a/Implementation/SinglyLinkedList.java b/Implementation/SinglyLinkedList.java index aa43300..1ca6771 100644 --- a/Implementation/SinglyLinkedList.java +++ b/Implementation/SinglyLinkedList.java @@ -17,287 +17,191 @@ class * * */ - public class Node { - public Object item; - public Node next; - } - /** - * LinkedList.java - * - **/ - public class LinkedList { - private int size; - private Node head; - public LinkedList() { - this.size = 0; - this.head = null; - } - -/** - * Inserting new node at the end of the linked list - * - * @param item - represent the node item to be added to the linked list - */ - public void insertNode(String item) { - Node node = new Node(); - node.item = item; - Node current = this.head; - - if (this.head == null) { - this.head = node; - this.head.next = null; - this.size = 1; - System.out.println(this.head.toString()); - } else { - - while (current.next != null) { - current = current.next; - } - current.next = node; - node.next = null; - this.size += 1; - } - } - - /** - * Adding node at the first location of the linked list - * - * @param item - represent item of the node to be added to LL - */ - public void insertFirst(String item){ - Node node = new Node(); - node.item = item; - node.next = this.head; - this.head = node; - this.size++; - } - - /** - * Adding node at the nth location of the linked list - * - * @param item - represent the item of the node to be added to the list - * @param position - position at which the node is to be added - */ - public void insertNth(String item, int position) { - Node node = new Node(); - node.item = item; - Node current = this.head; - if (this.head != null && position <= this.size) { - for (int i = 1; i < position; i++) { - current = current.next; - } - node.next = current.next; - current.next = node; - this.size += 1; - }else{ - System.out.println("Exceeded the linked list size. Current Size: "+size); - } - } - - /** - * Deleting the first node from the list - */ - public void deleteFirstNode() { - if(head != null){ - this.head = this.head.next; - this.size--; - }else{ - System.out.println("Linked list is empty"); - } - } - - /** - * Deleting the last node from the list - */ - public void deleteLastNode() { - Node currentNode = this.head; - if(size == 1){ - head = null; - size = 0; - }else{ - Node prevNode = null; - while (currentNode.next != null) { - prevNode = currentNode; - currentNode = currentNode.next; - } - prevNode.next = null; - this.size--; - } - } - - /** - * Deleting the node from the nth location in the list - * - * @param position - location of the node to be deleted - */ - public void deleteNthNode(int position) { - if (position <= this.size && this.head != null) { - Node currentNode = this.head; - Node prevNode = null; - for (int i = 0; i < position; i++) { - prevNode = currentNode; - currentNode = currentNode.next; - } - prevNode.next = currentNode.next; - this.size--; - }else{ - System.out.println("No node exist at location: "+position); - } - } - - /** - * Find if the node exist in the list - * - * @param item - item to be found in the list - * - */ - public void findNode(String item) { - Node node = this.head; - boolean found = false; - for(int i=0;i"); + temp=temp.next; + } + System.out.println("null"); + } + public static int delete(){ + node temp=head; + if(size==0){ + System.out.println("list is empty"); + return -1; + } + for(int i=0;isize){ + System.out.println("error!!!!!"); + return -1; + } + for(int i=0;i