Making a menu in linked list

Viewed 269

I want to add a user menu for this linked list but the program doesn't run.

The program lets the user add a node at the end of the current list.
Adds node at the start of the current list. Adds node at the certain index. Removes the last node in the given list and updates the tail node. Removes the first node in the given list and updates the head node. Removes the node at the given index in the given list and updates the head node. Checks if a node with the given value exists in the list, returns true or false. Checks if a node with the given value exists in the list, returns the index of the given value in the list.

There's nothing that appears when I run it. There's no error message too.

How do you fix this?

Thank you in advance.

class Node{
int val;
int target;
int index;
Node next;
Node head; 
Node tail;

public Node(int val){
    this.val = val;
                    }
{  
    int choice =0;  
    while(choice != 9)   
    {  
         System.out.println("\n\n*********Main Menu*********\n");  
         System.out.println("\nChoose one option from the following list ...\n");  
         System.out.println("\n1.Insert node\n2.Insert at start\n3.Insert certain index\n4.Remove node");
         System.out.println("\n5.Remove from the start\n6.Delete node after certain index\n7.Search for an element\n8.Search And Return Index\n9.Display nodes\n");     
         System.out.println("\nEnter your choice?\n");    

        switch(choice)  
        {  
            case 1:  
            addNode(val);      
            break;  
            case 2:  
            addNodeAtStart(val);         
            break;  
            case 3:  
            addNodeAtCertainIndex(val,index);       
            break;  
            case 4:  
            removeNode();       
            break;  
            case 5:  
            removeNodeAtStart();        
            break;  
            case 6:  
            removeNodeAtCertainIndex(index);          
            break;  
            case 7:  
            search(target);         
            break;  
            case 8:  
            searchAndReturnIndex(target);        
            break;  
            case 9:  
            printLinkedList();  
            break;  
            default:  
                System.out.println("Please enter valid choice..");  
        }  
    }  
}  


public void addNode(int val ){
    
    if(head==null){
    Node temp = new Node(val);
    head = temp;
    tail = temp;
    }else{
    tail.next = new Node(val);
    tail = tail.next;
    }
    }
public void addNodeAtStart(int val ){
    
    if(head==null){
    Node temp = new Node(val);
    head = temp;
    tail = temp;
    }else{
    Node temp = new Node(val);
    temp.next = head;
    head = temp;
    }
    }
public void addNodeAtCertainIndex(int val,int index){
      
     
    Node temp = head;
    int count = 0;
    while(temp!=null && ++count!=index)
    temp = temp.next;
    Node node = new Node(val);
    node.next = temp.next;
    temp.next = node;
    }
public void removeNode(){
    Node temp = head;
    while(temp.next!=null && temp.next.next!=null){
    temp = temp.next;
    }
    temp.next = null;
    tail = temp;
    }
public void removeNodeAtStart(){
    head = head.next;
    }
public void removeNodeAtCertainIndex(int index){
    
    Node temp = head;
    int count = 0;
    while(temp!=null && ++count!=index)
    temp = temp.next;
    temp.val = temp.next.val;
    temp.next = temp.next.next;
    }
public boolean search(int target){
     
    Node temp = head;
    while(temp!=null){
    if(temp.val == target)
    return true;
    }
    return false;
    }
public int searchAndReturnIndex(int target ){
    
    Node temp = head;
    int count = 0;
    while(temp!=null){
    count++;
    if(temp.val==target) return count;
    }
    return -1;
    }
public void printLinkedList(){
    System.out.println();
    Node temp = head;
    while(temp!=null){
    System.out.print(" "+temp.val);
    temp = temp.next;
    }
}

}

3 Answers

I fixed the errors by re-arranging stuff but the program doesn't run but I hope this helps to give you an idea.

class Node{
int val;
Node next;
Node head; 
Node tail;

public Node(int val){
    this.val = val;
                    }



public void addNode(){
    int val = 0;
    if(head==null){
    Node temp = new Node(val);
    head = temp;
    tail = temp;
    }else{
    tail.next = new Node(val);
    tail = tail.next;
    }
    }
public void addNodeAtStart(){
    int val = 0;
    if(head==null){
    Node temp = new Node(val);
    head = temp;
    tail = temp;
    }else{
    Node temp = new Node(val);
    temp.next = head;
    head = temp;
    }
    }
public void addNodeAtCertainIndex(){
    int val = 0; 
    int index = 0;
    Node temp = head;
    int count = 0;
    while(temp!=null && ++count!=index)
    temp = temp.next;
    Node node = new Node(val);
    node.next = temp.next;
    temp.next = node;
    }
public void removeNode(){
    Node temp = head;
    while(temp.next!=null && temp.next.next!=null){
    temp = temp.next;
    }
    temp.next = null;
    tail = temp;
    }
public void removeNodeAtStart(){
    head = head.next;
    }
public void removeNodeAtCertainIndex(){
    int index = 0;
    Node temp = head;
    int count = 0;
    while(temp!=null && ++count!=index)
    temp = temp.next;
    temp.val = temp.next.val;
    temp.next = temp.next.next;
    }
public boolean search(){
    int target = 0;
    Node temp = head;
    while(temp!=null){
    if(temp.val == target)
    return true;
    }
    return false;
    }
public int searchAndReturnIndex(){
    int target = 0;
    Node temp = head;
    int count = 0;
    while(temp!=null){
    count++;
    if(temp.val==target) return count;
    }
    return -1;
    }
public void printLinkedList(){
    System.out.println();
    Node temp = head;
    while(temp!=null){
    System.out.print(" "+temp.val);
    temp = temp.next;
    }
}


{  
    int choice =0;  
    while(choice != 9)   
    {  
         System.out.println("\n\n*********Main Menu*********\n");  
         System.out.println("\nChoose one option from the following list ...\n");  
         System.out.println("\n1.Insert node\n2.Insert at start\n3.Insert certain index\n4.Remove node");
         System.out.println("\n5.Remove from the start\n6.Delete node after certain index\n7.Search for an element\n8.Search And Return Index\n9.Display nodes\n");     
         System.out.println("\nEnter your choice?\n");    

        switch(choice)  
        {  
            case 1:  
            addNode();      
            break;  
            case 2:  
            addNodeAtStart();         
            break;  
            case 3:  
            addNodeAtCertainIndex();       
            break;  
            case 4:  
            removeNode();       
            break;  
            case 5:  
            removeNodeAtStart();        
            break;  
            case 6:  
            removeNodeAtCertainIndex();          
            break;  
            case 7:  
            search();         
            break;  
            case 8:  
            searchAndReturnIndex();        
            break;  
            case 9:  
            printLinkedList();  
            break;  
            default:  
                System.out.println("Please enter valid choice..");  
        }  
    }  
}  

}

Your method calls in the switch statement require a parameter.

// You call 
addNode();     
// required is
addNode(int val)
// which means you need to pass the value to be added in the switch 
//statement ;)
// Therefore the method calls without any parameter doesnt throw any error.
removeNode()

I made a better version.

  import java.util.Scanner;

class Node{
int val;
int target;
int index;
Node next;
Node head; 
Node tail;

public Node(int val){
    this.val = val;
                    }

public void addNode(int val ){
    
    if(head==null){
    Node temp = new Node(val);
    head = temp;
    tail = temp;
    }else{
    tail.next = new Node(val);
    tail = tail.next;
    }
    }
public void addNodeAtStart(int val ){
    
    if(head==null){
    Node temp = new Node(val);
    head = temp;
    tail = temp;
    }else{
    Node temp = new Node(val);
    temp.next = head;
    head = temp;
    }
    }
public void addNodeAtCertainIndex(int val,int index){
      
     
    Node temp = head;
    int count = 0;
    while(temp!=null && ++count!=index)
    temp = temp.next;
    Node node = new Node(val);
    node.next = temp.next;
    temp.next = node;
    }
public void removeNode(){
    Node temp = head;
    while(temp.next!=null && temp.next.next!=null){
    temp = temp.next;
    }
    temp.next = null;
    tail = temp;
    }
public void removeNodeAtStart(){
    head = head.next;
    }
public void removeNodeAtCertainIndex(int index){
    
    Node temp = head;
    int count = 0;
    while(temp!=null && ++count!=index)
    temp = temp.next;
    temp.val = temp.next.val;
    temp.next = temp.next.next;
    }
public boolean search(int target){
     
    Node temp = head;
    while(temp!=null){
    if(temp.val == target)
    return true;
    }
    return false;
    }
public int searchAndReturnIndex(int target ){
    
    Node temp = head;
    int count = 0;
    while(temp!=null){
    count++;
    if(temp.val==target) return count;
    }
    return -1;
    }
public void printLinkedList(){
    System.out.println();
    Node temp = head;
    while(temp!=null){
    System.out.print(" "+temp.val);
    temp = temp.next;
    }
}

public class SinglyLinkedList
{    
    public void main(String[] args)
    {             
        Scanner scan = new Scanner(System.in);


        System.out.println("Singly Linked List\n");   
        
        char ch;
       
        do
        {
            System.out.println("\nChoose one option from the following list ...\n");  
            System.out.println("\n1.Insert node\n2.Insert at start\n3.Insert certain index\n4.Remove node");
            System.out.println("\n5.Remove from the start\n6.Delete node after certain index\n7.Search for an element\n8.Search And Return Index\n9.Display nodes\n");     
            System.out.println("\nEnter your choice?\n");  
            int choice;  
            choice = scan.nextInt();
                      
            switch (choice)
            {
            case 1 : 
                System.out.println("Enter integer element to insert");
                addNode( scan.nextInt() );  

                break;                          
            case 2 : 
                System.out.println("Enter integer element to insert");
                addNodeAtStart( scan.nextInt() );  

                break;                         
            case 3 : 
                System.out.println("Enter integer element to insert"); 
                addNodeAtCertainIndex(val,index);  
                break;                                          
            case 4 : 
                System.out.println("Enter integer element to remove"); 
                removeNode();
                break;
            case 5 : 
                System.out.println("Enter integer element to remove");
                removeNodeAtStart(); 
                break;                   
            case 6 : 
                System.out.println("Enter integer element to remove");
                removeNodeAtCertainIndex( scan.nextInt() );  

                break;  
            case 7 : 
                System.out.println("Enter integer element to search");
                search( scan.nextInt() );  

                break;   
            case 8 : 
                System.out.println("Enter integer element to search");
                searchAndReturnIndex( scan.nextInt() );  

                break;   
            case 9 :  
                printLinkedList();  
                break;      
                
             default : 
                System.out.println("Wrong Entry \n ");
                break;   
            }
           
            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = scan.next().charAt(0);                        
        } while (ch == 'Y'|| ch == 'y');               
    }

}

}

Related