Thread: linked list HElP

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

    linked list HElP

    #include<string.h>
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    #define MAX 30

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


    /* any ideas why this code want run */

    int main()
    {
    int amount;

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

    struct node *ptr=(struct node*)malloc(sizeof (struct node));


    strcpy(ptr->name,"ANTHONY");
    cout<<ptr->name;

    ptr->next=(struct node*)malloc(sizeof(struct node));

    free(ptr->next);
    free(ptr);

    return 0;
    }

    /* thanks for the help */

  2. #2
    Unregistered
    Guest
    struct node *next;


    to start, remove the keyword struct from the above line. Not needed in C++.

  3. #3
    Unregistered
    Guest
    You ask how many people should be stored but you have no loop.
    ----So use a for loop for(int i=0; i<size; i++)

    In C++ struct keyword isn't needed.

    If you'd rather adopt a more C++ style (preferred in memory management) use new and delete. --- node *tmp=new node;

    you should at least have a node* head; which always points to the first node in the list.


    ...and this code does run, next time tell us exactly
    what you'd like us to look at

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