linked lists inserting problem

This is a discussion on linked lists inserting problem within the C Programming forums, part of the General Programming Boards category; Hi, I am making a long integer arthimetic calculator that performs +and*. I am adding an element and then printing ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    40

    linked lists inserting problem

    Hi,
    I am making a long integer arthimetic calculator that performs +and*. I am adding an element and then printing the node.The problem I am having is that I can the value I inserted but I also get random garbage in front that I never inserted.

    For example if I insert 10 I get 0 -38 10
    My code is:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    #include <string.h>
    #include <ctype.h>
    #define FALSE 0
    
    typedef struct node
    {
        int integerone;
        struct node * link;
    } list_head;
    /* Printing the list Contents */
    
    void printList(list_head*head){
      list_head*temp=head;
      while(temp){
        printf("%d  " ,  temp->integerone);
        temp=temp->link;
      }
    
    }
    
    void Menu(int *choice) {
    
        char    local;
        printf("\n1.Insert first number\n");
         printf("2. Insert second number\n");
         printf("3. Add numbers\n");
         printf("4. Multiply numbers\n");
         printf("5. Process file\n");
         printf("6. Quit\n");
        printf("Enter Option: ");
        do {
    	local = getchar ();
        } while (isdigit ((unsigned char) local) == FALSE);
        *choice = (int) local - '0';
    }
    
    
    
    int insert_to_list(list_head**head,int digit){
        list_head *tmp;
        tmp=NULL;
         tmp = malloc(sizeof(list_head));
    
       tmp->integerone=digit;
       tmp->link=NULL;
    
       if(*head==NULL){
          *head=tmp;
       }
     	 else{
    	    list_head*temp=*head;
                       while(temp->link!=NULL){
                       temp=temp->link;}
                       temp->link=tmp;
                      return *head;
           }
      }
    
    
    
    //allocate memory for a new list header
    list_head *create_list(void){
    list_head*p=NULL;
    p=malloc(sizeof(list_head));
    
    		p->link=NULL;
    
    return p;
    }
    int main(void){
    list_head *head=NULL;
    int choice;
    char c;
    
    head=create_list();
    
      do {
    	Menu (&choice);
    	switch (choice) {
    	    case 1:
    		 printf("1. Insert first number\n");
    	                scanf("%c",&c);
                                    do{
    
    		     head=insert_to_list(&head,c-'0');
                                    }while((c = getchar()) != '\n');
                         break;
    	    case 2:
    		printf("2.print list\n");
                                        printList(head);
    
    		break;
    	    case 3:
    		printf("3. Add numbers\n");
    
    		break;
    
    	    case 4:
    		break;
     		printf("4. Multiply numbers\n");
    
     	    case 5:
    		printf("5. Process file\n");
    		break;
    
    	    case 6:
    		break;
    	    default:
    		printf ("Invalid menu choice - try again\n");
    		break;
    	}
        } while (choice != 6);
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    does anybody have an idea what is wrong.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Well apart from the terrible formatting of your code on the board, read the comments
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define FALSE 0
    
    typedef struct node
    {
      int integerone;
      struct node * link;
    } list_head;
    /* Printing the list Contents */
    
    void printList(list_head*head){
      list_head*temp=head;
      while(temp){
        printf("%d  " ,  temp->integerone);
        temp=temp->link;
      }
    
    }
    
    /*!! this list of choices is inconsistent with the cases */
    /*!! in main */
    void Menu(int *choice) {
    
      char    local;
      printf("\n1.Insert first number\n");
      printf("2. Insert second number\n");
      printf("3. Add numbers\n");
      printf("4. Multiply numbers\n");
      printf("5. Process file\n");
      printf("6. Quit\n");
      printf("Enter Option: ");
      do {
        local = getchar ();
      } while (isdigit ((unsigned char) local) == FALSE);
      *choice = (int) local - '0';
    }
    
    void/*!!int*/ insert_to_list(list_head**head,int digit){
      list_head *tmp;
      /*!!tmp=NULL;*/
      tmp = malloc(sizeof(list_head));
    
      tmp->integerone=digit;
      tmp->link=NULL;
    
      if(*head==NULL){
        *head=tmp;
      }
      else{
        list_head*temp=*head;
        while(temp->link!=NULL){
          temp=temp->link;
        }
        temp->link=tmp;
        /*!!return *head;*/
      }
    }
    
    /*allocate memory for a new list header*/
    /*!! this function is not needed */
    list_head *create_list(void){
      list_head*p=NULL;
      p=malloc(sizeof(list_head));
    
      p->link=NULL;
    
      return p;
    }
    
    int main(void){
      list_head *head=NULL;
      int choice;
      char c;
    
      /*!!head=create_list();*/
    
      do {
        Menu (&choice);
        switch (choice) {
          case 1:
            printf("1. Insert first number\n");
            scanf("%c",&c);
            do{
              /*!!head=*/insert_to_list(&head,c-'0');
            } while((c = getchar()) != '\n');
            break;
          case 2:
            printf("2.print list\n");
            printList(head);
            break;
          case 3:
            printf("3. Add numbers\n");
            break;
          case 4:
            break;/*!!unreachable code*/
            printf("4. Multiply numbers\n");
          case 5:
            printf("5. Process file\n");
            break;
          case 6:
            break;
          default:
            printf ("Invalid menu choice - try again\n");
            break;
        }
      } while (choice != 6);
      return 0;/*!!added*/
    }
    The biggest problem seemed to be the way your insert_list function failed to return anything some of the time, and returned things of the wrong type at other times, resulting in a mashed list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    thanks for the help but there is one more problem it still prints
    -38 . I don't undertand where is -38 coming from.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Outside of tons of gross coding errors, the following lines irked me the most

    Code:
    list_head *create_list(void){
      list_head*p=NULL;  /*this sort of irks me, but whatever*/
      p=malloc(sizeof(list_head));
    
      p->link=NULL;
     /*You need to allocate memory for p->link*/
    
     /*now p has to point to the next node in the list*/
      return p;
    }
    First, you don't check malloc() for NULL. However, that is the least of your immediate worries in this case. You allocate memory for p, but not for p->link. On top of that, you need to then have p point to p->link (ie p = p->link).

    Second, the prototype for getchar() is
    Code:
    int getchar(void);
    In other words, don't go
    Code:
    char c;
    
    c = getchar();
    It should be
    int c;

    Third, scanf() blows. If you must use it, at least check for junk values.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > I don't undertand where is -38 coming from.
    It's the newline you push onto the list
    '\n' - '0' is -38

    Try
    Code:
    while((c = getchar()) != '\n') {
      insert_to_list(&head,c-'0');
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  3. Multiple Linked Lists w/HeadNode
    By tofugirl in forum C Programming
    Replies: 12
    Last Post: 12-10-2005, 10:21 AM
  4. One more question about linked lists?
    By shaeng in forum C Programming
    Replies: 2
    Last Post: 06-24-2003, 08:36 AM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21