如何在特定索引中插入节点

时间:2017-11-08 09:55:38

标签: java data-structures

首先请原谅我,如果我问的方式是错的,我是编程的新手,还需要很多学习,这是我的代码 这是我的Node类

public class Node
{

int value;
Node link;

void setValue(int a){
value = a;
}

int getValue(){
return value;
}

void setNode(Node i){
link = i;
}

Node getNode(){
return link;
}
}

这是我的NodeContainer类

    public class NodeContainer
{
Node tail;
Node head;

void setHead(int i){
    Node a = new Node();
    a.setValue(i);
    head = a;
    tail = head;
}

Node getHead(){
    return head;

}

void addNode(int i){
    Node a = new Node();
    a.setValue(i);

    tail.setNode(a);
    tail = a;
}

void addNode(int i,int index){
Node a = new Node();
a.setValue(i);
int c = 1;
Node temp = head;
Node first = head;
   while(head!=null){  
    head = head.getNode();
    if(c==index-1){
        a.setNode(head.getNode());
        temp = head;
        temp.setNode(a);
    }
    c++;
}
head = first;
head.setNode(temp);
}

int count(){
int c = 1;
boolean have = true;
if(head!=null){
while(head.getNode()!=null){       
    c++;
    head = head.getNode();
}
    return c;
}
else{
have = false;
return 0;}
} 

}

我只是想不出一种在特定索引中插入节点的方法,我在addNode方法上尝试了它已经尝试了很多我能想到的方法但它无法正常工作,提前谢谢

2 个答案:

答案 0 :(得分:0)

评论:

您不需要跟踪尾部(必要)我看到您是链接列表的新手,所以也许在添加额外功能之前尝试掌握基础知识。

另外,尝试在代码中添加一些空白区域,以便于阅读。

我大量评论了涵盖在指定索引处插入的部分。将所有这些文件放在同一个文件夹中,编译它。您应该得到以下输出:

输出:

0 1 2 3 4 5 6 7 8 9 
inserting -1 at position 5
0 1 2 3 4 -1 5 6 7 8 9 

主要课程:

class test {

  public static void main(String[] args) {


    NodeContainer linkedList = new NodeContainer();

    for (int i = 0; i < 10; i++) {
      linkedList.addNode(i);
    }

    display(linkedList.head);

    // NOTE - indexes start at 0, not 1
    System.out.println("inserting -1 at position 5");

    linkedList.addNode(-1, 5);

    display(linkedList.head);

  }

  public static void display(Node node) {
    while (node != null) {
      System.out.print(node.value + " ");

      node = node.link;
    }

    System.out.println();
  }
}

节点容器类:

public class NodeContainer {
  public Node head;
  public int size;

  public NodeContainer() {
    size = 0;
    head = null;
  }

  public void addNode(int i) {
    Node node = new Node(i);

    if (head == null) {
      head = node;

    } else {
      Node temp;

      temp = head;

      while (temp.link != null) {

      temp = temp.link;

      }
      temp.link = node;
    }

    size++;
  }

  public void addNode(int i, int index) {
    Node node;
    Node temp;
    int count;

    // If we want to insert
    // into the front of the list
    if (index == 0) {

      // User a constructor instead of
      // making a new node every time,
      // then typing set value
      node = new Node(i);
      node.link = head;
      head = node;

      // If the index is within the linkedlist
    } else if (index > 0 && index < size) {

      count = 0;
      temp = head;

      // Keep looking until the index prior to
      // the one we want to insert at
      while(temp.link != null && count < size) {

        // Once we find it, break out the loop
        if (count == index-1) {
          break;
        }

        // Keep traversing the list
        temp = temp.link;

        // increment counter
        count++;
      }

      // Instantiate the node
      node = new Node(i);

      // Point this node's link to
      // the n-1th node's link
      node.link = temp.link;

      // Set n-1th node's next to this
      // node (effectively putting it
      // in position n)
      temp.link = node;

      // Otherwise don't do anything
    } else {
      ;
    }

  }

}

节点类:

public class Node {

  public int value;
  public Node link;

  // User a constructor
  public Node(int i) {

    value = i;
    link = null;
  }

  public void setValue(int a) {
    value = a;
  }

  public int getValue() {
    return value;
  }

  public void setNode(Node i) {
    link = i;
  }

  public Node getNode() {
    return link;
  }
}

答案 1 :(得分:0)

我已将原始代码段更新为:

public class Node
{
    int value;
    Node next;

    void setValue(int value){
        this.value = value;
    }

    int getValue(){
        return this.value;
    }

    void setNext(Node next){
        this.next = next;
    }

    Node getNext(){
        return this.next;
    }
}

 public class NodeContainer {

    Node tail;
    Node head;

    void setHead(int i) {
        Node a = newNode(i);

        head = a;
        tail = a;
    }

    private Node newNode(int value) {
        Node node = new Node();
        node.setValue(i);
        return node;
    }

    void addNode(int i) {
        Node a = newNode(i);

        Node tmp = this.head;
        while(tmp.getNext() != null){
            tmp = tmp.getNext();
        }
        tmp.setNext(a);
        tail = a;
    }

    void addNode(int value, int index) {
        //TODO : add check on head
        Node a = newNode(value);

        Node tmp = this.head;
        int i = 0;
        while(tmp.getNext()!=null && i < index) {
            tmp = tmp.getNext();
            i++;
        }

        //TODO : add check on tmp
        Node next = tmp.getNext();
        tmp.setNext(a);
        a.setNext(next);
    }

    int count() {
        //...
    }

}