删除单链表

时间:2016-06-08 12:26:25

标签: java

这样如果要删除的元素是列表的第一个元素而列表只包含一个元素,则只需要为pfirst和plast赋值null。如果要删除的元素是列表的第一个元素,并且列表包含多个元素,则需要一个临时变量指向pfirst,然后移动pfirst指向其下一个元素并将临时变量设置为null。

这是我的代码无法按预期工作

3 个答案:

答案 0 :(得分:3)

它接缝你的问题是不完整的,因为它没有代码与它我也可以向你演示如何实现和删除列表

.*

在同一课程中包含此方法

public class SinglyLinkedList {


  public void addLast(SinglyLinkedListNode newNode) {

        if (newNode == null)

              return;

        else {

              newNode.next = null;

              if (head == null) {

                    head = newNode;

                    tail = newNode;

              } else {

                    tail.next = newNode;

                    tail = newNode;

              }

        }

   }



  public void addFirst(SinglyLinkedListNode newNode) {

        if (newNode == null)

              return;

        else {

              if (head == null) {

                    newNode.next = null;

                    head = newNode;

                    tail = newNode;

              } else {

                    newNode.next = head;

                    head = newNode;

              }

        }

  }



  public void insertAfter(SinglyLinkedListNode previous,

              SinglyLinkedListNode newNode) {

        if (newNode == null)

              return;

        else {

              if (previous == null)

                    addFirst(newNode);

              else if (previous == tail)

                    addLast(newNode);

              else {

                    SinglyLinkedListNode next = previous.next;

                    previous.next = newNode;

                    newNode.next = next;

              }

        }

    }

}    

答案 1 :(得分:1)

问题目前的格式并不清楚。如果您尝试在列表上执行某些自定义操作,请更好地包装列表界面并在列表上执行操作

import java.util.List;


public class SinglyList<T>
{
    List<T> list;

    private SinglyList(List<T> list)
    {
        super();
        this.list = list;
    }

    public T delete(){
        return list.remove(0);
    }
}

答案 2 :(得分:0)

Swift program to delete element of a single linked list

import UIKit

class Node<T: Equatable>{
    var value:T?
    var nextNode:Node?
}

class LinkedList<T: Equatable>{
    var headNode: Node = Node<T>()

    func insert(value: T){
        if self.headNode.value == nil{
            print("The item \(value) is inserted")
            self.headNode.value = value
        }else{
            var lastNode = self.headNode
            while lastNode.nextNode != nil {
                lastNode = lastNode.nextNode!
            }
            let newNode = Node<T>()
            newNode.value = value
            print("The item \(value) is inserted")
            lastNode.nextNode = newNode
        }
    }

    func remove(value: T){
        if self.headNode.value == value{
            print("The item \(value) is removed")
            if self.headNode.nextNode != nil{
               self.headNode = self.headNode.nextNode!
            }else{
                self.headNode.value = nil
            }
        }else{
            var lastNode = self.headNode
            var found = true
            while lastNode.nextNode?.value != value{
                if lastNode.nextNode != nil{
                   lastNode = lastNode.nextNode!
                }else{
                    found = false
                    break
                }
            }
            if found{
                print("The item \(value) is removed")
                if lastNode.nextNode?.nextNode != nil{
                    lastNode.nextNode = lastNode.nextNode?.nextNode!
                }else{
                    //if at the end, the next is nil
                    lastNode.nextNode = nil
                }
            }else{
                print("---------------")
                print("The item \(value) is not found")
                print("---------------")
            }
        }
    }

    func printAllKeys() {
        var currentNode: Node! = headNode
        print("---------------")
        print("Items in LINKED LIST")
        while currentNode != nil && currentNode.value != nil {
            print(currentNode.value!)
            currentNode = currentNode.nextNode
        }
        print("---------------")
    }
}

var linkedList = LinkedList<Int>()
linkedList.insert(value: 10)
linkedList.insert(value: 20)
linkedList.insert(value: 30)
linkedList.insert(value: 40)
linkedList.insert(value: 50)
linkedList.printAllKeys()
linkedList.remove(value: 10)
linkedList.printAllKeys()
linkedList.remove(value: 30)
linkedList.printAllKeys()
linkedList.remove(value: 40)
linkedList.printAllKeys()
linkedList.remove(value: 40)
linkedList.printAllKeys()