帮忙用Java制作一个单向链表

发布于 2024-09-24 05:45:38 字数 1264 浏览 2 评论 0原文

这是家庭作业,但请知道我已经在网上寻求帮助(例如 http://www.sethi.org/classes/class_stuff/cis435/others/notes-java/data/collections/lists/simple-linked-list.html )和我的教科书,但我仍然遇到一些问题。

任何帮助将不胜感激...

现在我正在尝试插入值,但没有任何效果。无论它是第一项,无论是作为最后一项添加,还是介于两者之间。

Node header = null;    // First element of list.
Node back  = null;    // Last element of list.

public void insert(int i, double value){ //insert value before i-th element
  Node e = new Node();
  e.num = value;
  Node curr = header;
  for(int x=0;x<i;x++) {
   if (i == 1) { //we want to insert as first thing
    if (size == 0) { //its the FIRST time we add something
     header.next = e;
     e.next = back;
     break;
    } else if (size == 1){
     e.next = header.next; //i.e. the second thing in the list
     header.next = e;
     break;
    } else {
     e.next = header.next.next; //i.e. the second thing in the list
     header.next = e;
     break;
    }
   }
   else if (x == (i-1)) {
    e.next = curr.next;
    curr.next = e;
    break;
   }
   curr = curr.next;
  }
  size = size+1;
 }

不太确定为什么它不起作用。

谢谢!

This is for homework but please know that I have looked online for help (such as http://www.sethi.org/classes/class_stuff/cis435/others/notes-java/data/collections/lists/simple-linked-list.html) and my textbook but I am still having some issues.

Any help would be appreciated...

Right now I'm trying to just insert values in but nothing is working. Whether it's the first item, whether it's being added as the last one, or somewhere in between.

Node header = null;    // First element of list.
Node back  = null;    // Last element of list.

public void insert(int i, double value){ //insert value before i-th element
  Node e = new Node();
  e.num = value;
  Node curr = header;
  for(int x=0;x<i;x++) {
   if (i == 1) { //we want to insert as first thing
    if (size == 0) { //its the FIRST time we add something
     header.next = e;
     e.next = back;
     break;
    } else if (size == 1){
     e.next = header.next; //i.e. the second thing in the list
     header.next = e;
     break;
    } else {
     e.next = header.next.next; //i.e. the second thing in the list
     header.next = e;
     break;
    }
   }
   else if (x == (i-1)) {
    e.next = curr.next;
    curr.next = e;
    break;
   }
   curr = curr.next;
  }
  size = size+1;
 }

Not really sure why it isn't working.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯然#的苍凉 2024-10-01 05:45:38

由于某种原因,仍在学习编程的人使事情变得比他们需要的复杂得多。我在学习 java 时就这样做了,当我刚刚进入一门新语言时我仍然这样做,而且我标记的学生找到了新的、令人惊奇的方法来做到这一点。您的插入中需要进行更多操作,例如,在特定索引处插入值的方法不应该检查它是否是要插入的第一个项目(并不是说它不应该检查边界)。这是我要做的伪代码。

insert(index, value)
    if index>size
        throw null pointer
    traverse to index -1 //lets call this nodeI
    create newnode and set value
    set newnode.next to nodeI.next
    set nodeI.next to newnode
    increase size.

给你几个方便的提示,你应该有一个函数从链接列表中获取一个元素,返回一个节点?例如公共节点 elementAt(int index) ?用它来遍历链表。如果你想追加到链接列表,试试这个

append(value)
    insert(size-1,value)

,如果你想在开头插入?同样的想法

insert(value)
    insert(0,value)

For some reason, people who are still learning to program make things far more complicated then they need to be. I did it when I was learning java, I still do it when I am just getting into a new language, and students that I have marked find new and amazing ways to do it. You have more going on in your insert then there needs to be, for example, a method that inserts a value at a specific index should not check if it's the first item to be inserted (not saying it shouldn't check bounds). Here is the pseudo code of what I would do.

insert(index, value)
    if index>size
        throw null pointer
    traverse to index -1 //lets call this nodeI
    create newnode and set value
    set newnode.next to nodeI.next
    set nodeI.next to newnode
    increase size.

Couple of handy hints for you, you should have a function to get an element from the link list, something that returns a node? public node elementAt(int index) for example? use that to traverse the linked list. If you want to append to the Linked list, try this

append(value)
    insert(size-1,value)

and if you want to insert at the beginning? same idea

insert(value)
    insert(0,value)
忘你却要生生世世 2024-10-01 05:45:38
  1. e.next = header.next.next 行中,如果 header.next 指向“null”会发生什么?可以到达吗?
  2. 您必须处理哪些特殊情况?您是否已将它们全部考虑在内?
  3. 能否先从最简单的情况开始,要么在前面加一个元素,要么在后面加一个元素?那么用那些函数来实现插入呢?
  1. In the line e.next = header.next.next what would happen if header.next points to a 'null'? Is it possible to get there?
  2. What are the corner cases you have to deal with and have you taken them all into account?
  3. Can you start with the simplest case first, adding either an element to the front or an element to the back? Then use those functions to implement the insert?
爱人如己 2024-10-01 05:45:38

一些建议:

  1. 实现 java.util.List
  2. 考虑泛型
  3. 阅读 这个

在考虑“在 i 处插入”之前,先从“在末尾插入”开始。

A few suggestions:

  1. implement java.util.List
  2. Think about generics
  3. Read this.

Start with "insert at the end" before you think about "insert at i".

我三岁 2024-10-01 05:45:38

我尝试了一个简单的程序,这对你们很有用,我也在学习Java,如果有任何错误,请原谅我,但这个程序运行良好。

我正在发布一个非常简单的 Java 单链表程序,我今天尝试了它。
我希望它能帮助大家。

LinkList.java

class LinkList
{
 public static void main(String args[])
 {
  Node node = new Node(1);
  node.addAtLast(2);
  node.addAtLast(3);
  node.addAtLast(4);
  node.addAtLast(5);
  node.printList();
 }

}

Node.java

class Node
{
 private int data;
 private Node link;

 public Node(int mydata)
 {
  data = mydata;
  link = null;
 }

 public void printList()
{
 System.out.print("|"+data+"|"+"->");
 if(link != null)  
 {
//recursive call
 link.printList();

 }
else
 {
//marking end of list as NULL
 System.out.print("|NULL|"); 
 }
}

public void addAtLast(int mydata)
{
 if(link == null)
 {

  link = new Node(mydata);
 }
 else
 {
  link.addAtLast(mydata);
 }

}

}

输出:

下面是我们的输出

|1|->|2|->|3|->|4|->|5|->|NULL|

I have tried a simple program, which will be useful for you guys, I am also learning Java, please bear with me for any mistakes, but this program works fine.

I am posting a very simple singly linked list program in Java, which I tried out today.
I hope it will help all.

LinkList.java

class LinkList
{
 public static void main(String args[])
 {
  Node node = new Node(1);
  node.addAtLast(2);
  node.addAtLast(3);
  node.addAtLast(4);
  node.addAtLast(5);
  node.printList();
 }

}

Node.java

class Node
{
 private int data;
 private Node link;

 public Node(int mydata)
 {
  data = mydata;
  link = null;
 }

 public void printList()
{
 System.out.print("|"+data+"|"+"->");
 if(link != null)  
 {
//recursive call
 link.printList();

 }
else
 {
//marking end of list as NULL
 System.out.print("|NULL|"); 
 }
}

public void addAtLast(int mydata)
{
 if(link == null)
 {

  link = new Node(mydata);
 }
 else
 {
  link.addAtLast(mydata);
 }

}

}

OUTPUT :

The below is our output

|1|->|2|->|3|->|4|->|5|->|NULL|

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文