Thread: Link list :|

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Link list :|

    I am new here, sorry if i am posting in the wrong section or breaking any rules or something :|...
    My professor told us to make a program on linked lists to create a linked list, insert an element at the beginning and the end, and insert into a given position. I tried searching online for solutions but i really didn't get them :|

    This program has some logical error(s) cause the output is an infinite number series *_*...I would really appreciate it if you would tell me where they are and what exactly the error is :|...do try and explain as if i am a 5 year old :|

    Code:
    #include<stdio.h>
    #include<conio.h>
    struct node
    {
    int info;
    node *next;
    };
    node create();
    node push_beg(node *ll);
    node push_end(node *ll);
    node push_posi(node *ll);
    void traverse(node *ll);
    node create()
    {
    node *newnode=new node;
    if(newnode==NULL)
    {
    printf("Can't create the node");
    }
    else
    {
    printf("Enter the value: ");
    scanf("%d",&newnode->info);
    newnode->next=NULL;
    }
    return *newnode;
    }
    node push_beg(node *ll)
    {
    node *start;
    printf("Enter the values: ");
    scanf("%d",&start->info);
    if(ll==NULL)
    {
    ll=start;
    }
    else
    {
    start->next=ll;
    }
    return *ll;
    }
    node push_end(node *ll)
    {
    node *end;
    while(ll->next!=NULL)
    {
    ll=ll->next;
    }
    printf("Enter the element to be inserted: ");
    scanf("%d",&end->info);
    ll->next=end;
    end->next=NULL;
    return *ll;
    }
    node push_posi(node *ll)
    {
    node *start;
    int posi,temp=0;
    printf("Enter the position at which you want to enter the value: ");
    scanf("%d",&posi);
    while(temp<posi)
    {
    temp++;
    ll=ll->next;
    }
    printf("Enter the value: ");
    scanf("%d",&start->info);
    ll->next=start;
    start->next=ll;
    return *ll;
    }
    void traverse(node *ll)
    {
    while(ll->next!=NULL)
    {
    printf("%d",ll->next);
    }
    }
    //I wasn't sure how to make the main soo i did w.e popped into my //head :|, try not to laugh xD
    void main()
    {
    node *main, *second, *third, *fourth;
    *main=create();
    *second=push_beg(main);
    *third=push_end(second);
    *fourth=push_posi(third);
    traverse(fourth);
    getch();
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I won't read this code until you style it correctly.

    Take an idea Indent style - Wikipedia, the free encyclopedia

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First even though this code looks like C it is actually C++ because you are using things that don't exist in C, like new. Second I would be careful about naming variables with similar names as your functions. You have a main() function and then you name a variable main. While not necessarily incorrect this can become very confusing.

    And as already stated fix your indentation before you do anything else. That will make your program so much easier to read.


    Jim

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Quote Originally Posted by std10093 View Post
    I won't read this code until you style it correctly.

    Take an idea Indent style - Wikipedia, the free encyclopedia
    Umm sorry, i wasn't aware about the indentation. First time anyone ever told me :/...
    I will try and write it properly and post.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Mayank Rikh View Post
    Umm sorry, i wasn't aware about the indentation. First time anyone ever told me :/...
    I will try and write it properly and post.
    Do not be sorry I did not know about it until something explain it to me

    Also welcome to the forum

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Hopefully this is fine :|
    Code:
    #include<stdio.h>
    #include<conio.h>
    struct node
    {
      int info;
      node *next;
    };
    node create();
    node push_beg(node *ll);
    node push_end(node *ll);
    node push_posi(node *ll);
    void traverse(node *ll);
    node create()
    {
      node *newnode;
      if(newnode==NULL)
           printf("Can't create the node");
      else {
           printf("Enter the value: ");
           scanf("%d",&newnode->info);
           newnode->next=NULL;
         }
      return *newnode;
    }
    node push_beg(node *ll)
    {
      node *start;
      printf("Enter the values: ");
      scanf("%d",&start->info);
      if(ll==NULL)
           ll=start;
      else 
           start->next=ll;
      return *ll;
    }
    node push_end(node *ll)
    {
      node *end;
      while(ll->next!=NULL)
           ll=ll->next;
      printf("Enter the element to be inserted: ");
      scanf("%d",&end->info);
      ll->next=end;
      end->next=NULL;
      return *ll;
    }
    node push_posi(node *ll)
    {
      node *start;
      int posi,temp=0;
      printf("Enter the position at which you want to enter the value: ");
      scanf("%d",&posi);
      while(temp<posi) {
           temp++;
           ll=ll->next;
        }
      printf("Enter the value: ");
      scanf("%d",&start->info);
      ll->next=start;
      start->next=ll;
      return *ll;
    }
    void traverse(node *ll)
    {
      while(ll->next!=NULL) {
      printf("%d",ll->next);
      ll->next=ll;
       }
    }
    void main()
    {
      node *main, *second, *third, *fourth;
      *main=create();
      *second=push_beg(main);
      *third=push_end(second);
      *fourth=push_posi(third);
       traverse(fourth);
       getch();
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So does this new code compile without errors or warnings? If not then post the errors/warnings exactly as they appear in your development environment. If it does then ask specific questions based on the code provided.


    Note: The indentation is much better, but does need better consistency.

    Jim

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    So does this new code compile without errors or warnings? If not then post the errors/warnings exactly as they appear in your development environment. If it does then ask specific questions based on the code provided.


    Note: The indentation is much better, but does need better consistency.

    Jim
    There are no errors when i compile it but there comes an infinite number series, like the numbers i input are repeated infinite times. Kinda like an infinite loop :|,
    Soo probably a logical error somewhere that i can't find.
    My main concern is whether i have done the linked list program correctly?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There are no errors when i compile it
    Strange, when I compile your code as a C program I get the following errors.
    ||=== c_homework, Debug ===|
    main.c|6|error: unknown type name ‘node’|
    main.c|8|error: unknown type name ‘node’|
    main.c|9|error: unknown type name ‘node’|
    main.c|9|error: unknown type name ‘node’|
    main.c|10|error: unknown type name ‘node’|
    main.c|10|error: unknown type name ‘node’|
    main.c|11|error: unknown type name ‘node’|
    main.c|11|error: unknown type name ‘node’|
    main.c|12|error: unknown type name ‘node’|
    main.c|13|error: unknown type name ‘node’|
    main.c||In function ‘create’:|
    main.c|15|error: unknown type name ‘node’|
    main.c|20|error: request for member ‘info’ in something not a structure or union|
    main.c|21|error: request for member ‘next’ in something not a structure or union|
    main.c|25|error: unknown type name ‘node’|
    main.c|25|error: unknown type name ‘node’|
    main.c|36|error: unknown type name ‘node’|
    main.c|36|error: unknown type name ‘node’|
    main.c|47|error: unknown type name ‘node’|
    main.c|47|error: unknown type name ‘node’|
    main.c|63|error: unknown type name ‘node’|
    main.c|70|error: return type of ‘main’ is not ‘int’ [-Wmain]|
    main.c||In function ‘main’:|
    main.c|72|error: unknown type name ‘node’|
    main.c|72|warning: ‘main’ is usually a function [-Wmain]|
    main.c|72|warning: declaration of ‘main’ shadows a global declaration [-Wshadow]|
    main.c|70|warning: shadowed declaration is here [-Wshadow]|
    main.c|74|error: implicit declaration of function ‘push_beg’ [-Wimplicit-function-declaration]|
    main.c|75|error: implicit declaration of function ‘push_end’ [-Wimplicit-function-declaration]|
    main.c|76|error: implicit declaration of function ‘push_posi’ [-Wimplicit-function-declaration]|
    main.c|77|error: implicit declaration of function ‘traverse’ [-Wimplicit-function-declaration]|
    ||=== Build finished: 26 errors, 3 warnings ===|
    When I compile it as a C++ program I get these warnings:
    main.cpp||In function ‘void traverse(node*)’:|
    main.cpp|66|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘node*’ [-Wformat]|
    main.cpp||In function ‘node create()’:|
    main.cpp|16|warning: ‘newnode’ is used uninitialized in this function [-Wuninitialized]|
    main.cpp||In function ‘node push_beg(node*)’:|
    main.cpp|29|warning: ‘start’ is used uninitialized in this function [-Wuninitialized]|
    main.cpp||In function ‘node push_end(node*)’:|
    main.cpp|42|warning: ‘end’ is used uninitialized in this function [-Wuninitialized]|
    main.cpp||In function ‘node push_posi(node*)’:|
    main.cpp|58|warning: ‘start’ may be used uninitialized in this function [-Wuninitialized]|
    main.cpp||In function ‘int main()’:|
    main.cpp|73|warning: ‘main’ is used uninitialized in this function [-Wuninitialized]|
    main.cpp|74|warning: ‘second’ is used uninitialized in this function [-Wuninitialized]|
    main.cpp|75|warning: ‘third’ is used uninitialized in this function [-Wuninitialized]|
    main.cpp|76|warning: ‘fourth’ is used uninitialized in this function [-Wuninitialized]|
    ||=== Build finished: 0 errors, 9 warnings ===|
    So how are you compiling your program, with a C or C++ compiler?

    But in either case your program has major problems.

    Jim

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    Strange, when I compile your code as a C program I get the following errors.


    When I compile it as a C++ program I get these warnings:


    So how are you compiling your program, with a C or C++ compiler?

    But in either case your program has major problems.

    Jim
    Using turboc :O, it shows no errors or warnings to me for some reason just the numbers 12803 as the output over and over again
    Well i am using .cpp extension soo....

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Hey, I found the problem!:
    Quote Originally Posted by Mayank Rikh View Post
    Using turboc
    Toss that piece of anchient crap back into the scrap heap and use a real compiler - e.g. VS2008 or later.
    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"

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Quote Originally Posted by iMalc View Post
    Hey, I found the problem!:

    Toss that piece of anchient crap back into the scrap heap and use a real compiler - e.g. VS2008 or later.
    I would love to but sadly that is taught to us in college :|
    Exams and all take place on it only :|

    Note: Well i fixed the infinite output part but it still shows only 1424 as the output irrespective of what i give as the input :|
    Anyone any idea how to fix that? :|
    Last edited by Mayank Rikh; 11-14-2012 at 03:11 AM.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you fixed all the problems I pointed out in my last post?

    If you are trying to learn C then I suggest you change your file name to use a .c extension to compile this mess as a C program.

    After you have fixed all the issues already pointed out, post your new code.

    Jim

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    Have you fixed all the problems I pointed out in my last post?

    If you are trying to learn C then I suggest you change your file name to use a .c extension to compile this mess as a C program.

    After you have fixed all the issues already pointed out, post your new code.

    Jim
    I just decided to delete this program.
    I started another program from scratch, managed to get it to work near perfect
    Thanks all the same
    Moving onto doubly linked lists now xD

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Mayank Rikh View Post
    I just decided to delete this program.
    I started another program from scratch, managed to get it to work near perfect
    Wow, well done!
    Sometimes we want to say that to people on here, and sometimes we actually do, but usually people are reluctant to actually do that.
    This time though, you've gotten the jump on us and just done it, with good results. I'm impressed.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link list
    By johnhuge in forum C Programming
    Replies: 15
    Last Post: 09-21-2011, 01:51 PM
  2. Link List
    By siavoshkc in forum C++ Programming
    Replies: 59
    Last Post: 08-01-2006, 11:48 PM
  3. Left justification using linked list of link list
    By hykyit in forum C Programming
    Replies: 7
    Last Post: 08-29-2005, 10:04 PM
  4. link list
    By mayfda in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2003, 10:08 AM
  5. Link List
    By papedum in forum C Programming
    Replies: 3
    Last Post: 01-08-2002, 06:31 PM