Is this creating a linked list?

This is a discussion on Is this creating a linked list? within the C Programming forums, part of the General Programming Boards category; I typedef'd some stuff: Code: typedef struct country Country; struct country { char name[LENGTH]; int dimen; int num; //QTnode *flag; ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Is this creating a linked list?

    I typedef'd some stuff:

    Code:
    typedef struct country Country;
    
    struct country {
      char name[LENGTH];
      int dimen;
      int num;
      //QTnode *flag;
      Country *next;
    };
    And here is the function that makes the node:

    Code:
     Country *makeNode(char p[MAXLENGTH]){
    
            int i = 1;
    
    
            Country *node;
            node = (Country*)malloc(sizeof(Country));
            strcpy(node->name, p);
            getImage(node->name, &node->dimen);
            node-> num = i;
            node-> next = NULL;
            printf("*%d [%d] %s\n", node->num, node->dimen, node->name);
            return (node);
    }
    Suppose the user presses "A", and this function gets activated and the following commands are executed properly. If the user presses "A" again, is this node destroyed and a new one created? If so, how should I go about retaining this node and creating a new node and having a pointer to the new node so that new information is stored there and can be accessed later?

    Cheers

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Since makeNode allocates the memory necessary to make an instance of Country, a new node is created in that function and returned to the caller. Nothing is destroyed nor will it ever be until the node is unlinked from the data structure and explicitly freed.

    You can access a node again later by linking it in the data structure - where the node is placed in the data structure depends entirely how you assign the return value.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    So currently if two nodes are made by my function, they aren't linked?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Nope.

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    How would I link them?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    I told you. Assign the return value. In a singly linked list this is especially simple.
    Code:
    Country *first    =   makeNode( "America" );
    first->next       =   makeNode( "France" );
    first->next->next =   makeNode( "Germany" );
    You now have a singly-linked list with a NULL tail. You're free to allocate an extra dummy node to serve as a tail as well, rather than relying on a simple NULL comparison.

    This idea is otherwise known as insertion. That is commonly implemented as a function that attaches a node parameter to another list, creating a larger list.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I think I see what you mean, thanks for the pointers, I'll see if I can implement them now.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  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. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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