Thread: Linked List problem

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    61

    Linked List problem

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    
    typedef struct node
    {
     int num;
     char last[10];
     char first[10];
     char middle[10];
     char st_add[20];
     char phone_num[10];
     struct node *next;
    } node;
    
    
    node *head=NULL;
    
    
    void main()
    {
     
     char erase[10];
     int nomatch=0;
     int lp=0;
     char choice;
     char ilast[10];
     char ifirst[10];
     char imiddle[10];
     char ist_add[20];
     char iphone_num[10];
     node *p;
     node *save;
     node *save2;
     while (choice!='E')
     {
     clrscr();
     printf("[I] Insert\n");
     printf("[V] View\n");
     printf("[D] Delete\n");
     printf("[E] Exit\n");
     printf("\nEnter your choice here: ");
     scanf("%c", &choice);
     switch (choice)
     {
      case 'I':
          printf("\nEnter your integer here: ");
          scanf("%d", &number);
          printf("\nLast Name: ");
          scanf("%s", ilast);
          printf("\nFirst Name: ");
          scanf("%s", ifirst);
          printf("\nMiddle Name: ");
          scanf("%s", imiddle);
          printf("\nStreet Address: ");
          scanf("%s", ist_add);
          printf("\nPhone Number: ");
          scanf("%s", iphone_num);
          p=(node *)malloc(sizeof(node));
          strcpy(p->last, ilast);
          strcpy(p->first, ifirst);
          strcpy(p->middle, imiddle);
          strcpy(p->st_add, ist_add);
          strcpy(p->phone_num, iphone_num);
          p->next=NULL;
          if (head==NULL)
          {
           head=p;
          }
          else
          {
           save=head;
           while (save->next!=NULL)
           {
            save=save->next;
           }
           save->next=p;
          }
          printf("\n%s, %c, %c was successful inserted!\n", p->last, p->first[0], p->middle[0]);
          getch();
          break;
    
    
      case 'V': save=head;
          lp=0;
          if (head==NULL)
          {
           printf("\nThe directory is empty\n");
          }
          else
          {
           printf("\n\tName\t\t\tStreet Address\t\t\tPhone Number\n");
           while (save!=NULL)
           {
            lp++;
            printf("\n%d  %s, %c, %c\t\t%s\t\t\t%s", lp, p->last, p->first[0], p->middle[0], p->st_add, p->phone_num);
             save=save->next;
           }
          }
          getch();
          break;
    
    
      case 'D':
          printf("\n\tName\t\t\tStreet Address\t\t\tPhone Number\n");
           while (save!=NULL)
           {
            lp++;
            printf("\n%d  %s, %c, %c\t\t%s\t\t\t%s", lp, p->last, p->first[0], p->middle[0], p->st_add, p->phone_num);
             save=save->next;
           }
          if (head!=NULL)
          {
          printf("\nEnter the Last Name you want to delete from the directory:\n");
          scanf("%s", erase);
          save=head;
          if (save->last==erase)
          {
            save=head->next;
            free(head);
            head=save;
          }
          else
          {
           while (save->next!=NULL)
           {
            save2=save;
            save=save2->next;
            if (save->last==erase)
            {
             save2->next=save->next;
             free(save);
             nomatch++;
            }
           }
           if (nomatch==0)
    
    
           {
             printf("\nNo match found.\n");
           }
          }
          }
          else
          {
            printf("\nThere is nothing to delete.\n");
          }
          printf("\nProgram done!\n");
          getch();
          break;
    
    
      case 'E': printf("\nThanks for using the program. Bye!\n\n");
          break;
    
    
      default: printf("\nYour input is not in the options.\n");
     } //end of switch-cases
    } //end of while-loop
    } //end of the program
    Hi Everyone I have a problem with this program.
    I have a Wrong Ouput in Case 'V': and I cannot delete a node in Case 'D'.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please:
    • Indent your code properly.
    • Break up your long main function into smaller functions that do one thing and do it well.
    • Change void main to int main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    right off the bat
    Code:
    void main
    is wrong
    Code:
    int main(void)
    with a
    Code:
    return 0;
    the "right" way

    then
    Code:
    scanf("%d", &number);
    number is undeclared

    start there

    //edit//
    and in the code,

    street address doesnt allow spaces, like in 123 main st.

    after that

    your last name search isnt matching right
    Last edited by Crossfire; 01-06-2013 at 08:11 AM.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    
    typedef struct node
    {
     int num;
     char last[10];
     char first[10];
     char middle[10];
     char st_add[20];
     char phone_num[10];
     struct node *next;
    } node;
    
    
     node *head=NULL;
     char erase[10];
     int nomatch=0;
     int lp=0;
     char choice;
     char ilast[10];
     char ifirst[10];
     char imiddle[10];
     char ist_add[20];
     char iphone_num[10];
     node *p;
     node *save;
     node *save2;
    
    
    void insert()
    {
          printf("\nLast Name: ");
    	  scanf("%s", ilast);
    	  printf("\nFirst Name: ");
    	  scanf("%s", ifirst);
    	  printf("\nMiddle Name: ");
    	  scanf("%s", imiddle);
    	  printf("\nStreet Address: ");
    	  scanf("%s", ist_add);
    	  printf("\nPhone Number: ");
    	  scanf("%s", iphone_num);
    	  p=(node *)malloc(sizeof(node));
    	  strcpy(p->last, ilast);
    	  strcpy(p->first, ifirst);
    	  strcpy(p->middle, imiddle);
    	  strcpy(p->st_add, ist_add);
    	  strcpy(p->phone_num, iphone_num);
    	  p->next=NULL;
    	  if (head==NULL)
    	  {
              head=p;
    	  }
    	  else
    	  {
    	      save=head;
    	      while (save->next!=NULL)
    	      {
    	         save=save->next;
              }
    	      save->next=p;
    	  }
    	  printf("\n%s, %c, %c was successful inserted!\n", p->last, p->first[0], p->middle[0]);
    	  getch();  
    }
    
    
    void view()
    {
         save=head;
    	  lp=0;
          if (head==NULL)
    	  {
    	     printf("\nThe directory is empty\n");
    	  }
    	  else
    	  {
              printf("\n\tName\t\t\tStreet Address\t\t\tPhone Number\n");
    	      while (save!=NULL)
    	      {
    	         lp++;
    	         printf("\n%d  %s, %c, %c\t\t%s\t\t\t%s", lp, p->last, p->first[0], p->middle[0], p->st_add, p->phone_num);
    	         save=save->next;
              }
    	  }
    	  getch();
    }
    
    
    void delete()
    {
         printf("\n\tName\t\t\tStreet Address\t\t\tPhone Number\n");
         while (save!=NULL)
         {
    	    lp++;
    	    printf("\n%d  %s, %c, %c\t\t%s\t\t\t%s", lp, p->last, p->first[0], p->middle[0], p->st_add, p->phone_num);
            save=save->next;
         }
         if (head!=NULL)
    	  {
           printf("\nEnter the Last Name you want to delete from the directory:\n");
           scanf("%s", erase);
           save=head;
           if (save->last==erase)
            {
    	        save=head->next;
    	        free(head);
    	        head=save;
            }
    	  else
    	  {
    	      while (save->next!=NULL)
    	      {
                    save2=save;
    	            save=save2->next;
    	            if (save->last==erase)
    	            {
    	               save2->next=save->next;
    	               free(save);
    	               nomatch++;
                    }
              }
    	      if (nomatch==0)
    	      {
                 printf("\nNo match found.\n");
              }
    	  }
    	  else
    	  {
    	    printf("\nThere is nothing to delete.\n");
    	  }
    	  printf("\nProgram done!\n");
    	  getch();
    }
    
    
    void main()
    {
     while (choice!='E')
     {
     clrscr();
     printf("[I] Insert\n");
     printf("[V] View\n");
     printf("[D] Delete\n");
     printf("[E] Exit\n");
     printf("\nEnter your choice here: ");
     scanf("%c", &choice);
     switch (choice)
     {
      case 'I':
           insert();
           break;
    
    
      case 'V': 
          view();
    	  break;
    
    
      case 'D':
    	  delete();
    	  break;
    
    
      case 'E': printf("\nThanks for using the program. Bye!\n\n");
    	  getch();
          break;
    
    
      default: printf("\nYour input is not in the options.\n");
     } //end of switch-cases
    } //end of while-loop
    } //end of the program

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    Why there is an error in void delete.. "Declaration Terminated incorrectly"?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    But both crossfire and laserlight said that main should return int, not void!
    Example of a typical main
    Code:
    int main(void)
    {
          ...
          return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    And also I would suggest you not to cast what malloc returns.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Next step is to cut back the number of global variables. If a variable is only used in one function then move the declaration into that function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    your "new" code has done a few things...

    first off, the following locals are now global....

    Code:
     char erase[10];
     int nomatch=0;
     int lp=0;
     char choice;
     char ilast[10];
     char ifirst[10];
     char imiddle[10];
     char ist_add[20];
     char iphone_num[10];
     node *p;
     node *save;
     node *save2;
    causing you to not send nor return anything.
    which each of your functions should be like the following

    Code:
    void insert(void)
    void view(void)
    void delete(void)
    the following still has not been changed
    Code:
    void main()
    but overall, that is all you have done, was turned it into functions...
    nothing else has been addressed?

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This does NOT work as you think it does!
    Code:
    if (save->last==erase)
    Look-up how to use strcmp os strncmp
    strcmp - C++ Reference

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by programmerc View Post
    Why there is an error in void delete.. "Declaration Terminated incorrectly"?
    You probably compile in C++ mode. In C++ `delete` is a reserved keyword. Change the extension of your source code file to .c, and if it does not help, force C mode in the compiler's options. Or simply, use a different identifier name.

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    In C++ `delete` is a reserved keyword. Change the extension of your source code file to .c, and if it does not help, force C mode in the compiler's options. Or simply, use a different identifier name.
    Yeah i forgot about that!!!
    change delete to something else, even Delete works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with linked list in c
    By hugoguan in forum C Programming
    Replies: 1
    Last Post: 12-10-2010, 05:28 AM
  2. linked list problem
    By nynicue in forum C Programming
    Replies: 15
    Last Post: 07-27-2009, 02:57 PM
  3. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM