gpt4 book ai didi

c - C 同义词库程序中的插入函数无法正常工作

转载 作者:行者123 更新时间:2023-11-30 17:36:50 24 4
gpt4 key购买 nike

我已经开始创建一个程序并使用我所理解的 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);
}*/

最佳答案

  1. 您创建了一个节点,但没有创建顶点,因此您没有添加任何新数据 - 您用新数据替换旧数据

  2. 程序至少有 2 个地方应该失败或执行类似 UB 的操作:

一个地方

if (v==NULL)    //*if vertex is empty
{
new = create(word);
newsyn = create(syn);
v -> next = new; //<--- you use v without allocating memeory

下面有同样的问题:

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

关于c - C 同义词库程序中的插入函数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22563706/

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