|
-
October 9th, 2004, 11:13 AM
#5
Re: Method not searching properly...
I think that I might have gotten somewhere...
Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert_node(struct node ** , int );
void remove_node(struct node ** );
void view_nodes(struct node ** );
int main()
{
struct node *list = malloc(sizeof(struct node));
list->next = NULL;
int i = 0;
for(i = 0; i < 100; i++)
{
insert_node(&list, i + 1);
}
printf("View nodes...\n");
view_nodes(&list);
printf("\n\n");
printf("Deleted node...\n");
for(i = 0; i < 100; i++)
{
remove_node(&list);
}
free(list);
putchar('\n');
return 0;
}
void insert_node(struct node ** head, int value)
{
struct node * new_node = malloc(sizeof(struct node));
new_node -> data = value;
new_node -> next = (*head);
(*head) = new_node;
}
void remove_node(struct node ** head)
{
struct node * temp = *head;
struct node * next_node;
if(temp != NULL)
{
next_node = temp -> next;
printf("%d, ", temp -> data);
free(temp);
(*head) = next_node;
}
}
void view_nodes(struct node ** head)
{
struct node * temp = *head;
while(temp != NULL)
{
printf("%d, ", temp -> data);
temp = temp -> next;
}
}
The only problem is that when I execute the method 'view_nodes' it prints out 0. That should not be possible, since in the first for-loop in the main method I increment i by 1, which should give me numbers from 100 to 1 (this is the exact result that I get when I run the method remove_node.) Why is this happening?
// Probably something extremelly intelligament that I've done...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|