Something about a linkedlist program..

This is a discussion on Something about a linkedlist program.. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> typedef struct linkedlist { int data; struct linkedlist *next; }newnode; void insert(newnode **,int); void show(newnode ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Something about a linkedlist program..

    Code:
     
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct linkedlist {
            int data;
            struct linkedlist *next;
            }newnode;
    
    void insert(newnode **,int);
    
    void show(newnode *);
    
    
    
    int main(int argc, char *argv[])
    {
    newnode *list1s=NULL;
    newnode *list2s=NULL;
    int list1,list2;
    printf("Enter the integer for first list...");
                     scanf("%d",&list1);
                     if(list1!=0) insert(&list1s,list1);
                     printf("Enter the integer for second list...");
                     scanf("%d",&list2);
                      if(list2!=0) insert(&list2s,list2); 
    
      while(list1!=0 || list2!=0) {
                     if(list1==0) {
                                   printf("Enter the integer for second list...");
                     scanf("%d",&list2);
                      if(list2!=0) insert(&list2s,list2);  }
                                  if(list2==0) {
                      
                                  printf("Enter the integer for first list...");
                     scanf("%d",&list1);
                      if(list1!=0) insert(&list1s,list1); }
                    
                     }
    show(list1s); printf("\n");
    show(list2s); 
      system("PAUSE");	
      return 0;
    }
    
    void insert(newnode **sptr,int value) {
       newnode *previous,*newptr,*current;
       if((newptr=malloc(sizeof(newnode)))!=NULL) {
           newptr->data=value;
           newptr->next=NULL;
           previous=NULL;
           current=*sptr;
           while(current!=NULL && value> current->data) {
                               previous=current;
                               current=current->next; }
                               if(previous==NULL) {
                                                  newptr->next=*sptr;
                                                  *sptr=newptr; }
                                                  else {
                                                       newptr->next=current;
                                                       previous->next=newptr; }
                                                       }
                                                       else { printf("there is no available memory"); }
                                                       }
                                                       
    void show(newnode *sptr) {
         if(sptr!=NULL) {
            printf("%d---->",sptr->data);
           sptr=sptr->next;
    while(sptr!=NULL) {
                               printf("%d---->",sptr->data);                                                 
            sptr=sptr->next;
     }                                                                                                                                                                            }
    printf("NULL");
    }
    I think the code was intended properly. If not please warn me. The problem here is I am trying to enter numbers to two different linkedlist one by one. Out put.

    Enter the first list's number ......
    Enter the second...
    Enter the first list...
    Enter the second..... (Not exact output but the aim's the same)

    What I want to do , if I enter 0 for one , it will never want me to enter an integer for that list again , and want me to enter integers for the other one until I enter 0 for that too. The related part of the code is here:

    Code:
    #include <stdlib.h>
    
    typedef struct linkedlist {
            int data;
            struct linkedlist *next;
            }newnode;
    
    void insert(newnode **,int);
    
    void show(newnode *);
    
    
    
    int main(int argc, char *argv[])
    {
    newnode *list1s=NULL;
    newnode *list2s=NULL;
    int list1,list2;
    printf("Enter the integer for first list...");
                     scanf("%d",&list1);
                     if(list1!=0) insert(&list1s,list1);
                     printf("Enter the integer for second list...");
                     scanf("%d",&list2);
                      if(list2!=0) insert(&list2s,list2); 
    
      while(list1!=0 || list2!=0) {
                     if(list1==0) {
                                   printf("Enter the integer for second list...");
                     scanf("%d",&list2);
                      if(list2!=0) insert(&list2s,list2);  }
                                  if(list2==0) {
                                       printf("Enter the integer for first list...");
                                          scanf("%d",&list1);
                                     if(list1!=0) insert(&list1s,list1); }
                    
                     }
    What is the problem. It's been 3 days since I have stopped to think. I couldnt give enough time before too. So I am not really used to this piece of code . But yet , I want to solve this.Thank you for all responses.

  2. #2
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19
    when you do scanf it should be &list1->data or list1->&data not sure which but i know you can't just pass it the structure

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    No. list1 is not even a structure pointer . That is an integer.

  4. #4
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19
    whoops i see that now

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    ozumsafa, one of the tools programmers use (frequently!) is a debugger. If you're using DevC++, you can run the debugger by locating the "Debug" menu and using its options. Once you start the debugging process you can control program execution using things like "step" or "next," and watch troublesome variables.

    Had you done that you would have noticed that there is a problem with main():
    Code:
    printf("Enter the integer for first list...");
    scanf("%d",&list1);
    if(list1!=0) insert(&list1s,list1);
    printf("Enter the integer for second list...");
    scanf("%d",&list2);
    if(list2!=0) insert(&list2s,list2);
    while(list1!=0 || list2!=0) {
        if(list1==0) {
            printf("Enter the integer for second list...");
            scanf("%d",&list2);
            if(list2!=0) insert(&list2s,list2);
        }
        if(list2==0) {
            printf("Enter the integer for first list...");
            scanf("%d",&list1);
            if(list1!=0) insert(&list1s,list1);
        }
    }
    First off, it strikes me as odd that you go ahead and accept input before your loop structure begins. It's sort of like starting off before you start, it doesn't make sense. Determine if you need to run the body of a loop before testing the condition at all, then perhaps write a do ... while loop.

    Anyway
    > while( list1s!=0||list2s!=0 ) {
    Once list1s receives an object to point to, this loops forever, doing practically nothing thanks to your if statements.

    I recommend that you watch pointers carefully while debugging, because as you can see, pointers are valid as far as any loop control is concerned as long as they point to *something.* If you expect to loop properly in such cases, pointer(s) need to be updated properly every iteration.

    Another way to look at this problem would be:
    * read an integer from the user.
    * if the integer isn't zero, create a node.
    * insert the node into one of the lists using the insert function.

    After the input function is working like you expect, debug the insert function. After the insert function is working like you expect, you still have work to do. You could work on deleting the list too.



    If you insist on developing code without learning how to indent properly, fine, but at least use a pretty print form on the internet before you dump it somewhere. Here's one: www.prettyprint.de

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    On the contrary , the loop never starts. It stops before entering the loop. I still could not find my defect about if statement.I am trying.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you mean to say that it crashes before that loop, or that the loop is never entered? If either number is non-zero, it should enter the loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    > If either number is non-zero, it should enter the loop.
    And it does.

    The problem here is that because nothing apparently happens after he enters two values, he thinks his program stopped. It hasn't. It's just doing a lot of nothing.

    > while(list1!=0 || list2!=0) {
    Because this is true at that point;
    > if(list1==0) {
    > if(list2==0) {
    Yet both of these are false.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Heh , I couldnt believe my eyes when I see the nonsense I did. When I was writing I would do that
    if(list1!=0)



    if(list2!=0)

    What the hell did I do there =)) Sorry for the on the contrary contrast hehddpds. You were right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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