![]() |
| | #1 |
| Brak Join Date: Mar 2007 Location: Bangkok
Posts: 62
| deleting a node in linked list Code: Enter (+) integers and terminate it by 0 1 2 3 0 The list is: 1 2 3 3 items were entered to the list Sum of the items are: 6 Enter a integer to check is it in the list:2 2 is in the list The list is: 1 2 3 Enter a number to delete it from the list:2 The list is modified The list is: 1 0 3 This is the function: Code: /*Function to delete a specific number from the list*/
void delete(int n, node_ptr list)
{
node_ptr tmp;
node_ptr current = list->next;
int number;
printf("Enter a number to delete it from the list:");
scanf("\n%d",&number);
while(current != NULL)
{
if(current->data_item == number)
{
tmp = current->next;
free (current);
current = tmp;
printf("The list is modified\n");
break;
}
else
{
current = current->next;
}
}
if(current == NULL )
{
printf("%d is not in the list\n", number);
}
return;
Code: #include <stdio.h>
#include <stdlib.h>
#include "1.h"
/*Function to create an empty List*/
node_ptr create(void)
{
node_ptr list=(node_ptr)malloc(sizeof(struct node));
list->data_item = 0;/*The first node*/
list->next = NULL; /*the last node*/
return list;
}
/*Function to return dynamically allocated memory in list*/
void destroy(node_ptr list)
{
node_ptr current = list;
while(current)
{
node_ptr to_free = current;
current = current -> next;
free(to_free);
}
}
/*Function to insert n at front of list*/
void insert_at_front(int n, node_ptr list)
{
node_ptr new_node= (node_ptr)malloc(sizeof(struct node));
new_node -> data_item = n;
new_node -> next = list-> next;
list -> next = new_node;
}
/*Function to print list*/
void print(node_ptr list)
{
node_ptr current = list -> next;
printf("The list is: ");
while(current)
{
printf("%d ", current->data_item);
current = current->next;
}
printf("\n");
}
/*Function to insert n in ( non decreasing ) order in list - assuming list items are already in ( non decreasing ) order. */
void insert_in_order(int n, node_ptr list)
{
node_ptr before = list;
node_ptr new_node = (node_ptr) malloc(sizeof(struct node));
new_node-> data_item = n;
while(before-> next && (before->next->data_item < n))
{
before = before->next;
}
new_node->next = before-> next;
before->next = new_node;
}
/*Function to find the number of items in the list*/
int length(node_ptr list)
{
node_ptr current = list->next;
int r = 0;
while(current)
{
r++;
current = current->next;
}
printf("\n%d items were entered to the list", r);
return r;
}
/*Function to sum the integer values in the list*/
int sum(node_ptr list)
{
node_ptr current = list->next;
int t = 0;
while(current)
{
t = t + current->data_item;
current = current->next;
}
printf("\nSum of the items are: %d\n", t);
return t;
}
/*Function to find a specific number in the list*/
node_ptr find(int n, node_ptr list)
{
node_ptr current = list->next;
int number=0;
printf("Enter a integer to check is it in the list:");
scanf("\n%d",&number);
while(current != NULL)
{
if(current->data_item == number)
{
printf("%d is in the list\n", number);
break;
}
else
{
current = current->next;
}
}
if(current == NULL)
{
printf("%d is not in the list\n", number);
}
return current;
}
/*Function to delete a specific number from the list*/
void delete(int n, node_ptr list)
{
node_ptr tmp;
node_ptr current = list->next;
int number;
printf("Enter a number to delete it from the list:");
scanf("\n%d",&number);
while(current != NULL)
{
if(current->data_item == number)
{
tmp = current->next;
free (current);
current = tmp;
printf("The list is modified\n");
break;
}
else
{
current = current->next;
}
}
if(current == NULL )
{
printf("%d is not in the list\n", number);
}
return;
}
__________________ “Example isn't another way to teach, it is the only way to teach” Albert Einstein |
| BoneXXX is offline | |
| | #2 |
| The larch Join Date: May 2006
Posts: 3,082
| Code: tmp = current->next; free (current); current = tmp; Hint, you need to link the previous node before current to current->next. |
| anon is offline | |
| | #3 | |
| Brak Join Date: Mar 2007 Location: Bangkok
Posts: 62
| Quote:
Code: node_ptr before = list; before -> next = current-> next; current = before -> next; free(current);
__________________ “Example isn't another way to teach, it is the only way to teach” Albert Einstein | |
| BoneXXX is offline | |
| | #4 |
| The larch Join Date: May 2006
Posts: 3,082
| I think you might try something like this: Code: Node* current = first;
Node* previous = NULL;
while (current != NULL) {
//...
previous = current;
current = current->next;
}
|
| anon is offline | |
| | #5 |
| Brak Join Date: Mar 2007 Location: Bangkok
Posts: 62
| I am sorry u helped lot but I just couldn't make it. When I try to understand more I started to confuse more: Code:
void delete(int n, node_ptr list)
{
node_ptr previous = NULL;
node_ptr current = list->next;
int number;
printf("Enter a number to delete it from the list:");
scanf("\n%d",&number);
while(current != NULL)
{
if(current->data_item == number && previous->next->data_item != 0)
{
previous = current;
previous -> next = (current->next)->next;
current = current -> next;
free(current);
printf("The list is modified\n");
break;
}
else
{
current = current->next;
}
}
if(current == NULL )
{
printf("%d is not in the list\n", number);
}
return;
}
Code: Enter (+) integers and terminate it by 0 1 2 3 0 The list is: 1 2 3 3 items were entered to the list Sum of the items are: 6 Enter a integer to check is it in the list:3 3 is in the list The list is: 1 2 3 Enter a number to delete it from the list:3 Segmentation fault
__________________ “Example isn't another way to teach, it is the only way to teach” Albert Einstein |
| BoneXXX is offline | |
| | #6 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,260
| 1 links to 2 links to 3 If you want to remove 2, what must happen? 1 links to 3 You therefore must have a pointer pointing to the node before the one you're considering for removal, so you can make that node point to the one after the one you're going to remove. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #7 | |
| Brak Join Date: Mar 2007 Location: Bangkok
Posts: 62
| Quote:
__________________ “Example isn't another way to teach, it is the only way to teach” Albert Einstein | |
| BoneXXX is offline | |
| | #8 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,260
| Code: foo *prev = NULL, *curr = list;
while( curr )
{
... check to see if this is what you need...
... handle appropriately ...
... fix up your pointers ...
prev = curr;
curr = curr->next;
}
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #9 |
| Brak Join Date: Mar 2007 Location: Bangkok
Posts: 62
| The thing that I don't understand is: For example the list is 1 2 3 4 and I want to delete 3. When the program is searching to find the integer to delete( by while loop) it passes 2 and it comes to 3 then bingo current is 3 the program found it, and in that stage current-> next is 4. So , I want to link 2 to the 4 and free the node where the 3 is. But the thing that I don't get, I don't know how will I go back to 2 and link it to the 4. Because I already passed the value 2.
__________________ “Example isn't another way to teach, it is the only way to teach” Albert Einstein |
| BoneXXX is offline | |
| | #10 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,260
| You "go back" to two by having a pointer pointing to the previous node. Just like how you use one to track "current", you have another one that is tracking "curren - 1". That is to say, it's trailing one loop behind. You've been shown twice how to do it. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #11 | |
| Registered User Join Date: Mar 2005
Posts: 37
| Quote:
in your example, 2 will be held by prev, 3 will be held by current and 4 will be held by nex . now all you have to do is prev->next= nex ; and free (current). Happy hacking.. ;-) | |
| Machoscorpion is offline | |
| | #12 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,260
| You don't need a next node because you already have it as a member of the curr node itself. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #13 |
| Brak Join Date: Mar 2007 Location: Bangkok
Posts: 62
| Sorry guys I had problem with my internet connection and couldn't reply you, I solved the problem(of course with your helps). Thanks Code: void delete(int n, node_ptr list)
{
node_ptr previous = list;
node_ptr current = list->next;
printf("Enter a number to delete it from the list:");
scanf("\n%d",&n);
while(current != NULL)
{
if(current->data_item == n)
{
previous->next = current->next;
free(current);
printf("The list is modified\n");
break;
}
else
{
previous = current;
current = current->next;
}
}
if(current == NULL )
{
printf("%d is not in the list\n", n);
}
return;
}
__________________ “Example isn't another way to teach, it is the only way to teach” Albert Einstein |
| BoneXXX is offline | |
| | #14 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,260
| If the node is the first one in the list, your code won't remove it because you're starting looking at "list->next" as current. Also, if a NULL is passed as the list to your function, your assignment to "current" at the start will crash it when you try to access "list->next". Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #15 |
| Brak Join Date: Mar 2007 Location: Bangkok
Posts: 62
| Actually I thought that too, But it perfectly ran with simple 1,2,3 linked list. I deleted all the nodes one by one and no problemo. i ll try it again.
__________________ “Example isn't another way to teach, it is the only way to teach” Albert Einstein |
| BoneXXX is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pleas take a look & give a critique | sh3rpa | C++ Programming | 14 | 10-19-2007 10:01 PM |
| Reverse function for linked list | Brigs76 | C++ Programming | 1 | 10-25-2006 10:01 AM |
| Problems with deleting a node in a singly linked list | omishompi | C++ Programming | 7 | 02-23-2006 06:27 PM |
| Problem with linked list ADT and incomplete structure | prawntoast | C Programming | 1 | 04-30-2005 01:29 AM |
| singly linked list | clarinetster | C Programming | 2 | 08-26-2001 10:21 PM |