Thread: linked list help me

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

    linked list help me

    lets say amount has been passed a value any value now I want to
    make a linked list of size amount, How do I go about making the
    cin statements enter data into each node for each iteration and
    the following code is what i've got.

    Code:
    void new_node(int amount)
    {
    
        int loop;
    	node *top; // pointer of type node
    
        for(loop=0;loop<amount;loop++)
        	{
                top=new node; // ptr top points to spare memory, the size of node
    
                cout<<"Enter persons name >";
                cin>>top->name;
    
                cout<<'\n'<<"Enter persons age >";
                cin>>top->age;
    
                top->next=new node; // The pointer next in the first node now
                   // points to the next node with new setting the memory aside
            }
    }

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You have no way of returning the allocated list back to the calling program. I modified your function to take a pointer to the head of the list:
    Code:
    void new_node(node* top, int amount)
    {
    
    	int loop;
    	node* iterator = top;
    
     	for(loop=0;loop<amount;loop++)
        	{
                iterator->next = new node; // ptr top points to spare memory, the size of node
                iterator = iterator->next;
    
                cout<<"Enter persons name >";
                cin>> iterator->name;
    
                cout<<'\n'<<"Enter persons age >";
                cin>> iterator->age;
            }
    }
    That code is untested, but it should work.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Donīt really know if I understand your question properly. From what I understand you want to create a linked list with a size and enter some data in that list.

    First problem: You never specify a size for a linked list. This is the advantage of using a list, else we could have used a vector or an array.

    Second problem: I donīt really know how the node struct/class looks like and therefore itīs hard to say what is wrong. One thing that struct me was the line
    Code:
    cin>>top->name;
    this is actually not a good thing to do although it is possible. This breaks the rule of encapsulation this is a bad habit in OOP.

    If you could be more specific about your problem that would certainly help me to help you .
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    41
    ok ripper.

    for an amount of people then create a linked list
    for that amount of people ie if you input 15 I
    want a linked list of 15 structures(nodes) and the
    for loop to accept the input for each node.

    also how would I free the memory in a different for loop

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

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by anthonye
    ok ripper.

    for an amount of people then create a linked list
    for that amount of people ie if you input 15 I
    want a linked list of 15 structures(nodes) and the
    for loop to accept the input for each node.

    also how would I free the memory in a different for loop

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    
    #define MAX 30
    
    
    	struct node{
                        int age;
                        char name[MAX];
                        node *next;
                   };
    
    int main()
    {
        int amount;
    
        cout<<"How many people to be stored >";
        cin>>amount;
    
        if(amount==0)
        	exit(1);
    
        void new_node(int amount);
    
        getch();
    
        return 0;
    }
    
    
    void new_node(int amount)
    {
    
        int loop;
    	node *top; // pointer of type node
    
        for(loop=0;loop<amount;loop++)
        	{
                top=new node; // ptr top points to spare memory, the size of node
    
                cout<<"Enter persons name >";
                cin>>top->name;
    
                cout<<'\n'<<"Enter persons age >";
                cin>>top->age;
    
                top->next=new node; // The pointer next in the first node now
                   // points to the next node with new setting the memory aside
            }
    }
    To free the memory, you need to return the allocated memory back to the calling program (like I showed in an earlier post)

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Why make the function have a loop? Make the function just add a node to the end of the list and create a loop in main that calls the function as many times as you need
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

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

    linked list is this fine

    Is this prog fine and if so how would I go about
    freeing memory set up for the nodes in a seperate function

    also why is it a bad idea to store data in the top node.

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    
    #define MAX 30
    
    void new_node(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_node(node *top,int amount); // pass top node and amount to function
    
        getch();
    
        return 0;
    }
    
    
    void new_node(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;
    }

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Like i said in your other thread put your loop inside main, and make the function search to the end of the list and then add a person. When the function ends your loop in main will call it again and it will repeat until your amount of people have filled the list.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    ___
    Join Date
    Jun 2003
    Posts
    806
    Code:
     Errors I got...
    
    
    p1.cpp
    c:\compiled\nodes\cpp1.cpp(7) : error C2065: 'node' : undeclared identifier
    c:\compiled\nodes\cpp1.cpp(7) : error C2065: 'top' : undeclared identifier
    c:\compiled\nodes\cpp1.cpp(7) : error C2062: type 'int' unexpected
    c:\compiled\nodes\cpp1.cpp(18) : warning C4552: '*' : operator has no effect; expected operator with side-effect
    c:\compiled\nodes\cpp1.cpp(28) : error C2061: syntax error : identifier 'node'
    c:\compiled\nodes\cpp1.cpp(33) : error C2143: syntax error : missing ';' before '}'
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  10. #10
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Try fix this
    Code:
    top=new node; // memory set aside for the top node
    Missing semicolon

    Code:
    void new_node(node *top,int amount); // pass top node and amount to function
    This is a declaration of a function. You want to invoke a function like this
    Code:
    new_node(top,amount);
    But I am still unsure if this is the datastructure that you actually need(understand)? As I said in the previous thread a linked list should be sizeindependent. Usually you just add nodes to the front/back of the list. Iīm pretty sure that your problem can easilly be solved with a array/vector.

    P.S. Try make a search on this board about linked list.
    Last edited by ripper079; 07-15-2003 at 06:13 AM.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    41
    PLEASE PLEASE PLEASE try to keep my prog like it is
    as this is my first linked list and trying to get used to them.

    just solve my questions for now.

    thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  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