首页 > 解决方案 > scanf() 无休止地运行,程序在一条语句上停止运行

问题描述

这是连接图的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');
}

标签: cdata-structures

解决方案


OPs源代码的摘录:

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

其意图可能是阅读一个字母(yn)并\n确认输入。

所以,第二 ch=getchar();个(for 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');

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


推荐阅读