Home > Blockchain >  How do I delete the first node of a Linked List?
How do I delete the first node of a Linked List?

Time:10-18

I am working on a project for my Data Structures class that asks me to write a class to implement a linked list of ints.

  • Use an inner class for the Node.
  • Include the methods below.
  • Write a tester to enable you to test all of the methods with whatever data you want in any order.

I have to create a method called "public int deleteFromFront()". This method is meant to "Delete the node at the front of the list and return the int that was in it or null if the list is empty." I have my code for this method down below. However, when I test this method I get the wrong output. It should return the value of the node that was deleted or null if the list is empty. So for example, if I had a list like so "4 3 10 11 3 15 6 11 18 17 " and I wanted to delete the first node the method should return 4 as 4 is the first node. While the method does delete 4 it returns 3 the value of the new head. Does someone know what I did wrong? and How to fix it?

import java.util.Random;
import java.util.Scanner;

public class LinkedListOfInts {
    Node head;
    Node tail;

    private class Node {
        int value;
        Node nextNode;

        public Node(int value, Node nextNode) {
            this.value = value;
            this.nextNode = nextNode;
        }

    }

    public LinkedListOfInts(LinkedListOfInts other) {
        Node tail = null;
        for (Node n = other.head; n != null; n = n.nextNode) {
            if (tail == null)
                this.head = tail = new Node(n.value, null);
            else {
                tail.nextNode = new Node(n.value, null);
                tail = tail.nextNode;
            }
        }
    }

    public LinkedListOfInts(int[] other) {
        Node[] nodes = new Node[other.length];
        for (int index = 0; index < other.length; index  ) {
            nodes[index] = new Node(other[index], null);
            if (index > 0) {
                nodes[index - 1].nextNode = nodes[index];
            }
        }

        head = nodes[0];
    }

    public LinkedListOfInts(int N, int low, int high) {
        Random random = new Random();
        for (int i = 0; i < N; i  )
            this.addToFront(random.nextInt(high - low)   low);
    }

    public void addToFront(int x) {
        head = new Node(x, head);
    }

    public int deleteFromFront() {
        if (head == null)
            return -1;
        else {
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                head = head.nextNode;
            }
        }
        return head.value;
    }

    public String toString() {
        String result = " ";
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
            result  = ptr.value   " ";
        return result;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
        LinkedListOfInts copy = new LinkedListOfInts(list);
        boolean done = false;
        while (!done) {
            System.out.println("1. Delete from Front");
            System.out.println("2. toString");
            switch (input.nextInt()) {
            case 1:
                System.out.println("Delete an Item at the Front of the List");
                System.out.println(list.deleteFromFront());
                break;
            case 2:
                System.out.println("toString");
                System.out.println(list.toString());
                break;
            }
        }
    }
}

CodePudding user response:

You should keep the old head node value in a variable and, later, delete the head node. Also later, you should return this variable.

  public int deleteFromFront() {
        int headValue = -1;
        if (head == null)
            return headValue;
        else {
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                headValue = head.value;
                head = head.nextNode;
            }
        }
        return headValue;
    }
  • Related