I have a program that gives me this output:
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
the problem is when i wanted to delete a node, it deletes it but replaces with O. I try to point the node after the node that I want to delete and after that remove the node.But I couldn't make it.
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;
this is the all code:
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;
}