Click to See Complete Forum and Search --> : Method not searching properly...


YourSurrogateGod
October 7th, 2004, 09:06 PM
#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};

void insert_node(int, struct node**);
struct node * find_node(int, struct node**);
void user_insert(struct node **);
void user_search(struct node **);

int main()
{
struct node *curr;
struct node *list;
int i = 0;
int selection;

curr = list;

for(i = 0; i < 10; i++)
{
insert_node(i, &list);
}

do
{
printf("\n\nPlease enter a selection...\n\n");
printf("-1- See the entire list.\n");
printf("-2- Remove a node from the list.\n");
printf("-3- Insert a node into the list.\n");
printf("-4- Empty the entire list.\n");
printf("-5- Search for a specific node in the list.\n");
printf("-6- Exit.\n");
printf(" - > ");
scanf("%d", &selection);
fflush(stdin);

switch(selection)
{
case 1:
printf("Not yet implemented.\n");

break;

case 2:
printf("Not yet implemented.\n");

break;

case 3:
user_insert(&list);

break;

case 4:
printf("Not yet implemented.\n");

break;

case 5:

user_search(&list);

break;

case 6:
printf("Good bye.\n");

break;

default:
printf("Incorrect input...\n");
}
}
while(selection != 6);

return 0;
}

void insert_node(int value, struct node **list)
{
struct node *new_node = malloc(sizeof(struct node));

new_node -> data = value;

new_node -> next = (*list);

(*list) = new_node;
}

struct node * find_node(int value, struct node **list)
{
struct node *temp = NULL;
int i = 0;

while(!list)
{
if(value == (*(list + i)) -> data)
{
temp = *(list + i);

break;
}

i++;
}

return temp;
}

void user_insert(struct node **list)
{
int insert_value;

printf("Please enter a value that you would like to insert into the list - > ");
scanf("%d", &insert_value);
fflush(stdin);

insert_node(insert_value, list);
}

void user_search(struct node **list)
{
int search_value;

printf("Please enter a value that you would like to look up in the list - > ");
scanf("%d", &search_value);
fflush(stdin);

struct node *search_temp = find_node(search_value, list);

if(search_temp)
{
printf("The value %d has been found in the list.\n", search_value);
}
else
{
printf("The value %d has not been found in the list.\n", search_value);
}
}I'm using gcc. I'm a newbie to linked lists so I don't understand the idea too well, but I'm trying to learn through examples and attempts.

My problem is that whenever I try to search for some value in the list by using the method "find_node". However, the output is always ""The value %d has not been found in the list." (this is in the method user_search.) I don't understand why. If the specific node is not found, it should return a NULL. However, if something was found, then it should return a pointer to the node inside the linked list and a message confirming the finding should be displayed.

Ejaz
October 7th, 2004, 11:04 PM
Take a look at C++ Simple Linked Lists (http://www.cs.uregina.ca/Links/class-info/170/LinkedLists/Llist.html) for the theory and implementation, also this (http://www.mvps.org/user32/linkedlist.html) may be helpful. :thumb:

Ejaz
October 8th, 2004, 12:22 AM
Take a look at this, its a little modified version of your code.


#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};

void insert_node(int value , struct node **curr, struct node **prev);
void insert_at_head(int value , struct node **head, struct node **curr, struct node **prev);

struct node * find_node(int, struct node**);
void user_insert(struct node **curr, struct node **prev);

void user_search(struct node **);


int main()
{
struct node *head = NULL;
struct node *curr = NULL;
struct node *prev = NULL;

int i = 0;
int selection;

insert_at_head(0 , &head , &curr , &prev);

for(i = 1; i < 10; i++)
{
insert_node(i, &curr, &prev);
}

do
{
printf("\n\nPlease enter a selection...\n\n");
printf("-1- See the entire list.\n");
printf("-2- Remove a node from the list.\n");
printf("-3- Insert a node into the list.\n");
printf("-4- Empty the entire list.\n");
printf("-5- Search for a specific node in the list.\n");
printf("-6- Exit.\n");
printf(" - > ");
scanf("%d", &selection);
fflush(stdin);

switch(selection)
{
case 1: printf("Not yet implemented.\n"); break;

case 2: printf("Not yet implemented.\n"); break;

case 3: user_insert(&curr , &prev); break;

case 4: printf("Not yet implemented.\n"); break;

case 5: user_search(&head); break;

case 6: printf("Good bye.\n"); break;

default: printf("Incorrect input...\n");
}
}
while(selection != 6);

return 0;
}

void insert_at_head(int value , struct node **head, struct node **curr, struct node **prev)
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));

new_node -> data = value;
new_node -> next = NULL;

*head = *prev = *curr = new_node;
}

void insert_node(int value , struct node **curr, struct node **prev)
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
struct node *temp = *prev;

new_node -> data = value;
new_node -> next = NULL;

(*curr) = new_node;

(*prev)->next = *curr;
(*prev) = (*prev)->next;
}

struct node * find_node(int value, struct node **list)
{
struct node *temp = *list;

while(temp->next!=NULL)
{
if(value == temp->data)
return temp;

temp = temp->next;
}

return NULL;
}

void user_insert(struct node **curr, struct node **prev)
{
int insert_value;

printf("Please enter a value that you would like to insert into the list - > ");
scanf("%d", &insert_value);
fflush(stdin);

insert_node(insert_value, curr , prev);
}

void user_search(struct node **list)
{
int search_value;

printf("Please enter a value that you would like to look up in the list - > ");
scanf("%d", &search_value);
fflush(stdin);

struct node *search_temp = find_node(search_value, list);

if(search_temp)
{
printf("The value %d has been found in the list.\n", search_value);
}
else
{
printf("The value %d has not been found in the list.\n", search_value);
}
}

YourSurrogateGod
October 8th, 2004, 08:37 AM
Take a look at this, its a little modified version of your code.


#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};

void insert_node(int value , struct node **curr, struct node **prev);
void insert_at_head(int value , struct node **head, struct node **curr, struct node **prev);

struct node * find_node(int, struct node**);
void user_insert(struct node **curr, struct node **prev);

void user_search(struct node **);


int main()
{
struct node *head = NULL;
struct node *curr = NULL;
struct node *prev = NULL;

int i = 0;
int selection;

insert_at_head(0 , &head , &curr , &prev);

for(i = 1; i < 10; i++)
{
insert_node(i, &curr, &prev);
}

do
{
printf("\n\nPlease enter a selection...\n\n");
printf("-1- See the entire list.\n");
printf("-2- Remove a node from the list.\n");
printf("-3- Insert a node into the list.\n");
printf("-4- Empty the entire list.\n");
printf("-5- Search for a specific node in the list.\n");
printf("-6- Exit.\n");
printf(" - > ");
scanf("%d", &selection);
fflush(stdin);

switch(selection)
{
case 1: printf("Not yet implemented.\n"); break;

case 2: printf("Not yet implemented.\n"); break;

case 3: user_insert(&curr , &prev); break;

case 4: printf("Not yet implemented.\n"); break;

case 5: user_search(&head); break;

case 6: printf("Good bye.\n"); break;

default: printf("Incorrect input...\n");
}
}
while(selection != 6);

return 0;
}

void insert_at_head(int value , struct node **head, struct node **curr, struct node **prev)
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));

new_node -> data = value;
new_node -> next = NULL;

*head = *prev = *curr = new_node;
}

void insert_node(int value , struct node **curr, struct node **prev)
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
struct node *temp = *prev;

new_node -> data = value;
new_node -> next = NULL;

(*curr) = new_node;

(*prev)->next = *curr;
(*prev) = (*prev)->next;
}

struct node * find_node(int value, struct node **list)
{
struct node *temp = *list;

while(temp->next!=NULL)
{
if(value == temp->data)
return temp;

temp = temp->next;
}

return NULL;
}

void user_insert(struct node **curr, struct node **prev)
{
int insert_value;

printf("Please enter a value that you would like to insert into the list - > ");
scanf("%d", &insert_value);
fflush(stdin);

insert_node(insert_value, curr , prev);
}

void user_search(struct node **list)
{
int search_value;

printf("Please enter a value that you would like to look up in the list - > ");
scanf("%d", &search_value);
fflush(stdin);

struct node *search_temp = find_node(search_value, list);

if(search_temp)
{
printf("The value %d has been found in the list.\n", search_value);
}
else
{
printf("The value %d has not been found in the list.\n", search_value);
}
}
That makes more sense now. Thanks Ejaz.

YourSurrogateGod
October 9th, 2004, 11:13 AM
I think that I might have gotten somewhere...#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...

Ejaz
October 9th, 2004, 11:31 AM
The only problem is that when I execute the method 'view_nodes' it prints out 0.

I just copied your code and only typecasted the pointer when creating into (struct node *), like

struct node * new_node = (struct node *) malloc(sizeof(struct node));

and following output I get. There is no 0 in it :confused: (I haven't gone into the gone, its too late and I'm really tired, will see it in the morning) ;)

YourSurrogateGod
October 9th, 2004, 11:35 AM
I'd show you how things look for me but I don't know how to take screenshots in Linux Fedora Core 2 :o .

Oh well, good night :wave: .

YourSurrogateGod
October 9th, 2004, 12:25 PM
I think I figured it out. The odd address space that you were seeing on the side (and for some reason a 0 for me, I wonder if gcc sets all integers to 0 by default?) was the node that I initially created at the start, in the main method. So when I had that while loop go through the entire linked list, it went to that node as well.

YourSurrogateGod
October 9th, 2004, 07:02 PM
#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 ** );
void remove_nodes(struct node ** );
void custom_insert(struct node ** );

int main()
{
struct node *list = (struct node * )malloc(sizeof(struct node));
list -> next = NULL;
list -> data = -999;
int i = 0;

for(i = 0; i < 100; i++)
{
insert_node(&list, i + 1);
}

printf("View nodes...\n");
view_nodes(&list);

printf("\n\n");
custom_insert(&list);

printf("View nodes...\n");
view_nodes(&list);

printf("\n\nDeleted nodes...\n");
remove_nodes(&list);

free(list);
printf("\n\n");

return 0;
}

void insert_node(struct node ** head, int value)
{
struct node * new_node = (struct 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 = NULL;

if(temp != NULL)
{
next_node = temp -> next;

printf("%3d, ", temp -> data);

free(temp);

(*head) = next_node;
}
}

void view_nodes(struct node ** head)
{
struct node * temp = *head;

while(temp != NULL)
{
if(temp -> data != -999)
{
printf("%3d, ", temp -> data);
}

temp = temp -> next;
}
}

void remove_nodes(struct node ** head)
{
struct node * to_be_del = NULL;

while(temp_first -> next != NULL)
{
to_be_del = temp_first;
temp_first = temp_first -> next;
printf("%3d|", to_be_del -> data);
free(to_be_del);
}
}

void custom_insert(struct node ** head)
{
int position = 0;
int value = 0;
int iterations = 0;
struct node * new_node;
struct node * node_in_list = (* head);

printf("In which position would you like to insert the node - > ");
scanf("%d", &position);
printf("Which value would you like to insert - > ");
scanf("%d", &value);

while(node_in_list -> next != NULL)
{
if(iterations == position)
{
new_node = (struct node * )malloc(sizeof(struct node));
new_node -> next = NULL;
new_node -> data = value;

new_node -> next = node_in_list -> next;
node_in_list -> next = new_node;
}

iterations++;
node_in_list = node_in_list -> next;
}
}The last method isn't working too well. I'm trying to insert a node in a specific part of the list. I think I messed the pointers up. What I'm trying to do is have the program print out some integers to the command console and then the user has select a position in the list and that's where the new value will be inserted (position 1 will be at the very front, position 3 will be [(value1)(value2)(new_inserted_value)(value 3)....(value n)].) Thanks in advance.

YourSurrogateGod
October 9th, 2004, 11:48 PM
#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 ** );
void remove_nodes(struct node ** );
void custom_insert(struct node ** );

int main()
{
struct node *list = (struct node * )malloc(sizeof(struct node));
list -> next = NULL;
list -> data = -999;
int i = 0;

for(i = 0; i < 100; i++)
{
insert_node(&list, i + 1);
}

printf("View nodes...\n");
view_nodes(&list);

printf("\n\n");
custom_insert(&list);

printf("View nodes...\n");
view_nodes(&list);

printf("\n\nDeleted nodes...\n");
remove_nodes(&list);

free(list);
printf("\n\n");

return 0;
}

void insert_node(struct node ** head, int value)
{
struct node * new_node = (struct 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 = NULL;

if(temp != NULL)
{
next_node = temp -> next;

printf("%3d, ", temp -> data);

free(temp);

(*head) = next_node;
}
}

void view_nodes(struct node ** head)
{
struct node * temp = *head;

while(temp != NULL)
{
if(temp -> data != -999)
{
printf("%3d, ", temp -> data);
}

temp = temp -> next;
}
}

void remove_nodes(struct node ** head)
{
struct node * to_be_del = NULL;

while(temp_first -> next != NULL)
{
to_be_del = temp_first;
temp_first = temp_first -> next;
printf("%3d|", to_be_del -> data);
free(to_be_del);
}
}

void custom_insert(struct node ** head)
{
int position = 0;
int value = 0;
int iterations = 0;
struct node * new_node;
struct node * node_in_list = (* head);

printf("In which position would you like to insert the node - > ");
scanf("%d", &position);
printf("Which value would you like to insert - > ");
scanf("%d", &value);

while(node_in_list -> next != NULL)
{
if(iterations == position)
{
new_node = (struct node * )malloc(sizeof(struct node));
new_node -> next = NULL;
new_node -> data = value;

new_node -> next = node_in_list -> next;
node_in_list -> next = new_node;
}

iterations++;
node_in_list = node_in_list -> next;
}
}The last method isn't working too well. I'm trying to insert a node in a specific part of the list. I think I messed the pointers up. What I'm trying to do is have the program print out some integers to the command console and then the user has select a position in the list and that's where the new value will be inserted (position 1 will be at the very front, position 3 will be [(value1)(value2)(new_inserted_value)(value 3)....(value n)].) Thanks in advance.Got that to work as well.

Ejaz
October 9th, 2004, 11:50 PM
Here are some changed in the code and its working now.


#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 ** );
void remove_nodes(struct node ** );
void custom_insert(struct node ** );

int main()
{
struct node *list = (struct node * )malloc(sizeof(struct node));
list -> next = NULL;
list -> data = -999;
int i = 0;

for(i = 0; i < 100; i++)
{
insert_node(&list, i + 1);
}

printf("View nodes...\n");
view_nodes(&list);

printf("\n\n");
custom_insert(&list);

printf("View nodes...\n");
view_nodes(&list);

printf("\n\nDeleted nodes...\n");
remove_nodes(&list);

// Error - Crash
// free(list);
printf("\n\n");

return 0;
}

void insert_node(struct node ** head, int value)
{
struct node * new_node = (struct 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 = NULL;

if(temp != NULL)
{
next_node = temp -> next;

printf("%3d, ", temp -> data);

free(temp);

(*head) = next_node;
}
}

void view_nodes(struct node ** head)
{
struct node * temp = *head;

while(temp != NULL)
{
if(temp -> data != -999)
{
printf("%3d, ", temp -> data);
}

temp = temp -> next;
}
}

void remove_nodes(struct node ** head)
{
struct node * to_be_del = NULL;

// Error, what is temp_first <- By Ejaz
struct node * temp_first = *head;

while(temp_first -> next != NULL)
{
to_be_del = temp_first;
temp_first = temp_first -> next;
printf("%3d|", to_be_del -> data);
free(to_be_del);
}
}

void custom_insert(struct node ** head)
{
int position = 0;
int value = 0;
int iterations = 0;
struct node * new_node;
struct node * prev; // By Ejaz
struct node * node_in_list = (* head);

printf("In which position would you like to insert the node - > ");
scanf("%d", &position);
printf("Which value would you like to insert - > ");
scanf("%d", &value);

prev = *head; // By Ejaz
while(node_in_list -> next != NULL)
{
if(iterations == position)
{
new_node = (struct node * )malloc(sizeof(struct node));
new_node -> next = NULL;
new_node -> data = value;

new_node -> next = node_in_list -> next;
prev->next = new_node; // By Ejaz
break; // By Ejaz

// node_in_list -> next = new_node;
}

iterations++;
prev = node_in_list; // By Ejaz
node_in_list = node_in_list -> next;
}
}


The new node wasn't getting inserted in the list properly. Since this is a single link list, so while traversing the nodes, the track of previous node is important.

Suppose, the list is like 1->2->3->4->5

and you want to insert 6 after 3, then, you'll have to start traversing from the head (1, which you are already doing in your code), then whenever you move to the next node, save the pointer of the last node, its like leap frog jump, before jumping you put something that give you idea where you were last time.

So, when you create a new node, the next pointer of the new node should be pointing to the current position next pointer (node_in_list , in your code, which you did exactely), but the pointer of the previous node now should be pointing to the new node, rather then the node in the list (node_in_list), otherwise the new item will not be in the list.......it will be there, but will be a seperate node pointing to some particular node in the list chain.