[英]linked list tutorial in java
i am learning linked list in Java and wrote some sample code for practise. 我正在学习Java中的链表,并为练习编写了一些示例代码。 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. 那就是打印出cory,joe和tom,我希望输出是tom,joe和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. 您正在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. 如果您注意到我添加了尾部引用,而不是在开头插入新对象,我将它附加到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. 实际上你可以有两种方法保存你的方式...然后添加我的新方法插入但是调用它添加你可以在开始时插入或添加到LinkedList的结尾。
As @brso05 pointed out, you are inserting values to head, instead of to the tail. 正如@ brso05指出的那样,你要将值插入头部而不是尾部。
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 这不是一个很好的解决方案,应该是即兴的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.