graph - Insert function not working properly in Thesaurus program in C -


i have started creating program , used graph adt understood it. still have problem in inserting set of words. when try insert set of words, words have inserted before seem gone list though haven't terminated program. 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 points 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 following synonyms: ", searchkey);                     display(ss);                     getch();                  }                 else if(flag !=0)         //if not existing                 {                      printf("\nword not found!");                     getch();                 }                 break;              case 2:    //here lies problem//                  printf("\ninput word: ");       //asks word insert                 scanf("%s", word);                               getch();                 printf("synonym: ");            //asks synonym                 scanf("%s", syn);                 getch();                 v = insert(v, word, syn);                      getch();                 break;              case 3:                 exit(0);              default:                 printf("\ninvalid input. press key 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 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 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. you create node not create vertex, not add new data - replace old data new one

  2. there @ least 2 places program should fail or ub:

one place

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

and same issue bit below:

temp = v -> next; while(temp!=null) {    temp = temp -> below; }    new = create(word); newsyn = create(syn); temp -> below = new; // <-- use temp after while loop should null 

Comments

Popular posts from this blog

php - Magento - Deleted Base url key -

javascript - Tooltipster plugin not firing jquery function when button or any click even occur -

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -