i am learning linked list in Java and wrote some sample code for practise. Basically its a singe linked list. The code works fine but it reverses the output. That is it prints out cory, joe and tom and i want the output to be tom, joe and cory. Tom being the first node. How do i go about that or is that the way a single linked list works. That is it always reverses the output?
public class LinkedList {
public String name;
public LinkedList next;
public LinkedList(String name)
{
this.name = name;
this.next = null;
}
public String toString()
{
return name;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Linked l = new Linked();
l.insert("tom");
l.insert("joe");
l.insert("cory");
l.print();
}
}
class Linked
{
LinkedList first;
public Linked()//initialize
{
this.first = null;
}
public void insert(String name)
{
LinkedList g = new LinkedList(name);
g.next = first;
first = g;
}
//checks if the list is empty
public boolean isEmpty()
{
return (first ==null);
}
public void print() //displays the list
{
LinkedList t = first;
while(t!=null)
{
System.out.println(t);
t = t.next;
}
}
}
You are inserting at beginning of LinkedList. If you want to add then insert new node after last node. You would need a tail reference.
public String name;
public LinkedList next;
public LinkedList(String name)
{
this.name = name;
this.next = null;
}
public String toString()
{
return name;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Linked l = new Linked();
l.insert("tom");
l.insert("joe");
l.insert("cory");
l.print();
}
}
class Linked
{
LinkedList first;
LinkedList tail;
public Linked()//initialize
{
this.first = null;
this.tail = first;
}
public void insert(String name)
{
LinkedList g = new Test(name);
if(isEmpty())
{
first = g;
tail = first;
}
else
{
tail.next = g;
tail = g;
}
}
//checks if the list is empty
public boolean isEmpty()
{
return (first ==null);
}
public void print() //displays the list
{
LinkedList t = first;
while(t!=null)
{
System.out.println(t);
t = t.next;
}
}
If you notice I added a tail reference and instead of inserting the new object at the beginning i attach it to the end of the LinkedList. You could change the method name to add. In fact you could have 2 methods keep yours how it is...then add my new method insert but call it add that way you could insert at beginning or add to end of LinkedList.
As @brso05 pointed out, you are inserting values to head, instead of to the tail.
That is
tom
joe -> tom
cory -> joe -> tom
instead you should insert it to the tail, like this
public void insert(String name)
{
if(first==null)
{
LinkedList g = new LinkedList(name);
g.next = null;
first = g;
} else {
LinkedList g = new LinkedList(name);
if (first.next==null) {
g.next = null;
first = g;
return;
}
LinkedList l=first.next;
for(;l!=null;l=l.next){
if(l.next==null) {
l.next = g;
g.next = null;
break;
}
}
}
}
This is not a very good solution, it should be improvised
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.