Thread: Program problem

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    2

    Program problem


    in the following linked list program i input three nos for eg 123 and i want to display 312 but the output is 012 how to overcome this problem
    the program is given below:

    #include<stdio.h>

    struct node {
    int info;
    struct node *next;
    };
    typedef struct node *nodeptr;

    nodeptr q;
    nodeptr p;

    struct queue {
    nodeptr front, rear;

    };

    nodeptr getnode(void)
    {
    nodeptr p;
    p = (nodeptr) malloc(sizeof(struct node));
    return p;
    }

    void freenode(nodeptr p)
    {
    free(p);
    }





    int main()
    {
    int j;
    int y;
    int x;
    struct queue *b;
    nodeptr *plist;
    nodeptr *plist1;
    nodeptr s;
    nodeptr a;
    nodeptr d;
    q = *plist;


    for (j = 0; j < 3; j++) {
    printf("enter value");
    scanf("%d", &y);
    p = getnode();
    p->info = y;
    if (*plist == NULL)
    *plist = p;

    else {
    q->next = p;
    q = q->next;
    }
    }
    q = *plist;
    while ((q->next) != NULL) {

    q = q->next;
    printf("%d", q->info);
    }
    printf("enter element");
    scanf("%d", &x);
    s = NULL;
    q = *plist;
    while (q->next != NULL) {
    s = q;
    q = q->next;
    if (q->info == x)
    break;
    }

    a = q;
    for (q = *plist; q->next != NULL; q = q->next);
    q->next = *plist;
    d = a->next;
    a->next = NULL;
    *plist = d;
    q = *plist;
    while ((q->next) != NULL) {

    q = q->next;
    printf("%d", q->info);
    }
    return 0;
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481

    Wow.


    Read all of dwise1_aol's posts until you come to one admonishing those who ignore compiler warnings. It won't take long because so many of you are either new programmers or *nasty word removed by mom*.
    Code:
    c.c:41:11: warning: variable ‘s’ set but not used [-Wunused-but-set-variable]
    c.c:40:12: warning: unused variable ‘plist1’ [-Wunused-variable]
    c.c:38:17: warning: unused variable ‘b’ [-Wunused-variable]
    c.c:44:7: warning: ‘plist’ is used uninitialized in this function [-Wuninitialized]
    Do the unused variable warnings matter? Not here. Is it important that the first statement in main dereferences uninitialized memory?

    YES!


    Since you provided a broken program with probably an algorithm is implemented incorrectly, I can't tell what you intend. What if the input were

    99 errors here

    ? What do you expect?

    Suppose the input were a more reasonable

    9 4 21

    what would you expect for output?

    How is 123 three numbers?
    Last edited by b49P23TIvg; October 6th, 2013 at 07:47 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    And also, use code tags! By not using code tags, you've reduced your program to an unreadable mess. Which nobody would bother to read. That you used that K&R indenting style that hides all the open braces at the ends and even in the middle of lines and hence absolutely depends on proper indentation only makes matters worse -- you might consider using the vastly more readable Allman Style instead.

    Here is what your program looks like with its formatting restored (indenting recovered via the Reply button):
    Code:
    #include<stdio.h>
     
    struct node {
        int info;
        struct node *next;
    };
    typedef struct node *nodeptr;
     
    nodeptr q;
    nodeptr p;
     
    struct queue {
        nodeptr front, rear;
     
    };
     
    nodeptr getnode(void)
    {
        nodeptr p;
        p = (nodeptr) malloc(sizeof(struct node));
        return p;
    }
     
    void freenode(nodeptr p)
    {
        free(p);
    }
     
     
     
     
     
    int main()
    {
        int j;
        int y;
        int x;
        struct queue *b;
        nodeptr *plist;
        nodeptr *plist1;
        nodeptr s;
        nodeptr a;
        nodeptr d;
        q = *plist;
     
     
        for (j = 0; j < 3; j++) {
    	printf("enter value");
    	scanf("%d", &y);
    	p = getnode();
    	p->info = y;
    	if (*plist == NULL)
    	    *plist = p;
     
    	else {
    	    q->next = p;
    	    q = q->next;
    	}
        }
        q = *plist;
        while ((q->next) != NULL) {
     
    	q = q->next;
    	printf("%d", q->info);
        }
        printf("enter element");
        scanf("%d", &x);
        s = NULL;
        q = *plist;
        while (q->next != NULL) {
    	s = q;
    	q = q->next;
    	if (q->info == x)
    	    break;
        }
     
        a = q;
        for (q = *plist; q->next != NULL; q = q->next);
        q->next = *plist;
        d = a->next;
        a->next = NULL;
        *plist = d;
        q = *plist;
        while ((q->next) != NULL) {
     
    	q = q->next;
    	printf("%d", q->info);
        }
        return 0;
    }

    Comments on this post

    • b49P23TIvg agrees : It's a mess with code tags too.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Read all of dwise1_aol's posts until you come to one admonishing those who ignore compiler warnings. It won't take long because so many of you are either new programmers or *nasty word removed by mom*.
    Code:
    c.c:41:11: warning: variable ‘s’ set but not used [-Wunused-but-set-variable]
    c.c:40:12: warning: unused variable ‘plist1’ [-Wunused-variable]
    c.c:38:17: warning: unused variable ‘b’ [-Wunused-variable]
    c.c:44:7: warning: ‘plist’ is used uninitialized in this function [-Wuninitialized]
    Do the unused variable warnings matter? Not here. Is it important that the first statement in main dereferences uninitialized memory?

    YES!


    Since you provided a broken program with probably an algorithm is implemented incorrectly, I can't tell what you intend. What if the input were

    99 errors here

    ? What do you expect?

    Suppose the input were a more reasonable

    9 4 21

    what would you expect for output?

    How is 123 three numbers?

    #include<stdio.h>

    struct node
    {
    int info;
    struct node *next;
    };
    typedef struct node *nodeptr;

    nodeptr q;
    nodeptr p;

    struct queue
    {
    nodeptr front,rear;

    };

    nodeptr getnode(void)
    {
    nodeptr p;
    p=(nodeptr)malloc(sizeof(struct node));
    return p;
    }
    void freenode(nodeptr p)
    {
    free(p);
    }





    int main()
    {
    int j;
    int y;
    int x;
    struct queue *b;
    nodeptr *plist;
    nodeptr *plist1;
    nodeptr s;
    nodeptr start;
    nodeptr a;
    nodeptr d;
    q=*plist;


    for(j=0; j<3; j++)
    {
    printf("enter value");
    scanf("%d",&y);
    p=getnode();
    p->info=y;
    if(*plist==NULL)
    *plist=p;

    else
    {
    q->next=p;
    q=q->next;
    }
    }
    q=*plist;
    while((q->next)!=NULL)
    {

    q=q->next;
    printf("%d",q->info);
    }
    start=*plist;
    printf("enter element");
    scanf("%d",&x);
    s=NULL;
    q=*plist;
    while(q->next!=NULL)
    {
    s=q;
    q=q->next;
    if(q->info==x)
    break;
    }

    a=q;
    for(q=*plist; q->next!=NULL; q=q->next)
    ;
    q->next=start;
    d=a->next;
    a->next=NULL;
    *plist=d;
    q=*plist;
    while(q!=NULL)
    {

    printf("%d",q->info);
    q=q->next->next;
    }
    return 0;
    }

    NOW I AM GETTING OUTPUT AS 3 1 BUT NOT 2 PLEASE HELP
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    2
    Originally Posted by tendoeschate
    #include<stdio.h>

    struct node
    {
    int info;
    struct node *next;
    };
    typedef struct node *nodeptr;

    nodeptr q;
    nodeptr p;

    struct queue
    {
    nodeptr front,rear;

    };

    nodeptr getnode(void)
    {
    nodeptr p;
    p=(nodeptr)malloc(sizeof(struct node));
    return p;
    }
    void freenode(nodeptr p)
    {
    free(p);
    }





    int main()
    {
    int j;
    int y;
    int x;
    struct queue *b;
    nodeptr *plist;
    nodeptr *plist1;
    nodeptr s;
    nodeptr start;
    nodeptr a;
    nodeptr d;
    q=*plist;


    for(j=0; j<3; j++)
    {
    printf("enter value");
    scanf("%d",&y);
    p=getnode();
    p->info=y;
    if(*plist==NULL)
    *plist=p;

    else
    {
    q->next=p;
    q=q->next;
    }
    }
    q=*plist;
    while((q->next)!=NULL)
    {

    q=q->next;
    printf("%d",q->info);
    }
    start=*plist;
    printf("enter element");
    scanf("%d",&x);
    s=NULL;
    q=*plist;
    while(q->next!=NULL)
    {
    s=q;
    q=q->next;
    if(q->info==x)
    break;
    }

    a=q;
    for(q=*plist; q->next!=NULL; q=q->next)
    ;
    q->next=start;
    d=a->next;
    a->next=NULL;
    *plist=d;
    q=*plist;
    while(q!=NULL)
    {

    printf("%d",q->info);
    q=q->next->next;
    }
    return 0;
    }

    NOW I AM GETTING OUTPUT AS 3 1 BUT NOT 2 PLEASE HELP
    ENHANCEMENT OF THIS
    #include<stdio.h>

    struct node
    {
    int info;
    struct node *next;
    };
    typedef struct node *nodeptr;

    nodeptr q;
    nodeptr p;

    struct queue
    {
    nodeptr front,rear;

    };

    nodeptr getnode(void)
    {
    nodeptr p;
    p=(nodeptr)malloc(sizeof(struct node));
    return p;
    }
    void freenode(nodeptr p)
    {
    free(p);
    }





    int main()
    {
    int j;
    int y;
    int x;
    struct queue *b;
    nodeptr *plist;
    nodeptr *plist1;
    nodeptr s;
    nodeptr start;
    nodeptr a;
    nodeptr d;
    q=*plist;


    for(j=0; j<3; j++)
    {
    printf("enter value");
    scanf("%d",&y);
    p=getnode();
    p->info=y;
    if(*plist==NULL)
    *plist=p;

    else
    {
    q->next=p;
    q=q->next;
    }
    }
    q=*plist;
    while((q->next)!=NULL)
    {

    q=q->next;
    printf("%d",q->info);
    }
    start=*plist;
    printf("enter element");
    scanf("%d",&x);
    s=NULL;
    q=*plist;
    while(q->next!=NULL)
    {
    s=q;
    q=q->next;
    if(q->info==x)
    {
    a=q;
    break;
    }
    }


    for(q=*plist; q->next!=NULL; q=q->next)
    ;
    q->next=start;
    d=a->next;
    a->next=NULL;
    *plist=d;
    q=*plist;
    while(q!=NULL)
    {

    printf("%d%d",q->info,q->next->next->info);
    q=q->next->next->next;
    }
    return 0;
    }

    NOW THE OUTPUT IS 3 1 2 0
    THE 0 IS CREATING PROBLEM PLEASE HELP
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Indent your code and use code tags to preserve that indentation! What part of that are you incapable of understanding? If you post an unreadable mess, then we won't bother to try to read it! What part of that are you incapable of understanding?

    b49P23TIvg pointed you to a fatal flaw in your code, but you chose to ignore him!
    Originally Posted by b49P23TIvg
    Is it important that the first statement in main dereferences uninitialized memory?

    YES!
    Here is the offending code (note the use of indenting and of code tags!):
    Code:
    int main()
    {
        int j;
        int y;
        int x;
        struct queue *b;
        nodeptr *plist;
        nodeptr *plist1;
        nodeptr s;
        nodeptr start;
        nodeptr a;
        nodeptr d;
        
        q=*plist;
    
        for(j=0; j<3; j++)
        {
            printf("enter value");
            scanf("%d",&y);
            p=getnode();
            p->info=y;
            if(*plist==NULL)
                *plist=p;
            else
            {
                q->next=p;
                q=q->next;
            }
        }
    Direct your attention to the line in red. What is plist pointing to? IOW, what is the value of plist? You don't know, do you? Neither do I, because it is an uninitialized pointer! That means that it contains any possible random garbage. It could be pointing anywhere. Using an uninitialized pointer is like standing in the middle of a crowded room, closing your eyes and pointing a loaded pistol in any random direction, and squeezing the trigger. Who in their right mind would do such a thing? Who in their right mind would use an uninitialized pointer? Both acts are just as reckless, irresponsible, and reprehensible. As is ignoring somebody's warnings when you are about to perform such a deed, especially when you went through the motions of asking that person for advice.

    If plist is supposed to point to the start of the list, then, since there is no list at the start of the program, it needs to be initialized to indicate that there is no list. Then after that, you cannot change it or else you will have dropped that list. Consider this:
    Code:
        a=q;
        for(q=*plist; q->next!=NULL; q=q->next);
        q->next=start;
        d=a->next;
        a->next=NULL;
        *plist=d;
        q=*plist;
    You just dropped part of that list. Besides the fact that that is a major memory leak (a very insidious type of bug that is notoriously difficult to find, even though it is easy to correct), you have also lost a big chunk of data. Maybe you have a good reason to do that, but given the absence of comments, meaningful variables, or any kind of explanation of what your program is supposed to do, we have no way of knowing that. As b49P23TIvg commented about your code: "It's a mess with code tags too." Communication is an essential skill in programming. You need to write your code so that another programmer can pick it up and immediately be able to understand it. Meaningful variable names and informative comments are an essential part of that communication, as is making your code readable through proper formatting, which includes indenting your code and taking the proper steps to preserve that indentation (which here involves the use of code tags).

    And what is that nonsense about with making plist a pointer to a pointer? All you ever do with it is dereference it! All you're doing is making your code even more confusing than it already is.

    If we cannot understand what you are trying to do in that code, then we cannot help you.

    If you ignore what we tell you, then we cannot help you.

    PS
    This is what I get when I compile your code (MingW gcc on WinXP):
    C:TEST>gcc -Wall tendo2.c
    tendo2.c: In function `getnode':
    tendo2.c:22: warning: implicit declaration of function `malloc'
    tendo2.c: In function `freenode':
    tendo2.c:28: warning: implicit declaration of function `free'
    tendo2.c: In function `main':
    tendo2.c:39: warning: unused variable `plist1'
    tendo2.c:37: warning: unused variable `b'

    C:TEST>
    You knew to #include stdio.h in order to provide the prototypes for the standard I/O functions, scanf and printf. So why didn't you do the same for malloc and free? Or for NULL? malloc and free are prototyped in two header files: malloc.h and stdlib.h. In addition, NULL is declared in stdlib.h.

    The effects of not #include'ing a library function's header file is unpredictable. Sometimes you can get away with it. We have also seen it cause the program to crash, or at least for the function being called to completely fail to function properly.

    Why neglect to provide the compiler with the information that it needs to do its job? Why neglect to #include the header files that it needs?

    Why did you ignore the warnings you got because of that oversight? Only a fool ignores warnings.

    Are you compiling with warnings turned off? That is even more foolhardy!

    Until your program compiles cleaning (no errors or warnings which warnings turned on and up!), don't even bother to try to run it. Your program is broken. Any output or behavior your broken program would produce is absolutely meaningless.

    If your program is broken, then fix it! Only after you have fixed it can you try to run it and only then can you deal with unexpected output.
    Last edited by dwise1_aol; October 7th, 2013 at 01:37 PM.

IMN logo majestic logo threadwatch logo seochat tools logo