1) Write a Menu driven C program to implement the following operations
in the linked list.
Insert after a given node.
Insert before a given node.
Delete a given node.
#include <stdio.h>
#include <stdlib.h>
// Here is your linked list node
struct node {
int info;
struct node* link;
};
struct node* start = NULL;
int isCreated = 0;
// This is method for create the list
// Note: Without Creating list there will be no operation perform
void createList()
{
// check list is created or not
if(isCreated == 0 ){
isCreated = 1;
printf("\n List is Created. \n");
}
else {
printf("\n List is Already Created. \n");
}
}
// This is method for display the list that you created
void showList()
{
struct node* temp;
// check list is created or not
if(isCreated == 0 ){
printf("No List Create Yet, Please Create List First!");
}else{
// check list is empty or not
if (start == NULL) {
printf("\nList is empty\n");
}else {
// start display all info until temp not equal to Null
temp = start;
printf("============== Your List Start ==================\n");
while (temp != NULL) {
printf("%d\t", temp->info);
temp = temp->link;
}
printf("\n============== Your List End ==================");
}
}
}
// This is method for insert the node at the starting point list
void insertStart()
{
// check list is created or not
if(isCreated == 0 ){
printf("No List Create Yet, Please Create List First!");
}else{
int data;
struct node* temp;
// malloc method is use here for dynamic memory allocation to node
temp = malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
// pointer go to the temp and assign to the start
temp->info = data;
temp->link = start;
start = temp;
}
}
// This is method for insert the node at the ending point list
void insertEnd()
{
// check list is created or not
if(isCreated == 0 ){
printf("No List Create Yet, Please Create List First!");
}else{
int data;
struct node *temp, *head;
// malloc method is use here for dynamic memory allocation to node
temp = malloc(sizeof(struct node));
// Enter the number
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
// Changes links
temp->link = 0;
temp->info = data;
head = start;
// Loop until not getting null value in link
while (head->link != NULL) {
head = head->link;
}
// Set Node At the Last
head->link = temp;
}
}
// This is method for insert the node at the given position
void insertPosition()
{
// check list is created or not
if(isCreated == 0 ){
printf("No List Create Yet, Please Create List First!");
}else{
struct node *temp, *newnode;
int pos, data, i = 1;
// malloc method is use here for dynamic memory allocation to node
newnode = malloc(sizeof(struct node));
printf("\nEnter position and data :");
scanf("%d %d", &pos, &data);
temp = start;
newnode->info = data;
newnode->link = 0;
// Find the position
while (i < pos - 1) {
temp = temp->link;
i++;
}
// Set the newnode
newnode->link = temp->link;
temp->link = newnode;
}
}
// This is method for delete the node at starting point of list
void deleteFirst()
{
// check list is created or not
if(isCreated == 0 ){
printf("No List Create Yet, Please Create List First!");
}else{
struct node* temp;
// check list is empty or not
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->link;
// Remove First Node
free(temp);
}
}
}
// This is method for delete the node at ending point of list
void deleteEnd()
{
// check list is created or not
if(isCreated == 0 ){
printf("No List Create Yet, Please Create List First!");
}else{
struct node *temp, *prevnode;
// check list is empty or not
if (start == NULL)
printf("\nList is Empty\n");
else {
temp = start;
// loop till the end
while (temp->link != 0) {
prevnode = temp;
temp = temp->link;
}
// Free the last node
free(temp);
prevnode->link = 0;
}
}
}
// This is method for delete the node at given index point
void deletePosition()
{
// check list is created or not
if(isCreated == 0 ){
printf("No List Create Yet, Please Create List First!");
}else{
struct node *temp, *position;
int i = 1, pos;
// check list is empty or not
if (start == NULL)
printf("\nList is empty\n");
else {
printf("\nEnter position : ");
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
// Its check until getting position
while (i < pos - 1) {
temp = temp->link;
i++;
}
// This below code will basically change the links of node
position = temp->link;
temp->link = position->link;
// Free the position that user entered
free(position);
}
}
}
int main()
{
int choice;
while (1) {
printf("\n\t1 For create a list\n");
printf("\t2 For display a list\n");
printf("\t3 For insertion at begin\n");
printf("\t4 For insertion at end\n");
printf("\t5 For insertion at any position\n");
printf("\t6 For deletion of begin\n");
printf("\t7 For deletion of end\n");
printf("\t8 For deletion of element at any position\n");
printf("\t0 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
switch (choice) {
case 1:
createList();
break;
case 2:
showList();
break;
case 3:
insertStart();
break;
case 4:
insertEnd();
break;
case 5:
insertPosition();
break;
case 6:
deleteFirst();
break;
case 7:
deleteEnd();
break;
case 8:
deletePosition();
break;
case 0:
exit(1);
break;
default:
printf("Please Enter Correct Choice!\n");
}
}
return 0;
}
Comments
Leave a comment