| | |
Single Linked Circular Link List
![]() |
Does anyone know how to fully set up a circular linked list, and its methods, remove, insert, and find?
Here is what i have so far but i have no idea if its proper
public class CLL
{
public class Node
{
int item;
Node next;
public Node()
{
this.item = 0;
this.next = null;
}
public Node(int e, Node n)
{
item = e;
next = n;
}
}
Node head;
public void insert(int item)
{
if(head == null)
{
head = new Node(item,null);
head.next = head;
}
else
{
head.next = new Node(item,head.next);
}
}
public void remove(int item)
{
}
public int find(int item)
{
}
}
First is my insert right? I think it is, but I keep feeling I may have missed something, and secondly, I have no idea how to do the remove function, or the find function If someone has code to help me it would be very awesome
Here is what i have so far but i have no idea if its proper
public class CLL
{
public class Node
{
int item;
Node next;
public Node()
{
this.item = 0;
this.next = null;
}
public Node(int e, Node n)
{
item = e;
next = n;
}
}
Node head;
public void insert(int item)
{
if(head == null)
{
head = new Node(item,null);
head.next = head;
}
else
{
head.next = new Node(item,head.next);
}
}
public void remove(int item)
{
}
public int find(int item)
{
}
}
First is my insert right? I think it is, but I keep feeling I may have missed something, and secondly, I have no idea how to do the remove function, or the find function If someone has code to help me it would be very awesome
You insert looks right.
For remove you should really make a small drawing on paper on what you need to do.
Find the record to remove, and set the next of the record before it to the record next of the record you want to remove is pointing to.
That's all.
And oh, do remember to pass back a reference to an item still in the list from the remove function. Else the program using the list might loose the list if you remove the point where the program hooks into the list, which would have you effectively delete the entire list except the item you want to remove from it.
In a circular linked list it's impossible to find items based on an index. After all, there is no starting point!
You could give each item an index number and loop through the list from some point (you need to keep a reference to something in the list after all somewhere or the whole thing gets impossible to reach).
For remove you should really make a small drawing on paper on what you need to do.
Find the record to remove, and set the next of the record before it to the record next of the record you want to remove is pointing to.
That's all.
And oh, do remember to pass back a reference to an item still in the list from the remove function. Else the program using the list might loose the list if you remove the point where the program hooks into the list, which would have you effectively delete the entire list except the item you want to remove from it.
In a circular linked list it's impossible to find items based on an index. After all, there is no starting point!
You could give each item an index number and loop through the list from some point (you need to keep a reference to something in the list after all somewhere or the whole thing gets impossible to reach).
Well here is my remove and find functions. Are these correct? I didn't know what you meant by this part:
you wrote:==================================================
And oh, do remember to pass back a reference to an item still in the list from the remove function. Else the program using the list might loose the list if you remove the point where the program hooks into the list, which would have you effectively delete the entire list except the item you want to remove from it.
==========================================================
public void remove(int key)
{
Node current = head;
do
{
if(current.next.item == key )
{
Node temp = current.next;
current = temp.next;
if(temp == head)
{
head = head.next;
}
temp = null;
break;
}
current = current.next;
} while(current != head);
}
public int find(int key)
{
Node p = head;
while(p!=null && !(p.item == key))
{
p = p.next;
if(p!= null)
{
return p.item;
}
}
return 0;
}
I can't tell if the logic on those are both correct or not, because when using it, it turned up values, that I don't think were correct. Hope you can help, me cause its breakin ma balls
you wrote:==================================================
And oh, do remember to pass back a reference to an item still in the list from the remove function. Else the program using the list might loose the list if you remove the point where the program hooks into the list, which would have you effectively delete the entire list except the item you want to remove from it.
==========================================================
public void remove(int key)
{
Node current = head;
do
{
if(current.next.item == key )
{
Node temp = current.next;
current = temp.next;
if(temp == head)
{
head = head.next;
}
temp = null;
break;
}
current = current.next;
} while(current != head);
}
public int find(int key)
{
Node p = head;
while(p!=null && !(p.item == key))
{
p = p.next;
if(p!= null)
{
return p.item;
}
}
return 0;
}
I can't tell if the logic on those are both correct or not, because when using it, it turned up values, that I don't think were correct. Hope you can help, me cause its breakin ma balls
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#5 16 Days Ago
http://www.dreamincode.net/forums/showtopic50999.htm
The question is write a java program which maintains the data inside single linked list.The program needs to insert,delete and search and print the data using the main menu,The data should contain information about the newborn baby.
so is it right atleast partially
class Baby{
private string lastName;
private string firstName;
private int age;
public baby( stringlast,string first,inta)
{
lastName = last;
firstName = first;
age = a;
}
}
public void display()
{
System.out.print("Lastname:"+ lastName);
System.out.print("Firstname:"+ firstName);
System.out.print("age:"+ age);
}
public find(String searchName)
{
int i;
for (i = 0; i< nElems;j++)
if( a[i].getLast().equals(SearchName))
break;
if(j == nElems)
return null;
else
return a[i];
}
public insert( String last,String first,int a)
{
a [nElems] = new Baby(last,first,age)
nElems ++;
}
public boolean delete (String searchName)
{
int i ;
for ( i = 0;i < nElems;i++)
if( a[i].getLast().equals(SearchName))
break;
if(j == nElems)
return false;
else
{
for (int j = i;j<nElems;j++)
a[j] = a[j+1]
nElems --;
return True
}
}
public void displayA()
{
for int i = 0;i<nElems;i++)
a[i].display baby();
}
// tester class
class babyTester
{
public static void main (String args[])
{
int maxSize = 100;
class baby arr;= new classBaby(maxSize);
arr.insert("emina","karadzic",2);
arr.insert("elvira","pocak",2);
arr.insert("emina","karadzic",3);
arr.insert("haris","toronto",1);
arr.insert("ernes","karaica",2);
arr.insert("zahida","parween",1/5);
arr.insert("obaid","tayyip",2);
arr.insert("amela","betska",1);
arr.insert("faruk","hadizc",1);
}
arr.displayA();
String searchKey= ".....";
baby found;
found = arr.find(searchKey);
if (found!=null)
{
system.out.print}("found");
found.displayPerson();
}
else
system.out.print("cant find")
system.out.print("deleting selma,caner,2");
arr.delete("selma");
arr.delte("caner");
arr.delete(2);
arr.displayA();
}
}
}
}
}
}
)
}
could any one help me complete it cause and tell me if i did the right thing considering the question above
The question is write a java program which maintains the data inside single linked list.The program needs to insert,delete and search and print the data using the main menu,The data should contain information about the newborn baby.
so is it right atleast partially

class Baby{
private string lastName;
private string firstName;
private int age;
public baby( stringlast,string first,inta)
{
lastName = last;
firstName = first;
age = a;
}
}
public void display()
{
System.out.print("Lastname:"+ lastName);
System.out.print("Firstname:"+ firstName);
System.out.print("age:"+ age);
}
public find(String searchName)
{
int i;
for (i = 0; i< nElems;j++)
if( a[i].getLast().equals(SearchName))
break;
if(j == nElems)
return null;
else
return a[i];
}
public insert( String last,String first,int a)
{
a [nElems] = new Baby(last,first,age)
nElems ++;
}
public boolean delete (String searchName)
{
int i ;
for ( i = 0;i < nElems;i++)
if( a[i].getLast().equals(SearchName))
break;
if(j == nElems)
return false;
else
{
for (int j = i;j<nElems;j++)
a[j] = a[j+1]
nElems --;
return True
}
}
public void displayA()
{
for int i = 0;i<nElems;i++)
a[i].display baby();
}
// tester class
class babyTester
{
public static void main (String args[])
{
int maxSize = 100;
class baby arr;= new classBaby(maxSize);
arr.insert("emina","karadzic",2);
arr.insert("elvira","pocak",2);
arr.insert("emina","karadzic",3);
arr.insert("haris","toronto",1);
arr.insert("ernes","karaica",2);
arr.insert("zahida","parween",1/5);
arr.insert("obaid","tayyip",2);
arr.insert("amela","betska",1);
arr.insert("faruk","hadizc",1);
}
arr.displayA();
String searchKey= ".....";
baby found;
found = arr.find(searchKey);
if (found!=null)
{
system.out.print}("found");
found.displayPerson();
}
else
system.out.print("cant find")
system.out.print("deleting selma,caner,2");
arr.delete("selma");
arr.delte("caner");
arr.delete(2);
arr.displayA();
}
}
}
}
}
}
)
}
could any one help me complete it cause and tell me if i did the right thing considering the question above
Last edited by aylin; 16 Days Ago at 4:58 pm.
![]() |
Similar Threads
- Doubly linked circular list (C)
- To find whether Single Linked List is looped. (C)
- Circular linked list (C)
- circular link list (C)
Other Threads in the Java Forum
- Previous Thread: JavaFX question
- Next Thread: Odd/Even Code
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry block bluetooth class client code compile compiler component database developmenthelp eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loops mac main map method methods mobile myregfun netbeans nonstatic notdisplaying number online pearl problem program project qt recursion scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






