我已经开始创建一个程序并按照我的理解使用 Graph ADT。但是我在插入我的一组单词时仍然存在问题。当我尝试插入另一组单词时,我之前插入的单词似乎从列表中消失了,即使我没有终止程序。我不明白 请帮忙
#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);
}*/