Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在索引处删除函数删除索引之前的所有元素_Java_Algorithm_Data Structures_Linked List - Fatal编程技术网

Java 在索引处删除函数删除索引之前的所有元素

Java 在索引处删除函数删除索引之前的所有元素,java,algorithm,data-structures,linked-list,Java,Algorithm,Data Structures,Linked List,我使用这个删除方法删除列表的第三个元素。但它会删除指定索引之前的所有元素。如果Jhon,Sam,Philip,Emma是列表元素,如果我去掉第三个元素,剩下的唯一元素就是Nick。我怎样才能解决这个问题 import java.util.*; class List { Customer listPtr; int index; public void add(Customer customer) { Customer temp = customer;

我使用这个删除方法删除列表的第三个元素。但它会删除指定索引之前的所有元素。如果Jhon,Sam,Philip,Emma是列表元素,如果我去掉第三个元素,剩下的唯一元素就是Nick。我怎样才能解决这个问题

import java.util.*;

class List {
    Customer listPtr;
    int index;

    public void add(Customer customer) {
        Customer temp = customer;
        if (listPtr == null) {
            listPtr = temp;
            index++;
        } else {
            Customer x = listPtr;
            while (x.next != null) {
                x = x.next;
            }
            x.next = temp;
            index++;
        }
    }

    public void remove(int index) {
        int size = size();
        Customer tmp = listPtr, tmp2;
        int i = 0;
        while (i != size) {
            if ((i + 1) == index) {
                tmp2 = tmp;
                listPtr = tmp2.next;
                break;
            }
            tmp = tmp.next;
            ++i;
        }
    }

    public int size() {
        int size = 0;
        Customer temp = listPtr;
        while (temp != null) {
            temp = temp.next;
            size++;
        }
        return size;
    }

    public void printList() {
        Customer temp = listPtr;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

class DemoList {
    public static void main(String args[]) {
        List list = new List();
        Customer c1 = new Customer("10011", "Jhon");
        Customer c2 = new Customer("10012", "Sam");
        Customer c3 = new Customer("10013", "Philip");
        Customer c4 = new Customer("10014", "Emma");        
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        list.remove(3);
        System.out.println(list.size());
        list.printList();
    }
}

class Customer {
    String id;
    String name;
    Customer next;

    public Customer(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String toString() {
        return id + " : " + name;
    }

    public boolean equals(Object ob) {
        Customer c = (Customer) ob;
        return this.id.equals(c.id);
    }
}

remove()
方法有几个问题

首先,只有在
index==0
时才应该更改
listPtr

在所有其他情况下,您需要调整
节点。下一步
节点的位置
索引-1


< < > > <代码>索引>代码>作为代码>列表的数据成员看起来像是一个等待发生的意外。

您应该考虑跟踪大小,而不是每次都计算它。这是一个O(n)操作,因此列表中的每个方法现在都是O(n)。在add()上增加的索引应该是size。
public void remove(int index) {
  if (i == 0) {
      // TODO: do some checks (listPtr is set, has next...)
      listPtr = listPtr.next;
  }
  else {
      int currentIndex = 0;
      Customer currentElement = listPtr;
      while (currentIndex != size()) {
          if ((currentIndex + 1) == index) {
              currentElement.next = currentElement.next.next;
              break;
          }
          currentElement = currentElement.next;
          currentIndex++;
      }
  }
}