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");
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.
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;
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.
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;
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>
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
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.
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.
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.
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
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.