CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2013
    Posts
    6

    linked list problem

    #include<stdio.h>
    #include<iostream.h>
    struct node
    {
    int info;
    struct node *next;
    };
    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;
    int a;
    int r;
    nodeptr end;

    p=getnode();
    q=getnode();
    q=*k;

    for(i=0; i<3; i++)
    {

    printf("enter value");
    cin>>r;

    p->info=r;
    q->next=p;
    q=q->next;
    }


    for(q=*k; q->next!=NULL; q=q->next)
    cout<<"\n"<<(q->next)->info);

    return 0;
    }

    i am getting runtime error in some cases
    sometimes the last input is printed
    please help to correct the code and display all elements in the linked list

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: linked list problem

    1) When posting code, please use code tags. Go Advanced, select code and click '#'

    2) what debugging of the program has been done? How does the behaviour of the program deviate from that expected from the program design?

    3) What compiler are you using because this is not iso standard c++

    Code:
    #include<iostream.h>
    4) Why are using malloc/free in a c++ program? c++ programs would usually use new/delete

    5) why are you trying to add new modes as part of the main program? Functions like node add, remove etc would usually be seperate functions - or more usual in c++, class functions.

    6) In the whole of the program, you are only allocating 2 nodes at the start of main() - and then you are overwriting the value of the node q so you have a memory leak. You don't call freenode() anywhere. For a linked list to work properly, every time a new value is to be stored in the list, a new node needs to be allocated - which your program does not. Your node linkage in the for loop is just wrong. I would suggest that you produce a program design, walk through your design drawing and linking nodes on paper and only when you are satisified that the design is correct then code the program from the design.
    Last edited by 2kaud; August 31st, 2013 at 06:59 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2013
    Posts
    6

    Re: linked list problem

    The output is supposed to be the three value inputs
    but only the last input is getting displayed
    please help

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: linked list problem

    As 2kaud pointed out, you need to learn to use the debugger. You cannot code without it. However, the first problem I see is these two lines.

    nodeptr *k;
    ...
    q=*k;

  5. #5
    Join Date
    Jul 2013
    Posts
    6

    Re: linked list problem

    I DID A FEW CHANGES :
    #include<stdio.h>

    struct node
    {
    int info;
    struct node *next;
    };
    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;
    int a;
    int r;
    nodeptr end;

    p=getnode();
    q=getnode();
    q=*k;
    p=end;

    for(i=1; i<4; i++)
    {

    printf("enter value");
    scanf("%d",&r);

    p->info=r;

    q->next=p;

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

    for(q=*k; q->next!=NULL; q=q->next)
    printf("\n%d",(q->next)->info);

    return 0;
    }

    but it is only printing the first two members of the linked list

    please help

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: linked list problem

    But you haven't done as requested as 1) in my post #2. What don't you understand about using code tags???

    You also haven't addressed the issues raised in point 6)a dn by GCDEF in post #4. You haven't corrected the fundamental problems with this code. No 'tickering' with it is ever going to get it to work properly without a funadmental re-design.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: linked list problem

    Quote Originally Posted by tendoeschate View Post
    I DID A FEW CHANGES :
    So the question to you is why did you make these changes? What in the original code was wrong, and why were these changes made?

    Given that, how did you come up with the original code you posted? Did you design it yourself, or did you take bits and pieces of it from an existing implementation from a website, book, teacher, etc.? If you designed it yourself, then you need to take a step back and see where your code runs differently to your design. Any code that is designed by the programmer must be able to be debugged by that very same programmer.

    If you got the code you posted mostly from a book, website, etc. then you certainly cannot make a few changes and expect it to work unless you are an experienced programmer who knows exactly what they're doing.
    The output is supposed to be the three value inputs
    but only the last input is getting displayed
    please help
    but it is only printing the first two members of the linked list
    So why does it only print the first two members? Why are only three values displayed?

    If you don't know why, then debug the function(s) that prints and see where it goes wrong. Debugging is part of learning how to write programs -- it shouldn't fall on our shoulders to do this work that you should initially be doing. If after debugging, you notice that a line of code isn't executed, a variable set to the wrong value, etc., then give us this information.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 31st, 2013 at 10:42 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured