SORRY need my hand holding with linked lists

This is a discussion on SORRY need my hand holding with linked lists within the C++ Programming forums, part of the General Programming Boards category; Not sure if my program is safe to run,please comment on my comments and please get my program running where ...

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

    SORRY need my hand holding with linked lists

    Not sure if my program is safe to run,please comment on my comments and please get my program running where I can input the amount of nodes at run time.

    #include<string.h>
    #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;

    node *ptr=(node*)malloc(sizeof(node)); /* does'nt this need to be sizeof(node) * amount */
    /* because we need a block of memory for each node */
    strcpy(ptr->name,"ANTHONY");
    cout<<ptr->name;

    for(int start=0;start<amount;start++)
    {
    ptr->next=(node*)malloc(sizeof(node)); /* is this correct */
    ptr->next; /* and this */
    /* do something with nodes here */
    free(node ptr[start]); /* how do I free the node pointers individually/altogether */
    }
    ptr=NULL; /* is this needed */

    getch();

    return 0;
    }

    BIG thanks for this.

  2. #2
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Code:
    #include<cstring>
    #include<iostream>
    #include<cstdlib>
    
    #define MAX 30
    
    struct node
    {
      int age;
      char name[MAX];
      node *next;
    };
    
    int main ()
    {
      int amount;
      cout << "How many people to be stored"<<endl;
      cin >> amount;
      node *ptr = (node *) malloc (sizeof (node));	/* does'nt this need to be sizeof(node) * amount */
      // No
    
      /* because we need a block of memory for each node */
    
      // You cannot allocate space for the nodes behind that "node *next" like that.
      
      strcpy (ptr->name, "ANTHONY");
      std::cout << ptr->name << std::endl;
      for (int start = 0; start < amount; start++) {
        ptr->next = (node *) malloc (sizeof (node));	/* is this correct */
        // Yes, but new is usually used if you are using C++.
        ptr->next;		/* and this */
        // This does not do anything.
        // Do you mean ptr=ptr->next; ?
    
        /* do something with nodes here */
        
        free (ptr[start]);	/* how do I free the node pointers individually/altogether */
        // Like this, but it is not very wise to it here because you are using it again on the next
        // round in the loop
      }
      ptr = NULL;			/* is this needed */
    
      // Well, it's recommended to avoid problems later
    
      return 0;
    }

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

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


    int main()
    {
    //declare a single node, not associated with list yet
    //I'm using C++, not C so you'll have to do the changes there
    node *ptr= new node;
    strcpy(ptr->name,"ANTHONY");
    cout<<ptr->name;

    //now delete/free the memory just assigned to ptr
    delet ptr;
    ptr = NULL;

    //now work with a list

    node * head = NULL;
    //to keep track of the first node in the list. If you don't keep track of the first node you will never be able to find the others either, because the only way you know where to find nodes subsequent to head is by looking at the next pointer in the current node.

    //ptr will be used to keep track of where we are in the list

    node * newNode;
    //newNode will be used to get the new memory to be added to the list


    int amount;

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

    //add a given number of nodes to a list at the tail of the list each time

    for(int start=0;start<amount;start++)
    {
    //each loop will add one node
    newNode = new node;
    //assign data to newNode here;

    //assign newNode to head if head is still NULL
    if(head == NULL)
    {
    head = newNode;
    }
    else
    {
    //assign newNode to the end of the list
    //find the end of the list first
    //start at head each time
    ptr = head;
    //when ptr->next == NULL you have found the last node in the list
    while(ptr->next != NULL)
    {
    ptr = ptr->next;
    }
    //now assign newNode to ptr->next
    ptr->next = newNode;
    }


    //delete the list in any order you want;
    //typically find the last node in the list and delete it OR
    //delete the first node each time, like below
    ptr = head;
    while(ptr != NULL)//as
    {
    head = head->next;//keep track of the "new" first node!
    delete ptr;
    ptr = head;
    }

    getch();

    return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    When you know how to do your own list, stop reinventing the weel and use the std::list instead... easy, painless, optimized, portable...

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I disagree with that last statement, ninebit. Truth is, linked lists teach us a LOT about the theory of programming, and should be studied earnestly by all coders.


    Code:
    
    
    void Escape( char *msg );
    
    
    
    struct node{ 
    int age; 
    char name[MAX]; 
    node *next; 
    }; 
    
    
    
    
    int main() 
    {
    bool initialized = false; 
    int amount; 
    
    node *head;
    node *newNode;
    node *go;
    node *goner;
    
    
    head = (node*)malloc(sizeof(node)); 
    
    if(head == NULL)
     {
       Abort("This Program Has Performed An Illegal Operation And Will Be Shut Down!!");
     }
    
    strncpy(head->name, "I never use the head for data...", MAX);
    
    head->next = NULL; //...THIS IS REQUIRED!!!
    
    cout<<"How many people to be stored"; 
    cin >> amount; 
    
    
    
    for( int start=0; start < amount; start++) 
    { 
     newNode = (node*)malloc(sizeof(node)); 
     
     if(newNode == NULL) //...panic!
      break;
    
     if( !initialized )
      {
        head->next = newNode;
        initialized = true;
      } 
    
     newNode->next = NULL; //...Note: forget this == sudden death!
    
     sprintf(ptr->name," %i) ANTHONY", start);
     
     cout << ptr->name; 
    }
    
    go = head;
    
    while(go->next)
     {
      goner = go; 
    
      go = go->next;  //..advance to next structure...
    
      if( goner != NULL )
       {
         free(goner);
       }
    
     }
    
    if( go != NULL ) free(go); //...free the very last cell...
    
    getch(); 
    
    return 0; 
    } 
    
    
    
    
    
    
    
    void Escape( char *msg )
     {
       clrscr();
       puts("\n\n\n\n\n\n\n\n\n");
       printf("          %s", msg );
       puts("\n\n\n\n\n\n        ");
       getch();
       exit(-1);
     }




    The most important point about linked lists is:

    -ALWAYS initialize "next" to 0 or NULL.
    -ALWAYS check new pointers for NULLness
    -ALWAYS check pointers for NULLness before deleting
    -Always perfect a method and encapsulate in a function!!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 03:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21