Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c编程添加列表中已存在的站点时出现分段错误_C_Database - Fatal编程技术网

使用c编程添加列表中已存在的站点时出现分段错误

使用c编程添加列表中已存在的站点时出现分段错误,c,database,C,Database,我试图添加一个站点列表,如果站点名称已经出现在单链接列表中,则不需要添加站点名称。但当我在findstation函数中进行字符串比较时,我得到了一个分段错误。我调试已经很久了。我找不到哪里出了问题。如果我知道我的错误,那将是一个很大的帮助。提前谢谢 #include<stdio.h> #include<stdbool.h> #include<stdlib.h> #include<string.h> struct s

我试图添加一个站点列表,如果站点名称已经出现在单链接列表中,则不需要添加站点名称。但当我在findstation函数中进行字符串比较时,我得到了一个分段错误。我调试已经很久了。我找不到哪里出了问题。如果我知道我的错误,那将是一个很大的帮助。提前谢谢

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

    struct station
    {
        char station[100];
        struct station *next;
    }destinations;


    struct station *findStation(struct station *stations,char *station_name);
    void addStation(struct station **stations,char *station_name);
    //void addDestination(struct station *stations, char *station_name);
    void printStations(struct station *stations);
    //void removeStation(struct station **stations, char *station_name);
    int findShortestPath(struct station *stations, struct station *a, struct station *b);

    int main()
    {
        struct station *source = NULL;
        char *station_name;
        char c;
        bool quit=false;
        while(!quit)
        {
            scanf("%c",&c);
            switch(c)
            {
                case 'a':
                {
                    scanf(" %s", station_name);
                    addStation(&source,station_name);
                    break;
                }
                case 'd':
                {
                    scanf(" %s",station_name);
                    //addDestination(&destinations,station_name);
                    break;
                }
                case 'p':
                {
                    printStations(source);
                    break;

                }
                case 'q':
                {
                    quit=true;
                    break;
                }
                default:
                    break;
            }
        }
        return 0;
    }


    struct station *findStation(struct station *stations,char *station_name)
    {
        //returns a pointer to the station with the name station name in the list stations
            struct station *find=NULL;
            printf("h1");
            //printf("%s",station_name);
            //printStations(stations);
            while((stations!=NULL))
            {
                if(strcmp(stations->station,station_name)!=0)
                {
                    printf("%s\n", stations->station);
                    printf("%s\n", station_name);
                    stations=stations->next;
                    find=stations;
                    //printf("hello");
                }
                else
                {
                  find=NULL; 
                 // printf("bye");
                  return ;
                }
            }
            //printStations(find);
            return find;
    }

    void addStation(struct station **stations,char *station_name)
    {
        struct station *find;
        struct station *temp;
        temp= (struct station *)malloc(sizeof(struct station ));
        //adds a new station with the name station name to the front of the list stations
        if(*stations== NULL)
        {
            strcpy(temp->station,station_name);
            temp->next=NULL;
            (*stations)=temp;

        }
        else
        {
            //printf("hi");
            find= findStation(*stations,station_name);
            printStations(find);
            if(find !=NULL)
            {
                printf("Station station_name already exists.");
                    exit (0);
            }
            else
            {
            strcpy(temp->station,station_name);
            temp->next=NULL;
            temp->next=(*stations);
            (*stations)=temp;
            }

        }

    }

        enter code here
#包括
#包括
#包括
#包括
结构站
{
char站[100];
结构站*下一站;
}目的地;
结构站*查找站(结构站*站,字符*站名称);
void addStation(结构站**站,字符*站名称);
//void addDestination(结构站点*站点,字符*站点名称);
无效打印站(结构站*站);
//无效再投资(结构车站**车站,字符*车站名称);
int findShortestPath(结构站*站、结构站*a、结构站*b);
int main()
{
结构站*source=NULL;
字符*站名;
字符c;
bool-quit=false;
而(!退出)
{
scanf(“%c”、&c);
开关(c)
{
案例“a”:
{
scanf(“%s”,站点名称);
addStation(源和站名);
打破
}
案例“d”:
{
scanf(“%s”,站点名称);
//addDestination(目的地和目的地,站点名称);
打破
}
案例“p”:
{
印刷站(来源);
打破
}
案例‘q’:
{
退出=真;
打破
}
违约:
打破
}
}
返回0;
}
结构站*查找站(结构站*站,字符*站名称)
{
//返回指向“桩号”列表中具有桩号名称的桩号的指针
结构站*find=NULL;
printf(“h1”);
//printf(“%s”,站点名称);
//印刷站(站);
while((站!=NULL))
{
if(strcmp(车站->车站,车站名称)!=0)
{
printf(“%s\n”,站->站);
printf(“%s\n”,站点名称);
站点=站点->下一步;
查找=站点;
//printf(“你好”);
}
其他的
{
find=NULL;
//printf(“再见”);
返回;
}
}
//印刷站(查找);
返回查找;
}
void addStation(结构站**站,字符*站名称)
{
结构站*查找;
结构站*温度;
温度=(结构站*)malloc(结构站大小);
//将名为station name的新桩号添加到桩号列表的前面
如果(*站==NULL)
{
strcpy(临时->站点,站点名称);
temp->next=NULL;
(*站)=温度;
}
其他的
{
//printf(“hi”);
查找=查找站点(*站点,站点名称);
印刷站(查找);
if(find!=NULL)
{
printf(“站点名称已存在”);
出口(0);
}
其他的
{
strcpy(临时->站点,站点名称);
temp->next=NULL;
温度->下一步=(*站);
(*站)=温度;
}
}
}
在这里输入代码

这里至少有两个问题:

第一个问题

findStation
功能有几个问题,这是一个已更正的版本:

struct station *findStation(struct station *stations, char *station_name)
{
  //returns a pointer to the station with the name station name in the list stations
  printf("h1");

  while ((stations != NULL))
  {
    if (strcmp(stations->station, station_name) == 0) //<< use == instead of !=
                                                      // read the documentation of `strcmp`
    {
      printf("%s\n", stations->station);
      printf("%s\n", station_name);
      return stations;                                 //<< station found, return it
    }
  }

  return NULL;                                         //<< no station found => return NULL
}
struct station*findStation(struct station*stations,char*station\u name)
{
//返回指向“桩号”列表中具有桩号名称的桩号的指针
printf(“h1”);
while((站!=NULL))
{
if(strcmp(车站->车站,车站名称)==0)//车站);
printf(“%s\n”,站点名称);
返回站;//尝试以下操作:

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

struct station
{
    char station[20];
    struct station *next;
}destinations;


struct station *findStation(struct station *stations,char *station_name);
void addStation(struct station **stations,char *station_name);
//void addDestination(struct station *stations, char *station_name);
// void printStations(struct station *stations);
//void removeStation(struct station **stations, char *station_name);
int findShortestPath(struct station *stations, struct station *a, struct station *b);

int main()
{
    struct station *source = NULL;
    char station_name[20];
    char c;
    bool quit=false;
    while(!quit)
    {
        scanf("%c",&c);
        switch(c)
        {
            case 'a':
            {
                scanf(" %s", station_name);
                addStation(&source,station_name);
                break;
            }
            case 'd':
            {
                scanf(" %s",station_name);
                //addDestination(&destinations,station_name);
                break;
            }
            case 'p':
            {
                // printStations(source);
                break;

            }
            case 'q':
            {
                quit=true;
                break;
            }
            default:
                break;
        }
    }
    return 0;
}


struct station *findStation(struct station *stations,char *station_name)
{
    //returns a pointer to the station with the name station name in the list stations
        struct station *find=NULL;
        printf("h1");
        //printf("%s",station_name);
        //printStations(stations);
        while((stations!=NULL))
        {
            if(strcmp(stations->station,station_name)!=0)
            {
                printf("%s\n", stations->station);
                printf("%s\n", station_name);
                stations=stations->next;
                find=stations;
                printf("Not found any match \n");
            }
            else
            {
              find=NULL; 
             // printf("bye");
             printf("Found any match \n");
              return NULL;
            }
        }
        //printStations(find);
        return find;
}

void addStation(struct station **stations,char *station_name)
{
    struct station *find;
    struct station *temp;
    temp= (struct station *)malloc(sizeof(struct station ));
    //adds a new station with the name station name to the front of the list stations
    if(*stations== NULL)
    {
        strcpy(temp->station,station_name);
        temp->next=NULL;
        (*stations)=temp;
    }
    else
    {
        //printf("hi");
        find= findStation(*stations,station_name);
        // printStations(find);
        if(find == NULL)
        {
            printf("Station station_name already exists.");
            exit (0);
        }
        else
        {
            strcpy(temp->station,station_name);
            temp->next=NULL;
            temp->next=(*stations);
            (*stations)=temp;
        }

    }

}
#包括
#包括
#包括
#包括
结构站
{
查站[20];
结构站*下一站;
}目的地;
结构站*查找站(结构站*站,字符*站名称);
void addStation(结构站**站,字符*站名称);
//void addDestination(结构站点*站点,字符*站点名称);
//无效打印站(结构站*站);
//无效再投资(结构车站**车站,字符*车站名称);
int findShortestPath(结构站*站、结构站*a、结构站*b);
int main()
{
结构站*source=NULL;
char station_名称[20];
字符c;
bool-quit=false;
而(!退出)
{
scanf(“%c”、&c);
开关(c)
{
案例“a”:
{
scanf(“%s”,站点名称);
addStation(源和站名);
打破
}
案例“d”:
{
scanf(“%s”,站点名称);
//addDestination(目的地和目的地,站点名称);
打破
}
案例“p”:
{
//印刷站(来源);
打破
}
案例‘q’:
{
退出=真;
打破
}
违约:
打破
}
}
返回0;
}
结构站*查找站(结构站*站,字符*站名称)
{
//返回指向名为station n的桩号的指针