简体   繁体   English

帮助制作Java中的单链列表

[英]Help making a singly linked list in Java

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

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! 谢谢!

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. 我在学习Java时就做到了,当我刚接触一种新语言时仍会这样做,而且我所标记的学生找到了新颖而惊人的方法。 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? 公共节点elementAt(int index)例如? 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)

A few suggestions: 一些建议:

  1. implement java.util.List 实现java.util.List
  2. Think about generics 考虑泛型
  3. Read this . 阅读

Start with "insert at the end" before you think about "insert at i". 在考虑“在i处插入”之前,请先从“末尾插入”开始。

  1. In the line e.next = header.next.next what would happen if header.next points to a 'null'? e.next = header.next.next行中,如果header.next指向'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? 然后使用那些功能实现插入?

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. 我尝试了一个简单的程序,对您有用,我也在学习Java,如有任何错误,请忍受,但该程序可以正常工作。

I am posting a very simple singly linked list program in Java, which I tried out today. 我正在用Java发布一个非常简单的单链列表程序,我今天尝试了该程序。 I hope it will help all. 希望对大家有帮助。

LinkList.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 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| | 1 |-> | 2 |-> | 3 |-> | 4 |-> | 5 |-> | NULL |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM