Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 17th, 2005, 7:33 PM   #1
epswing
Newbie
 
epswing's Avatar
 
Join Date: Feb 2005
Posts: 14
Rep Power: 0 epswing is on a distinguished road
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
Nothing in the list. Good!


Insert 10, then terminate:
Enter a series of intergers, eg. 10 3 8 0. Seperate by spaces, 0 to terminate:
10 0
10
There's a 10 in the list. Good!


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
There's my 10, then there's a 0...hm, something went wrong!


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>
...yeah...I don't know.


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?
epswing is offline   Reply With Quote
Old Feb 17th, 2005, 7:40 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
What happens if you hit enter between each number instead of using space?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 17th, 2005, 7:46 PM   #3
epswing
Newbie
 
epswing's Avatar
 
Join Date: Feb 2005
Posts: 14
Rep Power: 0 epswing is on a distinguished road
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);
then I get the same results no matter how many input numbers there are: It only stores the last two integers before the terminating 0.

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.
epswing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC