Thread: linked list example

  1. #1
    Registered User
    Join Date
    Jan 2002

    linked list example

    It's a while since I did linked lists is the following prog fine and if not why not.

    #define MAX 30
    	struct node{
        		  		int age;
                    	char name[MAX];
                    	node *next;  /* pointer to next node */
    int main()
        int amount;
    	node *top;      /* pointers to a structure of type node */
        node *newnode;
        node *start;
        node *hold;
        top=(node*)malloc(sizeof(node)); /* set memory aside for first node */
            abort(); /* abnormal program termination */
        cout<<"How many people to be stored >";
        		free(top); /* only memory set aside for top node freed */
                exit(0);   /* terminate program */
        cout<<"Input your name >";       /* we can only write to the top node(structure) at the moment */
        cout<<'\n'<<"Input your age";
        for(int start=1;start<amount;start++) /* start initialised to one as the first node has been filled */
            	newnode=(node*)malloc(sizeof(node));  /* set memory aside for the new node */
                top->next=newnode; /* pointer to top node points to next which is a pointer to a newnode */
                              /* which is assigned to next */
                cout<<'\n'<<"Input your name >";   /* write to next nodes */
                cout<<'\n'<<"Input your age";
                newnode->next=NULL; /* last node must point to NULL */
        cout<<top->name<<' '<<top->age;
        cout<<'\n'<<newnode->name<<' '<<newnode->age;
        start=top; /* node pointer start points to top/first node in the list */
        for(int begin=0;begin<amount;begin++)
                hold=start; /* assign each node to a holding node so we can free */
                            /* its memory later */
                start=start->next; /* start is now pointing to the next node */
                   	cout<<'\n'<<"Memory freed",free(hold);
        return 0;
    Thanks for all the help.

    Please use [code][/code]Tags

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Here is a good article on linked lists, with an example in C++.

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, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11: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, 11:21 PM
Website Security Test