gpt4 book ai didi

c - scanf() 无限运行,程序在一条语句上停止运行

转载 作者:行者123 更新时间:2023-11-30 16:25:19 25 4
gpt4 key购买 nike

这是连通图的BFT(广度优先遍历)的代码。我运行代码,然后成功创建邻接列表,并成功打印邻接列表,但在它之后,当我从用户处获取输入以从该输入节点启动 BFS 时,程序停止。scanf() 无限运行或其他我无法识别的错误。

#include<stdio.h>
#include<stdlib.h>

typedef struct root //the adjacency list which have all the vertices
{
int info;
struct adjacent *adj;
struct root *next;
} root;
typedef struct adjacent //Linked list which store adjacent nodes of any nodes in adj list.
{
int info;
struct adjacent *adj;
} adjacent;

typedef struct node // to make queue to store nodes to be explored.
{
int info;
struct node *next;
} nodeQ;

void insert(nodeQ **ft,nodeQ **rr,int n) // insert func of Q
{
nodeQ * new=(nodeQ *)malloc(sizeof(nodeQ));
new->info = n;
new->next = NULL;
if(*ft == NULL)
{
*ft=new;
*rr=new;
}
else
{
(*rr)->next = new;
*rr = new;
}
}
int delete(nodeQ **ft,nodeQ **rr) //delete func of Q
{
int value=(*ft)->info;
nodeQ *temp=*ft;
*ft=(*ft)->next;
free(temp);
if(*ft==NULL)
*rr=NULL;
return value;
}

void BFS(int total_nodes,int node_tobe_explored,root *head,nodeQ **ft,nodeQ **rr)
{
printf("ff");
int * visited=(int *)malloc(sizeof(int)*total_nodes);
for(int i=0;i<total_nodes;i++)
visited[i]=0; //initialize all value in visited array with 0
printf("aa");
visited[node_tobe_explored] = 1;
printf("%d",node_tobe_explored);
while(1) // this loop iterates until all nodes are not explored.
{
root *t=head;
while(t->info != node_tobe_explored) // this find the node address(t) of the node_tobe_explored.
t=t->next;
printf("bb");
adjacent * adj_node = t->adj;

while(adj_node)
{
if(visited[adj_node->info] == 0) //if that adjacent node is not visited then also we visit it.
{
int adj_node_val = adj_node->info;
visited[adj_node_val] = 1;
insert(ft,rr,adj_node_val);
printf(", %d",adj_node_val);
}
}
printf("cc");
if(*rr==NULL) //if Q is empty, means all nodes are explored, so we return.
return;
else //otherwise explore first node present in Q
node_tobe_explored = delete(ft,rr);
}
}

int main()
{
char ch;
int no,tot_nodes,start;
nodeQ *front=NULL,*rear=NULL;
printf("enter the no. of nodes: ");
scanf("%d",&tot_nodes);
root *head = NULL;
no = tot_nodes;
while(no!=0)
{ //to make the main chain of adjacency list.
root *new=(root *)malloc(sizeof(root));
new->info = no;
new->adj = NULL;
new->next = head;
head = new;
no--;
}
root *temp = head;
while(temp!=NULL)
{ // to add the adjacent nodes to main chain.

printf("enter the nodes adjacent to %d:\n",temp->info);
do
{
int element;
printf(" enter node: ");
scanf("%d",&element);
adjacent *nw = (adjacent *)malloc(sizeof(adjacent));
nw->info = element;
nw->adj = temp->adj;
temp->adj = nw;

printf("more adjacent nodes y/n: ");
ch=getchar();
ch=getchar();
}while(ch=='Y'||ch=='y');

temp=temp->next;
}
printf("display of the structur of the linked list formed:\n");
root * head1=head;
while(head1) // to display the formed adj. list.
{
printf("%d--",head1->info);
adjacent *t = head1->adj;
while(t)
{
printf("%d,",t->info);
t=t->adj;
}
printf("\n");
head1=head1->next;
}

do
{
printf("enter the node value from which you want to start BFS: ");
printf("before [enter image description here][1]");
int st;
scanf("%d",&st);
printf("after");
BFS(tot_nodes,st,head,&front,&rear); //calling BFS func.
printf("do you want to print more traversals y/n: ");
ch=getchar();
ch=getchar();
}while(ch=='Y'||ch=='y');
}

最佳答案

摘自 OP 源代码:

    printf("do you want to print more traversals y/n: ");
ch=getchar();
ch=getchar();
}while(ch=='Y'||ch=='y');

其目的可能是读取一个字母(yn)和确认输入的 \n

因此,第二个nd ch=getchar();(对于ENTER)会覆盖之前读取的字母。以下修复将改变这一点:

    printf("do you want to print more traversals y/n: ");
ch=getchar();
getchar(); /* ignore the returned value - it's just to consume '\n'. */
}while(ch=='Y'||ch=='y');

顺便说一句。这是应该通过对引用的四行进行逐步调试来发现的东西......

关于c - scanf() 无限运行,程序在一条语句上停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53429368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com