![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 14
Rep Power: 0
![]() |
simple linkedlist not working
Hello all,
I'm a java/python programmer plunging into C. This "pointer" business is pretty neat, although it took a while to visualize pointers TO pointers and stuff. Anyways, I thought I had it figured out, however I get wildly unexpected results with this simple singly-linked-list. All I want it to do for now is insert and print. #include <stdio.h>
/* node stucture */
struct node {
int value;
struct node * next;
};
/*
* inserts int 'i' at the end of linkedlist 'list'
*/
void insert(struct node ** list, int i)
{
/* create a node pointer for the node to be inserted*/
struct node * newnode = NULL;
/* allocate space for the node and assign to above pointer */
newnode = (struct node *) malloc(sizeof(struct node));
/* give our new node its info */
newnode->value = i;
newnode->next = NULL;
if (*list == NULL) {
/* the list is empty, assign the first node */
*list = newnode;
} else {
/* move to the end of the list, assign the newnode */
while ((*list)->next != NULL) {
*list = (*list)->next;
}
(*list)->next = newnode;
free(newnode);
}
}
/*
* prints linklist 'list' to the screen
*/
void print(struct node * list)
{
while (list != NULL) {
printf("%d ", list->value);
list = list->next;
}
printf("\n");
}
/*
* gather some numbers to put in the list, then print the whole list
*/
main()
{
struct node * first = NULL;
int n = 0;
printf("Enter a series of intergers, eg. 10 3 8 0. Seperate by spaces, 0 to terminate:\n");
do {
scanf("%d", &n);
if (n != 0)
insert(&first, n);
} while (n != 0);
print(first);
}Now then. Here's some output: Terminate right away: Enter a series of intergers, eg. 10 3 8 0. Seperate by spaces, 0 to terminate: 0 Insert 10, then terminate: Enter a series of intergers, eg. 10 3 8 0. Seperate by spaces, 0 to terminate: 10 0 10 Insert 10, then 12, then terminate: Enter a series of intergers, eg. 10 3 8 0. Seperate by spaces, 0 to terminate: 10 12 0 10 0 Insert 10, then 12, then 15, then terminate: Enter a series of intergers, eg. 10 3 8 0. Seperate by spaces, 0 to terminate: 10 12 15 0 <UNENDING LOOP OF OUTPUT DEATH HERE> Anything over 2 elements to be inserted, and it goes nuts. Inserting only 2 gives unexpected output. Inserting 1 gives the correct output. Anyone see where my problem is? |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
What happens if you hit enter between each number instead of using space?
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 14
Rep Power: 0
![]() |
Wow, never though of that. Just tried, same results.
Edit: Someone suggested that I can't borrow memory for the node, stash data in it, point a link at it, then immediately free it. I thought I was freeing the pointer to the node. Since something else is now pointing at it...isn't that ok? I guess not, I don't get why though. If I remove the line free(newnode); ie. 1 3 0 stores 1 and 3 in the list. 1 3 5 0 stores 3 and 5 in the list. Last edited by epswing; Feb 17th, 2005 at 10:05 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|