VB icon

Circular Linked List

Email
Submitted on: 1/2/2015 8:50:00 PM
By: michael worthington (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: Java (JDK 1.2)
Views: 2875
 
     The Circular Linked List class is created from scratch as well as uses its own Node class. It differs from the regular linked list because the last node points to the first node. CLL's are like arrays but have some differences. This is an excellent class for college students learning java.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Circular Linked List
// Description:The Circular Linked List class is created from scratch as well as uses its own Node class. It differs from the regular linked list because the last node points to the first node. CLL's are like arrays but have some differences. This is an excellent class for college students learning java.
// By: michael worthington (from psc cd)
//
// Returns:I threw in a main method to give a brief idea of how to use the CLL. The following is the output:
( )
( cool crazy punk )
true
false
( punk crazy cool )
//**************************************

public class CLL
{
private class Node
{
Object data;
Node next;
public Node(Object data, Node next)
{ 
this.data = data;
this.next = next;
}
public String toString()
{
return this.data.toString();
}
}
Node rear;
public CLL()
{rear = null;}
public String toString()
{
if(rear==null)return "( )";
Node temp = rear.next;
String retval = "( ";
do{
retval= (retval + temp.data.toString() + " ");
temp = temp.next;
}while(temp!=rear.next);
retval+=")";
return retval;
}
public boolean isEmpty()
{return (rear==null);}
public void clear()
{rear=null;}
public void add(Object obj)
{
Node temp = new Node(obj,null);
if(rear==null)
{
temp.next = temp;
rear = temp;
}
else
{
temp.next = rear.next;
rear.next = temp;
}
}
public Object delete()
{
if(isEmpty())return "empty list";
Object temp = rear.next.data;
rear.next = rear.next.next;
return temp;
}
public boolean find(Object target)
{
if(isEmpty())return false;
Node temp = rear.next;
do{
if(temp.data==target)return true;
temp=temp.next;
}while(temp!=rear.next);
return false;
}
public void reverse()
{
CLL temp = new CLL();
Node temper = rear.next;
if(isEmpty()){}
else
{
do{
temp.add(temper.data);
temper = temper.next;
}while(temper!=rear.next);
rear = temp.rear;
}
}
public static void main(String[] args)
{
CLL x = new CLL();
System.out.println(x);
x.add("punk");
x.add("crazy"); 
x.add("cool");
System.out.println(x);
System.out.println(x.find("crazy"));
System.out.println(x.find("lame"));
x.reverse();
System.out.println(x);
}
} 


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.