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

    Join Date
    Aug 2013
    Posts
    34
    Rep Power
    1

    Linked list help


    my code is
    #include<stdio.h>

    struct node
    {
    int info;
    struct node *next,*prev;
    };
    typedef struct node *nodeptr;
    nodeptr i;
    nodeptr q;
    nodeptr p;
    nodeptr *plist;

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




    int main()
    {
    int i;
    nodeptr *k;
    nodeptr j;
    nodeptr o;
    nodeptr head;
    nodeptr prev;
    nodeptr h;
    int a;
    int u;
    int v;
    int w;
    int *px;
    int r;
    nodeptr end;
    nodeptr s;
    nodeptr temp;
    nodeptr start;
    p=getnode();

    q=start;



    for(i=0; i<6; i++)
    {
    printf("enter value");
    scanf("%d",&r);
    p=getnode();
    p->info=r;

    q->next=p;
    q=q->next;

    }
    q=start;
    while(q->next!=NULL)

    {
    temp=q->next;
    q->next=q->prev;
    q->prev=temp;
    q=q->prev;
    }



    while((q->next)!=NULL)
    {
    printf("\n%d",(q->next)->info);
    q=q->next;
    }
    return 0;
    }

    I AM TRYING TO REVERSE THE LINKED LIST BUT IT IS NOT HAPPENING THE REVERSE CODE IS STARTED AFTER q=start please help
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Unreadable mess. You need to format your code by indenting it properly and post it in code tags in order to preserve that indentation.

    [code] insert your formatted code here [/code]

    I would have displayed your formatted code with code tags at this point, but since you didn't even bother to indent any of it, I have far better things to do with my time.

    Remember: if we can't read your code, then we can't help you.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by tendoeschate
    I AM TRYING TO REVERSE THE LINKED LIST BUT IT IS NOT HAPPENING THE REVERSE CODE IS STARTED AFTER q=start please help
    Since there are at least two lines of q=start, how are we to know which one you are talking about?

    Also, what do you mean by "BUT IT IS NOT HAPPENING"? That definition and description should probably also be accompanied by a definition and description for "REVERSE THE LINKED LIST". Are you trying to re-order the double-linked list (oh yeah, you forgot to mention that, didn't you?)? Or are you simply trying to traverse it from the end to the beginning?

    Comments in your code would really help us to understand what you are trying to do. Also, you have a shipload of node pointers, most of which lack any kind of meaningful name at all. What do they all do? Variables should be given meaningful names in order to make your code "self-documenting". If meaningful names are not used, then comments are absolutely required!

    You need to help us to help you.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Does your code even work at all? Here is what I get when I try to compile it:
    C:TEST>gcc -Wall nodeptr.c
    nodeptr.c: In function `getnode':
    nodeptr.c:17: warning: implicit declaration of function `malloc'
    nodeptr.c: In function `freenode':
    nodeptr.c:23: warning: implicit declaration of function `free'
    nodeptr.c: In function `main':
    nodeptr.c:43: warning: unused variable `s'
    nodeptr.c:42: warning: unused variable `end'
    nodeptr.c:40: warning: unused variable `px'
    nodeptr.c:39: warning: unused variable `w'
    nodeptr.c:38: warning: unused variable `v'
    nodeptr.c:37: warning: unused variable `u'
    nodeptr.c:36: warning: unused variable `a'
    nodeptr.c:35: warning: unused variable `h'
    nodeptr.c:34: warning: unused variable `prev'
    nodeptr.c:33: warning: unused variable `head'
    nodeptr.c:32: warning: unused variable `o'
    nodeptr.c:31: warning: unused variable `j'
    nodeptr.c:30: warning: unused variable `k'

    C:TEST>
    Yes, unused-variable warnings are benign, but having all those unused variables clutters up your code and since we have no idea what most of them are supposed to do, that makes your code far less readable.

    The warnings about malloc and free are far more important. Never ever implicitely declare anything! Always explicitly declare everything! For library functions, that means that you must always #include those functions' header files. While the compiler and linker might sometimes build it right despite your own efforts at self-sabotage, sometimes the executable doesn't get built right and it doesn't work when you try to run it. Just within this past week, somebody came here with bizaare errors and it was because he hadn't #include'd a function's header file.

    The header file for malloc and free is malloc.h, though they are also included in stdlib.h, which is a handy header file to always #include.

    Never ignore warnings. Always turn warnings on and up; that is what the -Wall option for gcc does. Warnings tell you that there's very likely something wrong with your code. Also, warnings can point you to very stupid mistakes and typos that you have made (eg, using assignment, = , instead of testing for equality, == , in an if statement). Warnings are much more important than error messages are. Always turn warnings on and up. And never ignore warnings.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Stupid question (read the highlighted comments):
    Code:
        q=start;
        while(q->next!=NULL)
        {
            temp=q->next;
            q->next=q->prev;
            q->prev=temp;
            q=q->prev;
        }
     
        // since you broke out of the preceding while loop 
        //   when q->next became NULL, could it possibly not equal NULL
        //   as you try to enter this second while loop?
        while((q->next)!=NULL)
        {
            printf("\n%d",(q->next)->info);
            q=q->next;
        }

    PS
    Code:
        nodeptr start;   // start contains garbage
        
        p=getnode();
    
        q=start;  // where is start pointing?
    What is start pointing to? You never initialize it, so it's pointing to some random garbage location. If you are really lucky, that should cause your program to crash for trying to access memory that doesn't belong to it. If you are really unlucky, then it won't crash, but instead just cause really weird problems as you clobber your own data, corrupting your data in unexpected ways.

    Never use a variable before you have initialized it to a known-good value, especially not pointers! Sometimes you'll be warned about trying to use uninitialized variables, but not always.

    PPS
    Code:
        q=start;
    
        for(i=0; i<6; i++)
        {
            printf("enter value");
            scanf("%d",&r);
            p=getnode();
            p->info=r;
    
            q->next=p;
            q=q->next;
        }
        q=start;
        while(q->next!=NULL)
    Where does q->next ever even get set to NULL? Instead, it's always set to a new node via p. Except for the next field of the last new node, which is never initialized and hence contains garbage.

    So once you enter that while loop, you will never get out of it. You'll be trapped in an infinite loop plowing through all kinds of garbage memory locations until you just happen to hit a grouping of zero bytes that can be interpreted as a NULL. Or you crash for an access violation, AKA "SEGFAULT".

    If part of your definition of "IT IS NOT HAPPENING" is that your program is crashing, then wouldn't you have thought that that would be something we should know about?

    Oh, and I forgot to tell you: DO NOT SHOUT AT US!
    Last edited by dwise1_aol; September 12th, 2013 at 04:06 PM.

IMN logo majestic logo threadwatch logo seochat tools logo