1. Write a menu driven program in c to perform the following operations in single linked list:
Creation of list
Traversal of the list
Insertion at the begining of the list
Program
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void traverseList();
void insertNodeAtBeginning(int data);
int main()
{
int n,t;
while(1)
{
printf("What operation do you want to perform:\n");
printf("Enter 1 to create list\n");
printf("Enter 2 to traverse a list\n");
printf("Enter 3 to insert node in the beginning\n");
scanf("%d",&n);
switch (n){
case 1:
{
printf("Enter the size of list you want to create: ");
scanf("%d",&t);
createList(t);
break;
}
case 2:
{
traverseList();
break;
}
case 3:
{
printf("Enter what value you want to insert at the beginning: ");
scanf("%d",&t);
insertNodeAtBeginning(t);
break;
}
default:
return 0;
}
}
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
void traverseList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
return;
}
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
void insertNodeAtBeginning(int data)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = head;
head = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
What operation do you want to perform:
Enter 1 to create list
Enter 2 to traverse a list
Enter 3 to insert node in the beginning
1
Enter the size of list you want to create: 5
Enter the data of node 1: 21
Enter the data of node 2: 32
Enter the data of node 3: 323
Enter the data of node 4: 44
Enter the data of node 5: 55
What operation do you want to perform:
Enter 1 to create list
Enter 2 to traverse a list
Enter 3 to insert node in the beginning
2
Data = 21
Data = 32
Data = 323
Data = 44
Data = 55
What operation do you want to perform:
Enter 1 to create list
Enter 2 to traverse a list
Enter 3 to insert node in the beginning
3
Enter what value you want to insert at the beginning: 200
DATA INSERTED SUCCESSFULLY
What operation do you want to perform:
Enter 1 to create list
Enter 2 to traverse a list
Enter 3 to insert node in the beginning
2
Data = 200
Data = 21
Data = 32
Data = 323
Data = 44
Data = 55
What operation do you want to perform:
Enter 1 to create list
Enter 2 to traverse a list
Enter 3 to insert node in the beginning
2. Write a menu driven program in c to perform the following operations in single linked list:
1.Creation of list
2.Traversal of the list
3.Insertion at the end of the list
4.Insertion at the specified location
Program
#include<stdlib.h>
#include <stdio.h>
void create();
void display();
void insert_end();
void insert_pos();
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
int main()
{
int choice;
while(1){
printf("\n MENU ");
printf("\n 1.Create ");
printf("\n 2.Display ");
printf("\n 3.Insert at the end ");
printf("\n 4.Insert at specified position ");
printf("\n 5.Exit ");
printf("\n--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_end();
break;
case 4:
insert_pos();
break;
default:
printf("\n Wrong Choice:");
break;
}
}
return 0;
}
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:");
exit(0);
}
printf("\nEnter the data value for the node:");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty:");
return;
}
else
{
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->info );
ptr=ptr->next ;
}
}
}
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:");
return;
}
printf("\nEnter the data value for the node:" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
}
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:");
return;
}
printf("\nEnter the position for the new node to be inserted:");
scanf("%d",&pos);
printf("\nEnter the data value of the node:");
scanf("%d",&temp->info) ;
temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++) { ptr=ptr->next;
if(ptr==NULL)
{
printf("\nPosition not found:");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}
}
MENU
1.Create
2.Display
3.Insert at the end
4.Insert at specified position
5.Exit
--------------------------------------
Enter your choice: 1
Enter the data value for the node:234
MENU
1.Create
2.Display
3.Insert at the end
4.Insert at specified position
5.Exit
--------------------------------------
Enter your choice: 2
The List elements are:
234
MENU
1.Create
2.Display
3.Insert at the end
4.Insert at specified position
5.Exit
--------------------------------------
Enter your choice: 3
Enter the data value for the node:32
MENU
1.Create
2.Display
3.Insert at the end
4.Insert at specified position
5.Exit
--------------------------------------
Enter your choice: 2
The List elements are:
234
32
MENU
1.Create
2.Display
3.Insert at the end
4.Insert at specified position
5.Exit
--------------------------------------
Enter your choice: 4
Enter the position for the new node to be inserted:1
Enter the data value of the node:111
MENU
1.Create
2.Display
3.Insert at the end
4.Insert at specified position
5.Exit
--------------------------------------
Enter your choice: 2
The List elements are:
234
111
32
3. Write a menu driven program in c to perform the following operations in single linked list:
1.Creation of list
2.Traversal of the list
3.Deletion of last node
4.Deletion of node at the specified location
Program
#include<stdlib.h>
#include <stdio.h>
void create();
void display();
void delete_end();
void delete_pos();
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
int main()
{
int choice;
while(1){
printf("\n MENU \n");
printf("\n 1.Create ");
printf("\n 2.Display ");
printf("\n 3.Delete from the end ");
printf("\n 4.Delete from specified position ");
printf("\n 5.Exit ");
printf("\n--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
delete_end();
break;
case 4:
delete_pos();
break;
case 5:
exit(0);
break;
default:
printf("\n Wrong Choice:\n");
break;
}
}
return 0;
}
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->info );
ptr=ptr->next ;
}
}
}
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nList is Empty:");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty:\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
}
}
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 1
Enter the data value for the node: 122
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 1
Enter the data value for the node: 133
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 1
Enter the data value for the node: 143
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 1
Enter the data value for the node: 123
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 1
Enter the data value for the node: 543
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 2
The List elements are:
122
133
143
123
543
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 3
The deleted element is:543
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 2
The List elements are:
122
133
143
123
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 4
Enter the position of the node to be deleted: 2
The deleted element is:143
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 2
The List elements are:
122
133
123
MENU
1.Create
2.Display
3.Delete from the end
4.Delete from specified position
5.Exit
--------------------------------------
Enter your choice: 5
Tags:
DSA