简体   繁体   中英

Delete element of singly linked list

such that If the element to be deleted is the first element of the list and the list contains only one element, you only need to assign null to the pfirst and plast. If the element to be deleted is the first element of the list and the list contain more than one element, you need a temporary variable to point to the pfirst then move the pfirst to point to its next element and set the temporary variable to null.

Here is my code that is not working as expected

it seams your question is incomplete because it doesnt have the code with it bu as well i can demonstrate you how to implement and delete a list

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;

              }

        }

    }

}    

include this method in the same class

  // delete an item from the linked list

    public void delete(int pos)

     {

       if (countitem() > 0){ 

       //make sure the list is not empty.

      ListNode<T> temp,del;

      if (pos== 1){

          //delete the first item
            if(countitem()==1){ //The list contains only one item
                pfirst=null;
                plast=null;

              }else
         { 
           //The list contains more than one item
           temp=pfirst;

           pfirst=pfirst.next;

           temp=null;
        }
        System.out.println("Deleted");


     }

        else if (pos > 1 && pos <=countitem()){

          //delete middle item

          temp=pfirst;

          int i;

      for(i=1;i<pos-1;i=i+1)

          {temp=temp.next;} //move to the item staying before the target item to be deleted

         del=temp.next; //target item to be deleted

         temp.next=del.next;

         if(del.next==null)

         plast=temp; //delete last item

         del=null;


         System.out.println("Deleted");

        }

    else System.out.println("Invalid position!");

    }

  else System.out.println("No item found");

 }

Question is not clear in it's present format. If you are trying to do some custom operation/s on a list better wrap a list interface and perform operation on the list

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);
    }
}
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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM