Another Linked List plee

This is a discussion on Another Linked List plee within the C Programming forums, part of the General Programming Boards category; Hi, You may have read alot of my previous posts about linked lists, this is my last resort. I have ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Another Linked List plee

    Hi,

    You may have read alot of my previous posts about linked lists, this is my last resort. I have no idea what I am doing. Everyone is assuming that I know what i am doing, or I know what they are writing about. In reality I dont, I know so far how to setup a struct and fscanf into it. It took some time but i figured it out. When I read the linked list posts I see those "->" thingies, the Node and pointer stuff.

    Could anyone take the time and explain it out to me, or take a look at my code and tell me what I have to do. Remember go slow, I am a relative newbie.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    typedef struct army {
    
    	struct army *next;
    	char section_name[81];
    	char sub_name[81];
    	int points;
    	char discription[500];
    };
    
    int i=0;
    	char name[81]="<name>", scan[81], descrip[10]="<descrip>";
    	char temp_word[81];
    	char temp_close[8]="</name>";
    	char temp_close2[10]="</descrip>";
    
    
    struct army Army;
    
    
    int main()
    {
    	
    	
    FILE *inputfile;
    
    inputfile = fopen("Rune2.txt","r");
    
    if (inputfile == NULL)
    	{
    		printf("error opening file");
    	}
    	else
    	{
    		
    	printf("File opened successfully. Loading data...\n\n");
    	}
    	
    
    for (fscanf(inputfile," %s ", temp_word); temp_word!=NULL; fscanf(inputfile," %s ", temp_word))
    
    {
    		
    	if (strcmp(temp_word, temp_close)==0) break;
    	strcat(temp_word, " ");
    	strcat(Army.sub_name, temp_word);
    	
    }
    
    
    	fscanf(inputfile,"%d", &Army.points); 
    	fscanf(inputfile,"%s", scan);
    
    for (fscanf(inputfile," %s ", temp_word); temp_word!=NULL; fscanf(inputfile," %s ", temp_word))
    
    {
    	i++;	
    	if (strcmp(temp_word, temp_close2)==0) break;
    	strcat(temp_word, " ");
    	strcat(Army.discription, temp_word);
    if (i==9)
    	{
    		i=0;
    		strcat(Army.discription, "\n");
    	}
    
    }
    
    
    printf("%s\n%d\n%s\n", Army.sub_name, Army.points, Army.discription);
    }
    And this is the text file I am referencing through the program

    Runes2.txt

    Code:
    <name> Master Rune of Snorri Spangelhelm </name>
    75
    <descrip> Any blows struck by a weapon engraved with this rune will always hit. No roll to hit necessary. </descrip>
    
    <name> Master Rune of Skalf Blackhammer </name>
    75
    <descrip> Any weapon Bearing this rune will automatically wound if it hits. No roll to wound required. Use characters strength for hte save modifier. </descrip>
    
    <name> Master Rune of Alaric the Mad </name>
    50
    <descrip> This rune cancels an opponents armour saving throw. When wounded by this weapon, the target is not allowed an armour saving throw of any kind. </descrip>

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Alright, I'll give you a quick structure / pointer / list tutorial:
    Code:
    struct name
    {
        int data;
    };
    This is a structure. It a collection of arbitrary data types combine into a single variable type. Structures have a name, which is optional when you declare them. If you don't provide a name at the time of declaration, you can't create instances of them later. So usually we give them a name. The above structure's name is name.

    It only has one member, which is an int, called data.

    To create an instance of a structure, we can do the following:
    Code:
    struct name instance;
    We now have an instance of this data type. It is called, oddly enough, instance. Creating an instance of a structure is exactly the same as creating any other variable. This variable's type is "struct name", it's name is "instance".

    To access members of a structure directly, you use the . operator, commonly called the dot operator (or period). To access data in our instance, we do:
    Code:
    /* assigning to the data... */
    int something = 10;
    instance.data = something;
    /* assinging the data to something else...*/
    something = instance.data;
    You can also have pointers to structures. I'll assume you know what pointers are. In case you don't, pointers are variables that store memory addresses. Normal variables hold values. The value a pointer holds is a memory address. You dereference a pointer to see what it points at, that is to say, to get the value of whatever is being pointed at.

    You create a pointer to a structure just like you would create a pointer to another variable.
    Code:
    int *pointer;
    struct name *ptrToStruct;
    Here we have created a pointer to an integer, and a pointer to a "struct name" data type. To assign an address to a pointer, you can do one of two things:
    Code:
    struct name *ptrToStruct;
    struct name *ptrToStruct2;
    struct name instance;
    
    /* assign the address of instance to a pointer directly... */
    ptrToStruct = &instance;
    /* assign the value of one pointer to another... */
    ptrToStruct2 = ptrToStruct;
    In the first example, we use the address-of operator to get the address of instance. Once we have it, we assign it to a pointer of the same type. In the second example, we directly give the value of one pointer to another. This is just like assigning "normal" variables values.

    To access the data in the structure through a pointer, you use the -> operator. This is commonly called the arrow operator. You use it so you don't have to dereference with parentheses and what not (due to operator precedence). Basicly, it's an easy way to access items in a structure when you're using a pointer to point at that structure. And so...
    Code:
    /* assign the address of instance to a pointer directly... */
    ptrToStruct = &instance;
    
    /* access data using our pointer... */
    ptrToStruct->data = 10;
    Now, were you to go back to instance, and access the member data, you would find that it is now 10.

    All a linked list is, is a structure which contains a pointer to a structure of the same type. This pointer is used to point to another structure, which points to another, which... and so on. Just like links of a chain. Let's demonstrate:
    Code:
    struct node { /* we'll call this one node, but the name doesn't matter, so long as it has one. */
        struct node *next; /* this is a pointer of the same type as the structure we're creating */
        int somedata;
        int somemoredata;
        int someotherdata;
    };
    Here we now have a structure, just like any other structure, with one minor exception. It contains a pointer to the structure of the same type as itself. This is so we can make a big list of them, (or small) where one points to the next, points to the next...

    And now we will do so:
    Code:
    /* for illustration, we'll just make 2 pointers, and 2 "regular" instances */
    struct node instance1, instance2;
    struct node *ptr1, *ptr2;
    
    /* again, for illustration, we'll make each pointer point to a different node */
    ptr1 = &instance1;
    ptr2 = &instance2;
    
    /* now, we'll make the next pointer in one point to another node */
    ptr->next = ptr2;
    We now have just created a linked list. It's small, but a list none the less. Were you to access "instance1.next" directly, you'd find that it stores the address of "instance2". Were you to access "ptr1->next", you'd find the same thing.

    That'll get you started. Be sure you always initialize your pointers before you use them. I didn't in the case of "ptr2->next", it points randomly at some place in memory. Usually you'd do:
    Code:
    ptr2->next = NULL;
    Or something like that. Anyway that's enough to get you in trouble, so I'll let you get to it.

    Quzah.
    [edit]Edits to close tags... Preview you say? Hah! [/edit]
    Last edited by quzah; 05-15-2004 at 07:16 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    For the illistration, when you made 2 pointers and 2 "regular" instances. Do you have to make the same amount of pointers as you do the instances? And does that mean i have to define another link in the list before i can store data to it?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to use malloc and free? In short, yes. Every time you want another structure, you create an instance of it. In a linked list, you (usually*) use pointers and allocate your new structures whenever you need another one. Then when you're done with them, you free them. Here is a function that creates a new instance dynamicly:
    Code:
    struct node *makenewnode( void )
    {
        struct node *temp = NULL;
        temp = malloc( sizeof( struct node ) );
        return temp;
    }
    This is the simplest of implementations. Usually your function would initialize each member of the structure to whatever you want. This one simply makes you a new node. You'd use it like so:
    Code:
    struct node *anode;
    anode = makenewnode( );
    if( anode != NULL )
    {
        ...do whatever you want with it...
    }
    else
        ...malloc failed, you're out of memory...
    But no, you don't have to make "regular" instances each time. And for your final question, yes, every time you want to store data in a link of the list, you have to create another node instance.

    It's like so:
    Code:
    create one node
        put some data in it
    
    create another node
        link it to the first node
        put some data in the new node
    
    create a third node
        link it to the second node
        put some data in it
    Now take a look at some of the other linked list examples on the forum, and see if you have a better understanding of them.

    Quzah.
    Hope is the first step on the road to disappointment.

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. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  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

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