簡體   English   中英

無法刪除鏈接列表中的第一項

[英]Unable to delete first item in linked list

我看到了一些解決方案,但仍然無法解決代碼中的錯誤。 我的deleteFromStart方法沒有從列表中刪除任何元素。 ob.display()兩個調用都產生相同的輸出。 您能告訴我我所缺少的是什么,或者錯誤在哪里?

LikedList:

package lab5;

public class LinkedList {

    public static void main(String argsp[]){

        List ob = new List();

        ob.addAtStart("y", 6);
        ob.addAtStart("w", 4);
        ob.addAtStart("z", 3);

        ob.addAtEnd("a",3);
        ob.addAtEnd("b",4);
        ob.addAtEnd("c",5);

        ob.display();

        ob.deleteFromStart();

        System.out.println("\n");
        ob.display();
    }
}

清單:

package lab5;

public class List {

    Node head;

    public List(){
        head=null;
    }

    public List(Node e){
        head=e;
    }

    Node oldfirst=null;
    Node lasthead=null;

    public void addAtStart(String name, int age){
        Node newObject= new Node(name,age);
        newObject.next=head;

        if (oldfirst==null) {
            oldfirst = newObject;
        }
        head = newObject;
        lasthead = head;
    }

    public void display() {
        Node store = head;
        while (store != null) {
            store.display();
            store=store.next;
            System.out.println();
        }
    }

    public void addAtEnd(String name, int age){
        Node atEndValue = new Node(name, age);
        oldfirst.next = atEndValue;
        oldfirst = atEndValue;
    }

    public void deleteFromStart() {
        while (lasthead != null) {
            lasthead = lasthead.next;
        }
    }

    public boolean isEmpty() {
        return head == null;
    }

節點:

package lab5;

public class Node {

    String name;
    int age;
    Node next;

    public Node(){
        name="Abc";
        age=10;
        next=null;
    }

    public Node(String name, int age ){
        this.name=name;
        this.age=age;
        next = null;
    }

    public void display(){
        System.out.println("Name: " + name + " Age: " + age);
    }
}

tl; dr要刪除鏈表中的第一個元素:

head = head.next

在實現單鏈接列表時,實際上只需要保留一個指針: head (即對列表中第一個節點的引用。實際上,跟蹤列表中的最后一個元素也很有用。 (通常稱為tail )。這允許在列表的末尾進行固定時間的操作,如果您經常在元素的末尾添加元素,這將非常有用。因此,使用此基本實現,您最終會得到以下內容:

class LinkedList {
    private Node head = null;
    private Node tail = null;

    public LinkedList() {}

    public LinkedList(Node e) {
        head = e;
        tail = e;
    }
}

class Node {
    Node next = null;
    // other data
}

在鏈表中添加和刪除元素歸結為更新headtail變量所指的內容。 考慮一個包含三個元素[A, B, C]的單鏈接列表。 headtail值對齊如下:

 A -> B -> C -> null
 ^         ^
 |         |
head      tail

如果要插入新元素X ,則有兩個步驟:

1)告訴X.next引用A

X -> A -> B -> C -> null
     ^         ^
     |         |
    head      tail

2)更新head以引用X

 X -> A -> B -> C -> null
 ^              ^
 |              |
head           tail

您以類似的方式來回移動headtail ,具體取決於您要添加還是刪除,以及操作是否在列表的開頭或結尾。

從頭開始刪除元素(假設列表不為空)就像更新head引用下一個元素一樣簡單。 在上面的示例中,這意味着將移動head引用X.next ,它是A

X -> A -> B -> C -> null
     ^         ^
     |         |
    head      tail

現在請記住,鏈表僅直接了解headtail ,因此一旦將head引用為A ,則在應用程序中的任何地方都沒有引用X ,並且該鏈接已被有效刪除(在Java中,這將使其成為垃圾) -集)。

實際上,我們在上面所做的只是head = head.next 同樣,您必須首先確保列表不為空,因為如果列表為空,則head.next會導致空指針異常。

我還建議刪除oldfirstlasthead ,並根據上述理論更新您的add *方法。

實際上,這不是鏈接列表,它更像是一個隊列,在隊列中您具有后指針和前指針。 在列表中顯示項目時,您使用的是“ head”指針,而從頭開始刪除時,您使用的是“ lasthead”。 這將移動最后一個頭,但不會再次將值分配給head。

 public void deleteFromStart(){

    while(lasthead!=null){

        this.lasthead=lasthead.next;
    }
    head = this.lasthead;
}

好吧,我嘗試了這段代碼,並且從一開始就刪除了所有元素(我希望這就是您想要的)

要刪除第一個元素:

public void deleteElementAtStart()
{
    if(lasthead != null)
    {
        this.lasthead = lasthead.next;
    }
    else
    {
        System.out.println("List is already empty!");
    }
    head = lasthead;
}

編輯它。

謝謝大家! 我意識到自己的錯誤是我使用了多余的對象,這就是為什么在解決方案中我得到了這兩種方法。

public void deleteFromStart(){


        while(lasthead.next!=null){
            lasthead=lasthead.next;
            head=lasthead;
            break;
        }

    }

要么

public void deleteFromStart(){

            while(head.next!=null){
                head=head.next;
                break;
            }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM