#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.
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...
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.
#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.
#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.
Here are some changed in the code and its working now.
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 ** );
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.