0

I have started creating a program and used Graph ADT as I understood it. But I still have a problem in inserting my set of words. When I try to insert another set of words, the words that I have inserted before that seem to be gone from the list even though I haven't terminated the program. I don't understand PLEASE HELP

#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);
}*/
moondrums
  • 21
  • 3

1 Answers1

0
  1. You create a node but do not create a vertex, so you do not add any new data - you replace old data with new one

  2. There are at least 2 places where the program should fail or do something like UB:

one place

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

and the same issue a bit below:

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
VladimirM
  • 817
  • 5
  • 7