Program:
#include <stdio.h>
#include <stdlib.h> // for calloc
typedef struct node {
struct node *next;
int value;
}node_t;
node_t *root = NULL;
void line_80(char *str)
{
int i;
for (i = 0; i < 80; printf("%s", str), i++);
printf("\n");
}
void display_list(void)
{
node_t *temp = NULL;
if (root == NULL) {
printf("List is empty \n");
}
printf("\tThe available list value is: ");
for (temp = root; temp ; temp = temp->next) {
printf("%d ", temp->value);
if (temp->next) {
printf("-> ");
}
}
printf("\n\n");
}
int insert_list(node_t **root_node, int value)
{
node_t *temp = NULL;
temp = (node_t *) calloc(1, sizeof(node_t));
temp->value = value;
if (*root_node == NULL) {
*root_node = temp;
} else {
temp->next = *root_node;
*root_node = temp;
}
}
void delete_list(node_t **root_node, int value)
{
node_t *temp = NULL, *prev = NULL;
if (*root_node == NULL) {
printf("List is empty, so could not delete any value \n");
return;
}
for (temp = prev = *root_node; temp ; prev = temp, temp = temp->next) {
if (temp->value == value) {
if (temp == *root_node) {
*root_node = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
temp = NULL;
printf("Delete the node with value %d \n", value);
return;
}
}
}
int main()
{
int input, choice = 0, value = 0;
while (1) {
line_80("*");
printf("\t 1. Display the list\n\t 2. Insert to list: \n\t 3.Delete from list\n");
line_80("*");
printf("\n\t Enter your choice: ");
scanf("%d", &choice);
if (choice <= 0 || choice >= 4) {
printf("Please choose correct choice \n");
continue;
}
switch(choice)
{
case 1:
display_list();
break;
case 2:
printf("Enter the value to insert:");
scanf("%d", &value);
insert_list(&root, value);
break;
case 3:
printf("Enter the value to delete:");
scanf("%d", &value);
delete_list(&root, value);
break;
default:
printf("Wrong choice, select correct choice \n");
}
}
}
Output:
learn$ ./a.out
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 1
List is empty
The available list value is:
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 2
Enter the value to insert:1
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 2
Enter the value to insert:2
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 2
Enter the value to insert:3
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 1
The available list value is: 3 -> 2 -> 1
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 3
Enter the value to delete:2
Delete the node with value 2
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 1
The available list value is: 3 -> 1
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 3
Enter the value to delete:1
Delete the node with value 1
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 1
The available list value is: 3
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 3
Enter the value to delete:3
Delete the node with value 3
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
Enter your choice: 1
List is empty
The available list value is:
********************************************************************************
1. Display the list
2. Insert to list:
3.Delete from list
********************************************************************************
No comments:
Post a Comment