diff --git a/LinkedList.java b/LinkedList.java index 0e26ea8..bf883b7 100644 --- a/LinkedList.java +++ b/LinkedList.java @@ -1,120 +1,132 @@ - class ListNode{ - + private int data; private ListNode next; - + public ListNode(int data){ + this.data = data; + } - + public void setData(int data){ - this.data = data; + + this.data = data; + } - + public int getData(){ return data; } - public void setNext(ListNode next){ - - this.next = next; - - } - + public ListNode getNext(){ return this.next; } - + + public void setNext(ListNode next){ + this.next = next; + } + + } + public class LinkedList{ - int length; + public int length; + public LinkedList(){ length = 0; } - + ListNode head; - - //return head - public synchronized ListNode getHead(){ - + public ListNode getHead(){ return head; - } - - //insert at the beginning position - public synchronized void insertAtBegin(ListNode node){ - //if head is null, we have no list - node.setNext(head); - head = node; - length++; - + + public synchronized void insertAtBeginning(ListNode node){ + + if(head == null){ + head = node; + } + else{ + node.setNext(head); + head = node; + } + length += 1; + } - - //insert at the last position + public synchronized void insertAtEnd(ListNode node){ - if(head == null){ + head = node; - }else{ - ListNode p , q; + } + else{ + ListNode p , q ; for(p = head ; (q = p.getNext()) != null ; p = q); p.setNext(node); - + } - length++; - + length += 1; } - - - - //insert at random position - public synchronized void insert(int data , int position){ - - //to validate if position is valid or not + + public synchronized void insert(int data){ + ListNode node = new ListNode(data); + if(head == null){ + head = node; + + } + else{ + ListNode p , q ; + for(p = head ; (q = p.getNext()) != null ; p = q); + p.setNext(node); + + } + length += 1; + + + } + + public synchronized void insertAtPosition(int data, int position){ + if(position < 0){ position = 0; } if(position > length){ position = length; } - - if(head == null){ - head = new ListNode(data); - - } - - - - ListNode temp = head; - - - for(int i = 1 ; i < position ; i++){ - - temp = temp.getNext(); - - } - ListNode node = new ListNode(data); - node.setNext(temp.getNext()); - temp.setNext(node); - + if(position == 0){ + + insertAtBeginning(node); + + }else{ + ListNode p = head; + for(int i = 1 ; i < position ; i++){ + p = p.getNext(); + + } + node.setNext(p.getNext()); + p.setNext(node); + + } + + length += 1; + } - - //remove the first node in list - public synchronized ListNode removeFromBegin(){ + + public synchronized ListNode removeFromFirst(){ - ListNode node = head; - if(node != null){ - head = node.getNext(); - node.setNext(null); - length--; + if(head == null) { + return null; } - return node; - + + ListNode p = head; + head = p.getNext(); + length -= 1; + return p; } - - //remove the last node in list + public synchronized ListNode removeFromLast(){ if(head == null){ @@ -123,112 +135,142 @@ public synchronized ListNode removeFromLast(){ ListNode p = head , q = null , next = head.getNext(); if(next == null){ head = null; + length -= 1; return p; + } - while((next = p.getNext()) != null){ q = p; p = next; + } + q.setNext(null); length -= 1; return p; - } - // remove the matching node - public synchronized void removeMatched(ListNode node){ - // if head is null, do nothing - if(head == null){ - return; - } - //if head is not null, check if head itself the node; - if(node.equals(head)){ - - head = head.getNext(); - return; - } - //else check if next or any of the node after head is the node - ListNode p = head , q = null ; - while((q = p.getNext()) != null){ - if(node.equals(q)){ // we meet the node here - p.setNext(q.getNext()); // setting its previous node to point to matched node's node - length -= 1; - return; - } - p = q; // to move ahead in the list , if the current nod is not the node - } - + } - - // remove at any position - public void remove(int position){ + + public synchronized ListNode removeAtPosition(int position){ if(position < 0){ position = 0; } if(position > length){ + position = length - 1; + } - + ListNode p = head; if(position == 0){ - - head = head.getNext(); - return; - + head = p.getNext(); + length -= 1; + return p; } else{ - ListNode p = head; - //loop till we reach a position one before the acutal element + for(int i = 1 ; i < position ; i++){ - p = p.getNext(); + p = p.getNext(); } + ListNode q = p.getNext(); p.setNext(p.getNext().getNext()); - - + length -= 1; + return q; } - length -= 1; - } - public String toString(){ - String result = "["; + public synchronized void removeNode(ListNode node){ + if(head == null){ - result += "]"; - + return; } + ListNode p = head , q = null; - ListNode temp = head; - while(temp != null){ - + if(node.equals(head)){ + head = head.getNext(); + length -= 1; + return; + } + + while((q = p.getNext()) != null){ + if(node.equals(q)){ + p.setNext(q.getNext()); + length -= 1; + return; + } + p = q; + } + } + + + + public String toString(){ + String result = "["; + ListNode p = head, q = null; + result += p.getData(); + while((q = p.getNext()) != null){ - result += temp.getData(); - if(temp.getNext() != null){ + if(p.getNext() != null){ result += " , "; } - temp = temp.getNext(); - + result += q.getData(); + p = q; } result += "]"; return result; } - - - public static void main(String[] args){ - - LinkedList ll = new LinkedList(); - ListNode node = new ListNode(10); - ll.insertAtBegin(node); - System.out.println(ll.toString()); - ll.insert(20, 1); - System.out.println(ll.toString()); - - } - - - + + public static void main(String[] args){ + + ListNode node = new ListNode(10); + LinkedList ls = new LinkedList(); + ls.insertAtBeginning(node); + System.out.println(ls.toString()); + ls.insert(20); + + System.out.println(ls.toString()); + + ls.insert(30); + + System.out.println(ls.toString()); + + ls.insert(40); + + System.out.println(ls.toString()); + + ls.insertAtBeginning(new ListNode(100)); + + System.out.println(ls.toString()); + + ls.insertAtEnd(new ListNode(200)); + + System.out.println(ls.toString()); + + ls.removeFromFirst(); + + System.out.println(ls.toString()); + + ls.removeFromLast(); + + System.out.println(ls.toString()); + + ls.removeAtPosition(2); + + System.out.println(ls.toString()); + + ls.insertAtPosition(240,2); + + System.out.println(ls.toString()); + + ls.removeNode(new ListNode(240)); + + System.out.println(ls.toString()); + + + } } -