0

我使用 add 方法将元素添加到所需的索引。但它是最后添加的。我想将一个元素添加到所需的索引。怎么能做到这一点。有可能吗?没有结束指针。

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 add(int index, Customer customer) {
        int size = size();
        while (size != -1) {
            size--;
            if (size == index) {
                System.out.println(size + ":" + index);
                add(customer);
            }
        }
    }

    public void removeFirst() {
        Customer temp = listPtr;
        if (listPtr != null) {
            listPtr = temp.next;
        }
    }

    public void removeLast() {
        Customer temp = listPtr;
        while (temp.next.next != null) {
            temp = temp.next;
        }
        temp.next = null;
        index--;
    }

    public void removeAll() {
        Customer temp = listPtr;
        while (temp != null) {
            listPtr = temp.next;
            temp = temp.next;
        }
    }

    public int size() {
        int size = 0;
        Customer temp = listPtr;
        while (temp != null) {
            size++;
            temp = temp.next;
        }
        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", "Sam");
        Customer c2 = new Customer("10012", "Jason");
        Customer c3 = new Customer("10013", "Arnold");
        Customer c4 = new Customer("10014", "Bob");
        Customer c5 = new Customer("10015", "Tom");
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        list.add(2, c5);
        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);
    }
}
4

2 回答 2

0

试试这个,从头开始找到索引并customer在那个位置添加

public void add(int index, Customer customer) {
    int size = size();
    Customer tmp=listPtr,tmp2;
    int i=0;
     //some boundary cases
    if((index<0) ||(index>size)){
       return;
    }
    //assumes all other cases are handled
    while (i != size) {            
        if ((i+1) == index) {
            System.out.println(size + ":" + index);
            tmp2=tmp.next;
            tmp.next=customer;
            customer.next=tmp2;
            break;
        }
        tmp=tmp.next;
        ++i;
    }
}
于 2013-08-11T17:32:41.513 回答
0

假设你有两个Customers, a, 和b, 并且它们连接在一起。

Customer a = new Customer("a", "John");
Customer b = new Customer("b", "George");
a.next = b;

现在,您想c在它们之间滑动:

Customer c = new Customer("c", "Paul");

您最终希望拥有a.next == cc.next == b。但是,如果您以错误的顺序进行这些更改,您的列表将b与列表的其余部分分离,并且将被垃圾收集。所以,按照正确的顺序做事,这样就不会丢失任何东西:

c.next = b;  // Now, both a.next and c.next == b.
a.next = c;  // Finishing the splice.  Now, a.next == c and c.next == b.

要将项目插入到列表中,请从开始的index位置前进。

Customer nth(int n) {
    Customer cust = listPtr;
    for (int i = 0; i < n; ++i) {
        cust = cust.next;  // can lead to an NPE; you add the error checking.
    }
    return cust;
}

现在,得到nth客户,并拼接一个新的。同样,注意 NPE。

于 2013-08-11T16:11:23.387 回答