插入函数在C中的同义词库程序中工作不正常
我已经开始创建一个程序,并按照我的理解使用Graph ADT。 但是我在插入我的一组单词时仍然有问题。当我尝试插入另一组单词时,我之前插入的单词似乎已从列表中删除,即使我尚未终止程序。我不明白,请帮忙插入函数在C中的同义词库程序中工作不正常,c,graph,insert,adt,thesaurus,C,Graph,Insert,Adt,Thesaurus,我已经开始创建一个程序,并按照我的理解使用Graph ADT。 但是我在插入我的一组单词时仍然有问题。当我尝试插入另一组单词时,我之前插入的单词似乎已从列表中删除,即使我尚未终止程序。我不明白,请帮忙 #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> struct node { char word [20]; struct
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char word [20];
struct node *below, *synonym, *nxt;
};
struct vertex
{
struct node *next; //vertex node that points to my list of nodes
};
typedef struct vertex V;
typedef struct node N;
void display(N *node);
int existingSyn(N *n, char s[20]);
N *search(V *v, char w[20]);
int checkNode(V *v, char w[20]);
V *insert(V *v, char word [20], char syn [20]);
N *create(char w [20]);
N *create(char w [20])
{
N *new;
new = (N*)malloc(sizeof(N));
new -> below = NULL;
new -> synonym = NULL;
new -> nxt = NULL;
strcpy(new->word, w);
return (new);
}
int main(void)
{
int choice, flag;
char word [20], syn [20], searchKey[20];
int logIn;
V *v;
N *ss;
v = NULL;
ss = NULL;
while(1)
{
clrscr();
printf("MENU\n1. Search\n2. Insert\n3. Exit \nCHOICE: "); //MENU
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nWORD: ");
scanf("%s", searchKey);
flag = checkNode(v, searchKey); //checks node if existing in list//
if(flag == 0) //if true
{
ss = search(v, searchKey);
printf("%s has the following synonyms: ", searchKey);
display(ss);
getch();
}
else if(flag !=0) //if not existing
{
printf("\nWORD NOT FOUND!");
getch();
}
break;
case 2: //here lies my problem//
printf("\nInput word: "); //asks for word to insert
scanf("%s", word);
getch();
printf("Synonym: "); //asks for synonym
scanf("%s", syn);
getch();
v = insert(v, word, syn);
getch();
break;
case 3:
exit(0);
default:
printf("\nINVALID INPUT. Press any key to try again...");
break;
}
}
}
V *insert(V *v, char word [20], char syn [20])
{
int flag, existing;
N *new, *newsyn, *temp;
if (v==NULL) //*if vertex is empty
{
new = create(word);
newsyn = create(syn);
v -> next = new;
new -> synonym = newsyn;
printf("\nINSERTED!\n");
}
else
{
flag = checkNode(v, word);
if (flag !=0)
{
temp = v -> next;
while(temp!=NULL)
{
temp = temp -> below;
}
new = create(word);
newsyn = create(syn);
temp -> below = new;
new -> synonym = newsyn;
printf("\nINSERTED!\n");
}
else if(flag == 0)
{
new = search(v,word);
existing = existingSyn(new,syn);
if(existing !=0)
{
temp = new -> synonym;
while(temp->nxt!=NULL)
{
temp = temp -> nxt;
}
newsyn = create(syn);
temp -> nxt = newsyn;
printf("\nINSERTED!\n");
}
else if(existing == 1)
{
printf("\nSynonym already exist in %s's records.", word);
getch();
}
}
}
return (v);
}
int checkNode(V *v, char w[20])
{
int flag;
N *temp;
temp = v -> next;
while(temp !=NULL)
{
/*if(temp->word == w)
{
printf("\nCHECKEDD!");
return (1);
}**/
if(strcmp(temp -> word,w) == 0)
{
printf("\nCHECKED!\n");
return (0);
}
temp = temp -> below;
}
flag = strcmp(temp -> word,w);
return (flag);
}
N *search(V *v, char w[20])
{
N *temp;
temp = v -> next;
while(temp != NULL)
{
if(strcmp(temp->word,w)==0)
{
return (temp);
}
temp = temp -> below;
}
return (temp);
}
int existingSyn(N *n, char s[20])
{
int flag;
N *temp;
temp = n -> synonym;
while(temp != NULL)
{
/*if(temp->word==s)
{
return (1);
}*/
if(strcmp(temp -> word,s)==0)
{
return (0);
}
temp = temp -> nxt;
}
flag = strcmp(temp -> word,s);
return (flag);
}
void display(N *node)
{
N *temp;
temp = node -> synonym;
while(temp != NULL)
{
printf("\n%s", temp->word);
temp = temp -> nxt;
}
}
/*
int Admin()
{
char user [20];
int pw;
int flag = 0;
clrscr();
printf("\n--ADMIN--\n");
printf("USERNAME: ");
scanf("%s", user);
printf("\nPASSWORD: ");
scanf("%d", &pw);
getch();
if(strcmp(user,"admin")==0 && pw == 123)
{
flag = 1;
}
else
{
printf("\nInvalid log -in!!");
}
return (flag);
}*/
#包括
#包括
#包括
#包括
结构节点
{
字符字[20];
结构节点*下,*同义词,*nxt;
};
结构顶点
{
struct node*next;//指向我的节点列表的顶点节点
};
typedef结构顶点V;
typedef结构节点N;
无效显示(N*节点);
int-existingSyn(N*N,chars[20]);
N*搜索(V*V,字符w[20]);
int checkNode(V*V,charw[20]);
V*insert(V*V,char-word[20],char-syn[20]);
N*创建(字符w[20]);
N*创建(字符w[20])
{
N*新的;
新=(N*)malloc(sizeof(N));
新建->低于=空;
新建->同义词=NULL;
新建->nxt=NULL;
strcpy(new->word,w);
返回(新);
}
内部主(空)
{
int选择,flag;
char-word[20],syn[20],searchKey[20];
int登录;
V*V;
N*ss;
v=零;
ss=NULL;
而(1)
{
clrsc();
printf(“菜单\n1.搜索\n2.插入\n3.退出\n选择:”;//菜单
scanf(“%d”,选择(&C);
开关(选择)
{
案例1:
printf(“\nWORD:”);
scanf(“%s”,搜索键);
flag=checkNode(v,searchKey);//检查列表中是否存在节点//
if(flag==0)//如果为true
{
ss=搜索(v,搜索键);
printf(“%s具有以下同义词:”,searchKey);
显示器(ss);
getch();
}
else if(flag!=0)//如果不存在
{
printf(“\n找不到单词!”);
getch();
}
打破
案例2://这就是我的问题所在//
printf(“\n输入单词:”);//要求插入单词
scanf(“%s”,单词);
getch();
printf(“同义词:”;//请求同义词
scanf(“%s”,syn);
getch();
v=插入(v,字,syn);
getch();
打破
案例3:
出口(0);
违约:
printf(“\n无效输入。按任意键重试…”);
打破
}
}
}
V*插入(V*V,字符字[20],字符同步[20])
{
int标志,现有;
N*新,*新闻同步,*临时;
如果(v==NULL)//*如果顶点为空
{
新建=创建(word);
newsyn=create(syn);
v->next=新建;
新建->同义词=newsyn;
printf(“\n插入!\n”);
}
其他的
{
标志=检查节点(v,字);
如果(标志!=0)
{
temp=v->next;
while(temp!=NULL)
{
温度=温度->以下;
}
新建=创建(word);
newsyn=create(syn);
温度->低于=新;
新建->同义词=newsyn;
printf(“\n插入!\n”);
}
else if(标志==0)
{
新建=搜索(v,word);
现有=现有同步(新,同步);
如果(现有!=0)
{
temp=新建->同义词;
同时(临时->nxt!=NULL)
{
温度=温度->nxt;
}
newsyn=create(syn);
temp->nxt=newsyn;
printf(“\n插入!\n”);
}
else if(现有==1)
{
printf(“\n同步已存在于%s的记录中。”,word);
getch();
}
}
}
返回(v);
}
int checkNode(V*V,字符w[20])
{
int标志;
N*温度;
temp=v->next;
while(temp!=NULL)
{
/*如果(临时->单词==w)
{
printf(“\n勾选!”);
申报表(1);
}**/
如果(strcmp(temp->word,w)==0)
{
printf(“\n检查!\n”);
返回(0);
}
温度=温度->以下;
}
标志=strcmp(临时->字,w);
返回(标志);
}
N*搜索(V*V,字符w[20])
{
N*温度;
temp=v->next;
while(temp!=NULL)
{
如果(strcmp(temp->word,w)==0)
{
返回(临时);
}
温度=温度->以下;
}
返回(临时);
}
int existingSyn(N*N,字符s[20])
{
int标志;
N*温度;
temp=n->同义词;
while(temp!=NULL)
{
/*如果(临时->单词==s)
{
申报表(1);
}*/
如果(strcmp(temp->word,s)==0)
{
返回(0);
}
温度=温度->nxt;
}
flag=strcmp(临时->单词,s);
返回(标志);
}
无效显示(N*节点)
{
N*温度;
temp=节点->同义词;
while(temp!=NULL)
{
printf(“\n%s”,临时->单词);
温度=温度->nxt;
}
}
/*
int Admin()
{
字符用户[20];
int pw;
int标志=0;
clrsc();
printf(“\n--ADMIN--\n”);
printf(“用户名:”);
scanf(“%s”,用户);
printf(“\n密码:”);
扫描频率(“%d”和&pw);
getch();
if(strcmp(用户,“管理员”)==0&&pw==123)
{
flag=1;
}
其他的
{
printf(“\n无效登录!!”);
}
返回(标志);
}*/
顶点
,因此不添加任何新数据-用新数据替换旧数据if (v==NULL) //*if vertex is empty
{
new = create(word);
newsyn = create(syn);
v -> next = new; //<--- you use v without allocating memeory
if(v==NULL)//*如果顶点为空
{
新建=创建(word);
newsyn=create(syn);
v->next=new;//next;
while(temp!=NULL)
{
临时工=
temp = v -> next;
while(temp!=NULL)
{
temp = temp -> below;
}
new = create(word);
newsyn = create(syn);
temp -> below = new; // <-- you use temp but after while loop it should be NULL