Thread: Linked List Problems

  1. #1
    Unregistered
    Guest

    Linked List Problems

    i have a problem with linked lists..here goes.

    So far i have a struct like this;

    struct details
    {
    char* firstname;
    char* lastname;
    int age;
    };

    struct node
    {

    struct details data;
    struct node* link;

    };

    now how am i supposed to make a linked list from this. i know how to get user input but not how to populate a linked list.
    could anyone please help?
    thanx

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    You make a node point to the next one. But first u havta allocate memory for that new node.

    mmmmm
    Code:
    struct node *newPtr;
    
    newPtr = malloc( sizeof( struct node ) );
    
    *currentPtr->nextPtr = newPtr;
    only the theory

  3. #3
    Unregistered
    Guest
    thanx

    so now if i create an instance of the struct e.g

    struct details d;

    and have a user enter details for it e.g

    d.firstname;
    ........
    etc...

    do i simply have to write sumthing like

    ptrNew->next = d;

    or smuthing different?

    if u havent guessed i am new at this :-D

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1
    Thats exactly what i need to know as well, im (really) new to C progarmming and need all the info and help i can

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Here's a simple linked list that counts from 0 to 9 with the root node being -1. It doesn't check for any errors, so keep that in mind:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
      int data;
      struct node *next;
    };
    
    int main ( void )
    {
      int x;
      /* Declare all of the node pointers that we will
      ** need through the course of the program and
      ** allocate memory for the root node.
      */
      struct node *iter, *whisker, 
                  *root = malloc ( sizeof *root );
      iter = root;
      iter->data = -1, iter->next = NULL;
      /* Populate the list by allocating memory for
      ** the current node's next pointer. This way
      ** we can then move iter to the next node easily.
      */
      for ( x = 0; x < 10; x++ ) {
        iter->next = malloc ( sizeof *iter );
        iter = iter->next;
        iter->data = x, iter->next = NULL;
      }
      /* Print the list with neato link symbols ^_^
      */
      for ( iter = root; iter != NULL; iter = iter->next )
        printf ( "%d %s", iter->data, ( iter->next != NULL ) ? "-> " : "\n" );
      /* Free the allocated memory so that the system
      ** can now use it. The list has been created, 
      ** used, and destroyed after this loop.
      */
      for ( iter = root; iter != NULL; iter = whisker ) {
        whisker = iter->next;
        free ( iter );
      }
      /* When we're through tidying up after outselves,
      ** we can end the program.
      */
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help!
    By mbk in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 03:54 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM