Thread: slight problem just can't see the answer

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

    slight problem just can't see the answer

    /* ok I now understand single linked lists just have this slight */
    /* problem freeing them here */


    /* I know when I input 1 for the amount there are two nodes ie top and newnode */
    /* so when my program gets down to freeing the memory it outputs memory freed */
    /* memory freed (which whould be correct) but when I input more than 1 for the amount */
    /* and it gets down to freeing the nodes I still only see memory freed memory freed */
    /* when I should be seeing memory freed equal to amount + one more memory freed */
    /* for the top node */



    #include<iostream.h>
    #include<stdlib.h>
    #include<conio.h>

    #define MAX 30

    struct node{
    int age;
    char name[MAX];
    node *next;
    };

    int main()
    {
    int amount;
    bool initialized=false;

    node *top;
    node *newnode;
    node *begin;
    node *store;

    top=(node*)malloc(sizeof(node));

    if(top==NULL)
    abort();

    top->next=NULL;

    cout<<"How many people to be stored >";
    cin>>amount;


    for(int start=0;start<amount;start++)
    {
    newnode=(node*)malloc(sizeof(node));

    if(newnode==NULL)
    break;

    if(!initialized)
    {
    top->next=newnode;
    initialized=true;
    }

    newnode->next=NULL;

    cout<<'\n'<<"Input your name >";
    cin>>newnode->name;

    cout<<'\n'<<"Input your age >";
    cin>>newnode->age;
    }

    begin=top;

    while(begin->next)
    {
    store=begin;

    begin=begin->next;

    if(store!=NULL)
    cout<<'\n'<<"Memory freed",free(store);
    }
    if(begin!=NULL) cout<<'\n'<<"Memory freed",free(begin);

    getch();

    return 0;
    }

    /* thanks for all the help */

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your problems start in this section:
    Code:
    for(int start=0;start<amount;start++)
    {
    	newnode=(node*)malloc(sizeof(node));
    
    	if(newnode==NULL)
    	break;
    
    	if(!initialized)
    	{
    		top->next=newnode;
    		initialized=true;
    	}
    
    	newnode->next=NULL;
    
    	cout<<'\n'<<"Input your name >";
    	cin>>newnode->name;
    
    	cout<<'\n'<<"Input your age >";
    	cin>>newnode->age;
    }
    After you have entered one name/age, the newnode pointer is reused in the next iteration of the loop, without first storing the address in the list. It'll work once because your are doing this once:
    >top->next=newnode;

    Also, the way you do it, leaves an empty node at the top of the list.

    Have a look at this version:
    Code:
    node *tmp;
    node *top = NULL;
    node *newnode;
    int amount;
    
    cout << "How many people to be stored >";
    cin >> amount;
    
    for (int start = 0; start < amount; start++)
    {
    	newnode = (node *) malloc(sizeof(node));
    
    	if (newnode == NULL) break;
    
    	newnode->next = NULL;
    
    	cout << '\n' << "Input your name >";
    	cin >> newnode->name;
    
    	cout << '\n' << "Input your age >";
    	cin >> newnode->age;
    
    	if (top == NULL)
    	{	/* first item in list */
    		top = newnode;
    		tmp = newnode;
    	}
    	else
    	{	/* add to end of the list */
    		tmp->next = newnode;
    		tmp = newnode;
    	}
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. slight problem
    By duvernais28 in forum C Programming
    Replies: 4
    Last Post: 02-03-2005, 11:03 AM
  2. slight problem on win app tut
    By JustPlay4Fun in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2005, 11:42 AM
  3. Random Numbers...Problem With FAQ Answer
    By sitestem in forum C++ Programming
    Replies: 12
    Last Post: 04-14-2004, 09:22 AM
  4. another slight problem
    By sissoko in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2002, 10:01 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM