【问题标题】:Making a menu in linked list在链表中制作菜单
【发布时间】:2022-01-13 06:07:21
【问题描述】:

我想为这个链表添加一个用户菜单,但是程序没有运行。

程序允许用户在当前列表的末尾添加一个节点。
在当前列表的开头添加节点。 在特定索引处添加节点。 删除给定列表中的最后一个节点并更新尾节点。 删除给定列表中的第一个节点并更新头节点。 删除给定列表中给定索引处的节点并更新头节点。 检查列表中是否存在具有给定值的节点,返回 true 或 false。 检查列表中是否存在具有给定值的节点,返回给定值的索引 列表。

我运行它时什么也没有出现。 也没有错误信息。

你如何解决这个问题?

提前谢谢你。

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

}

【问题讨论】:

    标签: java data-structures linked-list switch-statement singly-linked-list


    【解决方案1】:

    我做了一个更好的版本。

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

    }

    【讨论】:

    • 谢谢。但它不像以前那样运行了?
    • 是的,我还没有弄清楚如何让它运行。我只是让代码更具可读性。
    【解决方案2】:

    我通过重新安排东西修复了错误,但程序没有运行,但我希望这有助于给你一个想法。

    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..");  
            }  
        }  
    }  
    

    }

    【讨论】:

      【解决方案3】:

      您在 switch 语句中的方法调用需要一个参数。

      // 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()
      

      【讨论】:

      • 谢谢。我已经编辑并修复了错误,但程序没有运行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 2013-10-21
      • 2021-01-07
      • 2020-12-18
      • 2015-05-28
      相关资源
      最近更新 更多