Thread: linked list, geting there

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    41

    linked list, geting there

    please let me know if this is ok as it is my first linked list example
    I do not what my code changed as I will deal with that later.

    I just want to know is this fine up to this point and after new_nodes
    has finished I want to send the pointer new_nodes to another function
    to free all the memory(please show me with code), and why is it bad
    practice to store data in the top node.


    I know this is old style waiting for new compiler to be delivered.

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    
    #define MAX 30
    
    void new_nodes(node *top,int amount);
    
    	struct node{
                        int age;
                        char name[MAX];
                        node *next;
                   };
    
    int main()
    {
        int amount;
        node *top;
    
        cout<<"How many people to be stored >";
        cin>>amount;
    
        if(amount==0)
        	exit(1);
    
        top=new node // memory set aside for the top node
    
        void new_nodes(node *top,int amount); // pass top node and amount to function
    
        getch();
    
        return 0;
    }
    
    
    void new_nodes(node *top,int amount)
    {
    
        int loop;
    	node *new_node=top; // pointer of type node points to the top node
    
        for(loop=0;loop<amount;loop++)
        	{
                new_node->next=new node; // ptr new_node points to spare memory, the size of node(the loop node)
                new_node=new_node->next; // ptr new_node now points to the next element in the loop node
    
                cout<<"Enter persons name >";
                cin>>new_node->name;            // Enter name in second node
    
                cout<<'\n'<<"Enter persons age >";
                cin>>new_node->age;             // Enter age in second node
    
            }
    
        new_node->next=NULL;
    }

    Thanks all.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    1

    Talking

    the var "top" is a pointer to a node var, it has the type of "node* "but not "node", so its sure that you cannot store data in "top"

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    There are a few errors here.

    1. #define <iostream>
    using namespace std;
    Don't ask questions.

    2. top=new node; //semi-colon

    3. I don't think structs can use the new operator. But I may be wrong.

    4. Your call to the new_nodes() function should not be a complete declaration. It should be like this:

    new_nodes(top,amount);
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    I'm not trying to be a jerk here, but I don't understand why C++ programmers are trying to implement their own linked lists. And not only are you trying to implement your own linked list, but you are also trying to implement it C-style, without trying to encapsulate that nasty memory structure in a class or template. I guess it's okay to learn about how linked lists work by implementing one yourself, but it is kind of a waste of your time and effort to make your own for practical reasons because standard C++ already provides you with a good linked list container. If you are a good enough programmer to make one that was faster or more efficient or whatever than list<>, knock yourself out. But, at least for me, list<> and other containers are real time/effort savers, and I don't think they are as commonly used as they should be. I do think it is important to know what is going on under the hood, though.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Sometimes using a customized linked list is easier than trying to customize the STL containers. Like if you want to validate nodes before they are pushed onto the list.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    I don't understand why C++ programmers are trying to implement their own linked lists.
    I should not have said this because I do understand why people try to implement thier own linked lists and there are good reasons to do so, such as the ability to customize it or special circumstances where the list container just won't cut it (thank you xsquared for pointing this out).

    I just find it strange that people act like the STL doesn't exist because I think that the STL, along with classes, is one of the main 2 things that makes C++ so powerful and so different from just plain C. I don't like to see people do unnecissary(sp?) work because they have only been taught a watered-down C++(like what is taught by the tutorials on this site) and do not know the real C++. Use the tools C++ gives you, or use C, as C tends to be less complicated and maybe a teency weency bit more efficient (yeah, I said it).

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by PorkyChop
    I should not have said this because I do understand why people try to implement thier own linked lists and there are good reasons to do so, such as the ability to customize it or special circumstances where the list container just won't cut it (thank you xsquared for pointing this out).

    I just find it strange that people act like the STL doesn't exist because I think that the STL, along with classes, is one of the main 2 things that makes C++ so powerful and so different from just plain C. I don't like to see people do unnecissary(sp?) work because they have only been taught a watered-down C++(like what is taught by the tutorials on this site) and do not know the real C++. Use the tools C++ gives you, or use C, as C tends to be less complicated and maybe a teency weency bit more efficient (yeah, I said it).
    This person is obviously trying to implement their first linked list, i'm sure we've all been there. You've got to learn it sometime; how could you even consider using an std::list or std::slist in a program if you don't know what a linked list is in the first place.

    If you want a customized list, it's almost always easier to derive from an std::list or create a class which is composed of an std::list instead of building your own list from scratch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM