Hi all -
I'm trying to write a linked-list that automatically increments its integer member as each node is added, but I haven't quite got things right as yet.
This is the code I've done so far -
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
struct node {
int index;
struct node *next;
} ;
/* Create a new list */
struct node *list_new(int i)
{
struct node *mylist = NULL;
mylist = malloc(sizeof(struct node));
mylist->index = i;
mylist->next = NULL;
return mylist;
}
/* Function to add data to end of a list */
void list_append(struct node *mylist)
{
int i;
/* A pointer to iterate along the list */
struct node *ptr = NULL;
/* Now, create a new node using the data. */
struct node *newnode = NULL;
newnode = malloc(sizeof(struct node));
/* Is our existing list null? */
/* If so, our "list" is just the newly-created node. */
if(mylist == NULL)
{
mylist = newnode;
}
else
{
for( ptr = mylist; ptr->next != NULL; ptr = ptr->next )
{
/* Iterate through the list until we find the last node */
// i = ptr->index;
i = ptr->index;
}
/* Add the new node to the end of the list. */
/* In other words: the last ptr->next WAS null, but it is */
/* now pointing to newnode. */
ptr->next = newnode;
newnode->index = i+1;
newnode->next = NULL;
}
} /* End of list_append */
/* Free the list memory. */
void free_list(struct node *mylist)
{
/* We create TWO pointers. */
/* ptr is used to iterate through the list. */
/* tmp (which points to the same place) is used to */
/* actually free the memory. */
struct node *ptr, *tmp = NULL;
/* Check that the list is non-null. */
if(mylist != NULL)
{
for( ptr = mylist; ptr->next != NULL; ptr = ptr->next )
{
/* Iterate through the list */
printf( "Freeing %d\n", ptr->index );
tmp = ptr;
free(tmp);
}
/* Free the last element of the list */
/* (where ptr->next IS null). */
printf( "Freeing %d\n", ptr->index );
free(ptr);
}
}
void print_list(struct node *mylist)
{
/* A pointer for iterating. */
struct node *ptr = NULL;
/* Iterate along the list. */
for( ptr = mylist; ptr->next != NULL; ptr = ptr->next )
{
printf( "mylist->index = %d\n", ptr->index );
}
/* For the last node (where ->next IS NULL), we need to print */
/* it outside the loop. */
printf( "mylist->index = %d\n", ptr->index );
}
int main()
{
struct node *my_list = list_new(0);
list_append(my_list);
list_append(my_list);
list_append(my_list);
list_append(my_list);
list_append(my_list);
list_append(my_list);
print_list(my_list);
free_list(my_list);
return 0;
}
This compiles fine, but when I run it, I get this -
mylist->index = 0
mylist->index = 134520821
mylist->index = 1
mylist->index = 134520822
mylist->index = 2
mylist->index = 134520823
mylist->index = 3
Freeing 0
Freeing 134520821
Freeing 1
Freeing 134520822
Freeing 2
Freeing 134520823
Freeing 3
andy@obsidian ~/testdir $
So - what am I doing wrong? It seems to be printing the *address* of the int at each alternate step - not the int itself.
Many thanks in advance for your help!
Bye for now -
- Andy