实现很好。没有编译错误,一切正常。只是do-while结构似乎在不应该重复的时候重复了两次。例如,如果为"插入新节点"选择"i",则case应该切换到"i",调用insert函数,然后遍历列表。它确实会这样做,但也会再次打印do-while循环中的所有内容,并打印默认情况。
我将在我的代码输出后发布代码。
下面是我的C代码,它实现了一个链表,以及它的基本遍历和插入函数:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void traverse(struct node *);
void insert(struct node **);
int main()
{
char choice;
struct node *head,**tohead;
head=(struct node *)malloc (sizeof(struct node));
tohead=&head;
//STACKOVERFLOW NOTE: this is where I think the relevant portion begins
do
{
printf("nChoose operation to perform:n");
printf("tInsert a node(i)n");
printf("tShow currenct linked list(s)n");
printf("tQuit without saving changes(q):t");
scanf("%c",&choice);
switch(choice)
{
case 'i':
insert(tohead);
break;
case 's':
traverse(head);
break;
case 'q':
printf("QUITTINGn");
break;
default:
printf("Invalid choicen");
}
}while(choice!='q');
}//STACKOVERFLOW NOTE: This is where I think relevant portion ends. Possibly.
void traverse(struct node *p)
{
printf("Linked list looks like: ");
if(p->next==0)
{
printf("nothing. It's EMPTY");
p->next==NULL;
}
else
{ while(p->next!=NULL)
{
printf("%dt",p->data);
p=p->next;
}
printf("%d",p->data);
}
printf("n");
}
void insert(struct node **pp)
{
int value,position;
struct node *q;
q=*pp;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
printf("Insert what number?:t");
scanf("%d",&value);
printf("In what position? Push '0' for last,'1' for first");
printf("nttORnenter position no.:t");
scanf("%d",&position);
newnode->data=value;
if(position==1)
{ newnode->next=q;
*pp=newnode;
}
else if(position==0)
{
while(q->next!=NULL)
q=q->next;
q->next=newnode;
newnode->next=NULL;
}
else if(position>1)
{
int i;
for(i=1;i<position-1;i++)
q=q->next;
newnode->next=q->next;
q->next=newnode;
}
else
printf("Invalid Position");
traverse(*pp);
}
注意do-while语句是如何重复的,并且说'Invalid choice'
user@host: ~ $ ./a.o ut
Choose operation to perform:
Insert a node(i)
Show currenct linked list(s)
Quit without saving changes(q): i
Insert what number?: 5
In what position? Push '0' for last,'1' for first
OR
enter position no.: 0
Linked list looks like: 0 5
Choose operation to perform:
Insert a node(i)
Show currenct linked list(s)
Quit without saving changes(q): Invalid choice
Choose operation to perform:
Insert a node(i)
Show currenct linked list(s)
Quit without saving changes(q): i
Insert what number?: 3
In what position? Push '0' for last,'1' for first
OR
enter position no.: 1
Linked list looks like: 3 0 5
Choose operation to perform:
Insert a node(i)
Show currenct linked list(s)
Quit without saving changes(q): Invalid choice
Choose operation to perform:
Insert a node(i)
Show currenct linked list(s)
Quit without saving changes(q): i
Insert what number?: 2
In what position? Push '0' for last,'1' for first
OR
enter position no.: 3
Linked list looks like: 3 0 2 5
Choose operation to perform:
Insert a node(i)
Show currenct linked list(s)
Quit without saving changes(q): Invalid choice
Choose operation to perform:
Insert a node(i)
Show currenct linked list(s)
Quit without saving changes(q): s
Linked list looks like: 3 0 2 5
Choose operation to perform:
Insert a node(i)
Show currenct linked list(s)
Quit without saving changes(q): Invalid choice
Choose operation to perform:
Insert a node(i)
Show currenct linked list(s)
Quit without saving changes(q): q
QUITTING
user@host:~$
当您使用Scanf
输入任何内容时,您将以换行符结束。问题是scanf
函数只提取请求的输入,而将换行符留在输入缓冲区中,因此在下一次迭代中,程序读取并提取该换行符,并将其视为无效输入并再次循环。
这很容易解决:在"%c"
格式代码之前添加额外的空间,所以它是
scanf(" %c",&choice);
/* ^ */
/* | */
/* Note space here */
这将告诉scanf
读取和丢弃所有前导空格(换行符是)。
我还建议您阅读例如scanf
参考。