list of pairs on integers

This is a discussion on list of pairs on integers within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> typedef struct node { int a; int b; struct node *next; } *list; void print(list ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    32

    list of pairs on integers

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node {
      int a;
      int b;
      struct node *next;
    } *list;
    
    void print(list head);
    
    int main()
    {
      list ptr, new ;
      int n;
      
      ptr=NULL;
      new = malloc(sizeof(struct node));
      new->a = 1;
      new->b = 2;
      new->next = NULL;
      printf ("Enter both integers separated by a space.\nIf you want to stop, write everything but integers.\n");
    
    do {
    n = scanf("%d %d", &new->a, &new->b);
    if (n != 2) {
    printf("You must write integers only\n");
    return 1;
    }
    }
    while (n==2);
    
    printf("Integers you entered: %d %d", new->a, new->b);
    return 0;
    
    
       print(ptr);
    }
    
    
    
    void print(list head) { 
        list curr; 
        int i=0; 
        curr=head;
     
        printf("Printing the list:\n"); 
        while(curr) { 
            printf("%d\n", curr->a);
    	 printf("%d\n", curr->b);
            curr=curr->next; 
            i++; 
        } 
        printf("Printed %d elements\n", i); 
    }
    Could you tell my why void function is not working here?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well you never said what you mean by "not working". If you mean "not being executed", then
    Code:
    printf("Integers you entered: %d %d", new->a, new->b);
    return 0;
    
    
       print(ptr);
    You return before you have a chance to call it. If you turn up your compilers warning level, then you would have got a message saying this. Whether your function actually "works" or not is a different story (I havent looked at it either).

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    ok, it executes.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node {
      int a;
      int b;
      struct node *next;
    } *list;
    
    void print(list head);
    
    int main()
    {
      list ptr, new ;
      int n;
      
      ptr=NULL;
      new = malloc(sizeof(struct node));
      new->a = 1;
      new->b = 2;
      new->next = NULL;
      printf ("Enter both integers separated by a space.\nIf you want to stop, write everything but integers.\n");
    
      n = scanf("%d %d", &new->a, &new->b);
      if (n != 2) {
        printf("You must write integers only\n");
        return 1;
      }
    
      /*printf("Integers you entered: %d %d\n", new->a, new->b);*/
    
      print(new);
    
      return 0;
    }
    
    
    
    void print(list head) { 
        list curr; 
        int i=0; 
        curr=head;
     
        printf("Printing the list:\n"); 
        while(curr) { 
            printf("%d\n", curr->a);
    	 printf("%d\n", curr->b);
            curr=curr->next; 
            i++; 
        } 
        printf("Printed %d elements\n", i); 
    }
    However it asks me about only one pair - how to make it asking for more?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    In your first code that you posted you have a do-while loop (in main), but in your new code there is no do-while loop (in main), so certainly will only ask one time. You probably removed that loop when you were debugging and forgot to put it back in. Put your first one back in.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node {
      int a;
      int b;
      struct node *next;
    } *list;
    
    void print(list head);
    
    int main()
    {
      list ptr, new ;
      int n;
      
      ptr=NULL;
      new = malloc(sizeof(struct node));
      new->a = 1;
      new->b = 2;
      new->next = NULL;
      printf ("Enter both integers separated by a space.\nIf you want to stop, write everything but integers.\n");
    
      do {
      n = scanf("%d %d", &new->a, &new->b);
      if (n != 2) {
        printf("You must write integers only\n");
        return 1;
        		}
         }
    while (n==2);
    
    
      /*printf("Integers you entered: %d %d\n", new->a, new->b);*/
    
      print(new);
    
      return 0;
    }
    
    
    
    void print(list head) { 
        list curr; 
        int i=0; 
        curr=head;
     
        printf("Printing the list:\n"); 
        while(curr) { 
            printf("%d\n", curr->a);
    	 printf("%d\n", curr->b);
            curr=curr->next; 
            i++; 
        } 
        printf("Printed %d elements\n", i); 
    }
    ok. however, it doesn't print the content - just writes that I should enter integers only... How to fix it?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
      do {
      n = scanf("%d %d", &new->a, &new->b);
      if (n != 2) {
        printf("You must write integers only\n");
        return 1;
        		}
         }
    while (n==2);
    The loop continues as long as n is 2, so it stops when is not 2. However, after the scanf you check if n != 2 and if it is you exit ("return" from main). So either the loop continues indefinitely or you read n != 2 tokens and exit the program. So control never reaches past the end of the while loop.

    You have this problem because your loop "terminator"/condition is ambiguous. You need to define some other method to tell the program "Im done entering pairs of ints". For example, maybe when only one number is entered and its a '-1' or something. You then know that the user purposely put bad data with the intention of continuing the program. Any other input with n != 2 means bad data. (Use this or any other method you see fit).

    Next, your list always only contains one element, so "print" will always only print the last pair of numbers entered. This is because in your while loop your overwriting the pairs of integers a, b each time. You probably want to create a new element and add to the list, rather than overwriting the previous element.

    Also, if weird things happen with the input/output, see http://faq.cprogramming.com/cgi-bin/...&id=1043284385. But I think your exact input is ok now.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Quote Originally Posted by nadroj View Post
    your list always only contains one element, so "print" will always only print the last pair of numbers entered. This is because in your while loop your overwriting the pairs of integers a, b each time. You probably want to create a new element and add to the list, rather than overwriting the previous element.
    This is exaclty what I mean - but how to do it?

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well if its exactly what you mean then you should fix the previous problems. I doubt its exactly what you mean, because you wouldnt know what it outputs because it never does. So the only way of finding out is by you manually tracing the program, which I highly doubt you did.

    Im not going to write the program for you. Look up basic examples of a linked list. You have most of it, you just need a few more lines in your do/while loop (minimum of 2 lines I believe). You need to create a new node and "attach" it to the existing list.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    If you were the one who wrote the print function then you would know how to write the bit you're asking about now. Given that you aren't understanding the basic advice being offered, and fail to recognise why a loop is even necessary to input multiple values, I think you need to go back to other sources such as tutorials and actually learn about what you're doing.

    There is no shortcut to learning how to program. "Do or do not; there is no try!"
    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"

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    ok, could you point me to some good tutorials?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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