Skip to content

Commit

Permalink
added circular linked list variant1
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Jan 10, 2019
1 parent d253bf0 commit a0aa49e
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 116 deletions.
111 changes: 0 additions & 111 deletions Java/Data-Structures/LISTS/CIRCULAR/circular/CircularLinkedList.java

This file was deleted.

248 changes: 248 additions & 0 deletions Java/Data-Structures/LISTS/CIRCULAR/variant1/CircularLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
public class CircularLinkedList
{
private CLLNode head;
private int length;

//constructor
public CircularLinkedList()
{
head=null;
length=0;
}

/**
* INSERTION
* 1. AT HEAD
* 2. AT TAIL
* 3. AT MIDDLE
**/
/**
* addToHead()
* it will insert node at head
* @param int for data
* @return void
**/
public void addToHead(int data)
{
CLLNode newNode = new CLLNode(data);
if(head==null) //if list is empty
{
head=newNode;
head.setNext(head);
}
else
{
newNode.setNext(head);
CLLNode current=head.getNext();
while(current.getNext()!=head)
{
current=current.getNext();
}
current.setNext(newNode);
head=newNode;
}
length++;
}

/**
* addToTail()
* it will add the node to the tail
* @param int for data
* @return void
**/
public void addToTail(int data)
{
CLLNode newNode = new CLLNode(data);
if(head==null) //if empty
{
head=newNode;
head.setNext(head);
}
else
{
newNode.setNext(head);
CLLNode current=head.getNext();
while(current.getNext()!=head)
{
current=current.getNext();
}
current.setNext(newNode);
}
length++;
}

/**
* addToMiddle()
* it will add node to middle
* @param void
* @return void
**/
public void addToMiddle(int data,int position)
{
CLLNode newNode = new CLLNode(data);
if(position==0)
{
if(head==null)
{
head=newNode;
}
else
{
newNode.setNext(head);
CLLNode current = head.getNext();
while(current.getNext()!=head)
{
current=current.getNext();
}
head=newNode;
current.setNext(head);
}
}
else
{
//insert except head
CLLNode current=head;
CLLNode prev=head;
int it=0;
while(current!=null)
{
if(it==position)
{
break;
}
prev=current;
current=current.getNext();
it++;
}
prev.setNext(newNode);
newNode.setNext(current);
}
}

/**
* DELETION
* 1. FROM HEAD
* 2. FROM TAIL
* 3. FROM MIDDLE
**/

/**
* removeFromHead()
* it will remove node from head
* @param void
* @return int for removed node
**/
public int removeFromHead()
{
if(head==null)
{
return -1;
}
CLLNode temp = head;
CLLNode current=head.getNext();
while(current.getNext()!=head)
{
current=current.getNext();
}
head=head.getNext();
current.setNext(head);
temp.setNext(null);
length--;
return temp.getData();
}

/**
* removeFromTail()
* it will remove node from the tail
* @param void
* @return int for removed node
**/
public int removeFromTail()
{
if(head==null)
{
return -1;
}
CLLNode current=head.getNext();
CLLNode prev=head.getNext();
while(current.getNext()!=head)
{
prev=current;
current=current.getNext();
}
current.setNext(null);
prev.setNext(head);
length--;
return current.getData();
}

/**
* removeFromMiddle()
* @param void
* @return the removed node data
**/
public int removeFromMiddle(int position)
{
if(position==0)
{
//delete from head
CLLNode current=head;
while(current.getNext()!=head)
{
current=current.getNext();
}
CLLNode temp=head;
head=head.getNext();
current.setNext(head);
return temp.getData();
}
else
{
//delete from middle
CLLNode prev = head;
CLLNode current= head;
int it=0;
while(current!=null)
{
if(it==position)
{
break;
}
prev=current;
current=current.getNext();
it++;
}
CLLNode nextNode = current.getNext();
prev.setNext(nextNode);
current.setNext(null);
return current.getData();
}
}
/**
* length()
* @param void
* @return the length of the list
**/
public int length()
{
return length;
}
public void PrintCircularListData()
{
if(head==null)
{
System.out.println("null");
}
else
{
System.out.print(head.getData()+"->");
CLLNode current = head.getNext();
while(current!=head)
{
System.out.print(current.getData()+"->");
current=current.getNext();
}
System.out.println("HEAD");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ public static void main(String[] args)
clist.addToHead(4);
clist.PrintCircularListData();

clist.addToMiddle(99,10);

clist.PrintCircularListData();

clist.addToTail(5);
clist.addToTail(6);
clist.addToTail(7);
clist.addToTail(8);
clist.removeFromMiddle(1);
clist.PrintCircularListData();

clist.removeFromHead();
Expand Down
1 change: 1 addition & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* [Double Linked List using object data](Data-Structures/LISTS/DOUBLE/Double-Using-Object/DoubleLinkedList.java)
* CIRCULAR
* [Circular Linked List (using tail node)](Data-Structures/LISTS/CIRCULAR/STANDARD/CircularLinkedList.java)
* [Circular Linked List (using head node)](Data-Structures/LISTS/CIRCULAR/variant1/CircularLinkedList.java)
* MEMORY EFFICIENT DOUBLE LINKED LIST
* UNROLLED LINKED LIST
* SKIP LIST
Expand Down
4 changes: 3 additions & 1 deletion datastructures.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ Indexer for Data Structures Lover
* blog
* docs
* implementation
* [JAVA](Java/Data-Structures/LISTS/CIRCULAR/STANDARD/CircularLinkedList.java)
* JAVA
* [STANDARD](Java/Data-Structures/LISTS/CIRCULAR/STANDARD/CircularLinkedList.java)
* [Variant1: using head node](Java/Data-Structures/LISTS/CIRCULAR/variant1/CircularLinkedList.java)
* complexity

### INBUILT LISTS
Expand Down
Loading

0 comments on commit a0aa49e

Please sign in to comment.