我想为这个链表添加一个用户菜单,但程序没有运行。
该程序允许用户在当前列表的末尾添加一个节点
在当前列表的开头添加节点。在特定索引处添加节点。删除给定列表中的最后一个节点并更新尾部节点。删除给定列表中的第一个节点并更新头部节点。删除给定列表中给定索引处的节点并更新头节点。检查列表中是否存在具有给定值的节点,返回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("nn*********Main Menu*********n");
System.out.println("nChoose one option from the following list ...n");
System.out.println("n1.Insert noden2.Insert at startn3.Insert certain indexn4.Remove node");
System.out.println("n5.Remove from the startn6.Delete node after certain indexn7.Search for an elementn8.Search And Return Indexn9.Display nodesn");
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;
}
}
}
我通过重新安排内容修复了错误,但程序没有运行,但我希望这能给你一个想法。
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("nn*********Main Menu*********n");
System.out.println("nChoose one option from the following list ...n");
System.out.println("n1.Insert noden2.Insert at startn3.Insert certain indexn4.Remove node");
System.out.println("n5.Remove from the startn6.Delete node after certain indexn7.Search for an elementn8.Search And Return Indexn9.Display nodesn");
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..");
}
}
}
}
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()
我做了一个更好的版本。
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 Listn");
char ch;
do
{
System.out.println("nChoose one option from the following list ...n");
System.out.println("n1.Insert noden2.Insert at startn3.Insert certain indexn4.Remove node");
System.out.println("n5.Remove from the startn6.Delete node after certain indexn7.Search for an elementn8.Search And Return Indexn9.Display nodesn");
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');
}
}
}