ALOK KUMAR Roll-70 (1)

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 13

Name : Shibra firdousi

Roll no: 80
Stream : CSE1B X-group
NAME:ALOK KUMAR.
ROLL : 70.
NAME:ALOK KUMAR.
CLASS: CSE 1B"X".
ROLL : 70.
CLASS: CSE 1B"X".
Problem 1: Write a menu driven program in C to delete the first element, last
element and the given
element from the linked list.
Problem 1: Write a menu driven program in C to delete the first element, last
element and the given
element from the linked list.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
node{
data;
node *next;
};
struct node *start = NULL;
struct node *create_LL(struct node *);
struct node *display(struct node *);
struct node *delete_beginning(struct node *);
struct node *delete_end(struct node *);
struct node *delete_between(struct node *, int);
*start = NULL;
*create_LL(
*display(
*delete_beginning(
*delete_end(
*delete_between(
node *);
node *);
node *);
node *);
node *,
3
int main()
{
main()
int choice,num;
do{
choice,num;
lone
printf("\nEnter 1 for creating a
linked list\n");
printf("\nEnter 1 for creating a linked list\n");
printf("Enter 2 for Delete at Beginning a
printf("Enter 2 for Delete at Beginning a linked list\n");
printf("Enter 3 for Delete at End a linked list\n");
printf("Enter 4 for Delete at Between in a linked list\n");
printf("Enter 5 for displaying a linked list\n");
linked list\n");
printf("Enter 3 for Delete at End a
linked list\n");
printf("Enter
for Delete at Between
in a
linked list\n");
printf("Enter 5 for displaying a
linked list\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice){
case 1: start = create_LL(start);
printf("\n Linked List is created.\n");
break;
start = deletebeginning(start);
printf("\n First node deleted.\n");
break;
case 1: start = create_LL(start);
printf("\n Linked List is created.\n");
break;
case 2: start = delete_beginning(start);
printf("\n First node deleted.\n");
break;
case 3: start = delete_end(start);
case
3:
start
delete_end(start);
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice){
#include<stdio.h>
#include<stdlib.h>

printf("\n Last node deleted.\n");


break;
case 4: printf("\n Enter the position of the node to be deleted:
");
scanf("%d", &num);
start = delete_between(start, num);
printf("\n Node deleted at position %d.\n", num);
break;
case 5: start = display(start);
break;
}
} while(choice <6 && choice >0);
return 0;
}
struct node *create_LL(struct node *start){
struct node *new_node, *ptr;
int num;
printf("\n Enter 00 to end \n");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num != 00){
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
if(start == NULL){
new_node -> next = NULL;
start = new_node;
}
else{
ptr = start;
while(ptr->next != NULL){
ptr = ptr->next;}
ptr->next = new_node;
new_node -> next = NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *delete_beginning(struct node *start){
struct node *ptr;
if(start != NULL){
ptr = start;

start = start->next;
free(ptr);
}
return start;
}
struct node *delete_end(struct node *start){
struct node *ptr, *p;
if(start != NULL){
ptr = start;
while(ptr->next != NULL){
p = ptr;
ptr = ptr->next;
}
p->next = NULL;
free(ptr);
}
return start;
}
struct node *delete_between(struct node *start, int position){
struct node *ptr, *p;
int i;
ptr = start;
for(i=1; i<position; i++){
p = ptr;
ptr = ptr->next;
if(ptr == NULL){
printf("\n Less elements exist in the list than the provided
position \n");
return start;
}
}
p->next = ptr->next;
free(ptr);
return start;
}
struct node *display(struct node *start){
struct node *ptr;
ptr = start;
while(ptr != NULL){
printf("\t %d", ptr->data);
ptr = ptr -> next;
}
return start;
}

Output:
Enter 1
for creating a linked list
Enter 2 for Delete at Beginning a linked list
Enter 3 for Delete at End a linked list
Enter 4 for Delete at Between in a linked list
Enter 5
for displaying a linked list
Enter your choice: 1
Enter @@
to end
Enter the data : 6
Enter the data
Enter the data
Enter the data
Enter the data
Enter the data
Enter the data : @0
Linked List is created.
Enter 1
for creating a linked list
Enter 2 for Delete at Beginning a linked list
Enter 3 for Delete at End a linked list
Enter 4 for Delete at Between in a linked list
Enter 5
Steen e0wate
for displaying a linked list
ket
First node deleted.

Enter 1 for creating a linked list


Enter 2 for Delete at Beginning a linked list
Enter 3 for Delete at End a linked list
Enter 4 for Delete at Between in a linked list
Enter 5
for displaying a linked list
Enter your choice: 3
Last node deleted.
Enter 1
for creating a linked list
Enter 2 for Delete at Beginning a linked list
Enter 3 for Delete at End a linked list
Enter 4 for Delete at Between in a linked list
Enter 5
for displaying a linked list
Enter your choice: 4
Enter the position of the node to be deleted: 2
Node deleted at position 2.
Enter 1 for creating a linked list
Enter 2
for Delete at Beginning a linked list
Enter 3 for Delete at End a linked list
Enter 4 for Delete at Between in a linked list
Enter 5
for displaying a linked list
Enter your choice: 5
8
4
=

Problem 2: Write a menu driven program in C to insert an element at the beginning,


end, after and
before of the given element in a doubly linked list.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
struct node *prev;
};
struct node *start = NULL;
struct node *create_LL(struct node *);
struct node *display(struct node *);
struct node *insert_at_beginning(struct node *, int);
struct node *insert_at_end(struct node *, int);
struct node *insert_after_node(struct node *, int, int);
struct node *insert_before_node(struct node *, int, int);
int main()
{
int choice,num,val;
do{
printf("\nEnter 1 for creating a linked list\n");
printf("Enter 2 for inserting at Beginning a linked list\n");
printf("Enter 3 for inserting at End a linked list\n");
printf("Enter 4 for inserting a node After a specific node in the
linked list\n");
printf("Enter 5 for inserting a node before a specific node in the
linked list\n");
printf("Enter 6 for displaying a linked list\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice){
case 1: start = create_LL(start);
printf("\n Linked List is created.\n");
break;
case 2: printf("\nInserted at the beginning: ");
printf("\n Enter the data to be inserted at the
beginning: ");
scanf("%d",&num);
start = insert_at_beginning(start, num);
break;
case 3: printf("\n Enter the data to be inserted at the end: ");
scanf("%d", &num);
start = insert_at_end(start, num);
break;

case 4: printf("\nEnter the data to be inserted: ");


scanf("%d", &num);
printf("Enter the node after which the data has to be
inserted: ");
scanf("%d", &val);
start = insert_after_node(start, num, val);
break;
case 5: printf("\nEnter the data to be inserted: ");
scanf("%d", &num);
printf("Enter the node before which the data has to be
inserted: ");
scanf("%d", &val);
start = insert_before_node(start, num, val);
break;
case 6: start = display(start);
break;
}
} while(choice <7 && choice >0);
return 0;
}
struct node *create_LL(struct node *start){
struct node *new_node, *ptr;
int num;
printf("\nEnter -1 to end \n");
printf("Enter the data : ");
scanf("%d", &num);
while(num != -1){
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
if(start == NULL){
new_node -> next = NULL;
new_node -> prev = NULL;
start = new_node;
}
else{
ptr = start;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = new_node;
new_node -> prev = ptr;
new_node -> next = NULL;
}
printf("Enter the data
scanf("%d", &num);
}
return start;
: ");

}
struct node *display(struct node *start){
struct node *ptr;
ptr = start;
while(ptr != NULL){
printf("\t %d", ptr->data);
ptr = ptr -> next;
}
return start;
}
struct node *insert_at_beginning(struct node *start,int num){
struct node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = start;
start -> prev = new_node;
new_node -> prev = NULL;
start = new_node;
return start;
}
struct node *insert_at_end(struct node *start,int num){
struct node *new_node, *ptr;
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> next != NULL){
ptr = ptr -> next;
}
ptr -> next = new_node;
new_node -> prev = ptr;
new_node -> next = NULL;
return start;
}
struct node *insert_after_node(struct node *start,int num,int val){
struct node *new_node, *ptr;
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> data != val){
ptr = ptr -> next;
}
new_node -> next = ptr -> next;
new_node -> prev = ptr;
ptr -> next -> prev = new_node;

ptr -> next = new_node;


return start;
}
struct node *insert_before_node(struct node *start,int num,int val){
struct node *new_node, *ptr;
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> data != val){
ptr = ptr -> next;
}
new_node -> next = ptr;
new_node -> prev = ptr -> prev;
ptr -> prev -> next = new_node;
ptr -> prev = new_node;
return start;
}

Output:
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
for creating a linked list
for inserting at Beginning a linked list
for inserting at End a linked list
for inserting a node After a specific node in the linked list
for inserting a node before a specific node in the linked list
for displaying a linked list
your choice: 1
-1 to end
data :
data
data
data :
data : -1
the
the
aaats
ba ata
bata)
Linked List is created.
Enter
Salaaw
Enter
Enter
Enter
Enter
Enter
your choice: 2
for creating a linked list
for inserting at Beginning a linked list
for inserting at End a linked list
for inserting a node After a specific node in the linked list
for inserting a node before a specific node in the linked list
for displaying a linked list
Inserted at the beginning:
Enter the data to be inserted at the beginning: 5
Enter
Enter
Enter
Enter
Enter
Enter
Enter
our choice: 3
for creating a linked list
for inserting at Beginning a linked list
for inserting at End a linked list
for inserting a node After a specific node in the linked list
for inserting a node before a specific node in the linked list
for displaying a linked list
Enter the data to be inserted at the end: 69
Enter
Enter
Enter
Enter
Enter
Enter 6
for
for
for
for
for
for
creating a linked list
inserting at Beginning a linked list
inserting at End a linked list
inserting a node After a specific node in the linked list
inserting a node before a specific node in the linked list
displaying a linked list
Enter your choice: 4
Enter the data to be inserted:
98
Enter the node after which the data has to be inserted: 9
Enter
Enter
Enter
Enter
Enter
Enter
for
for
cela
for
for
for
Enter your choice: 5
Enter the data to be inserted:
creating a linked list
inserting at Beginning a linked list
inserting at End a linked list
inserting a node After a specific node in the linked list
inserting a node before a specific node in the linked list
displaying a linked list
99
Enter the node before which the data has to be inserted: 9
Enter
Enter
Enter
Enter
Enter
Enter
for
for
for
for
for
ela
creating a linked list
inserting at Beginning a linked list
inserting at End a linked list
inserting a node After a
Enter your choice: 6
=
inserting a node before a specific node in the linked list
displaying a linked list
specific node in the linked list
8
Lo)
£2
Problem 3: Write a menu driven program in C to delete the first, last and the given
element from the
doubly linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *start = NULL;
struct node *create_LL(struct node*);
struct node *display(struct node *);
struct node *delete_beg(struct node *);
struct node *delete_end(struct node *);
struct node *delete_mid(struct node *);
int main()
{
int choice;
do
{
printf("\n Input 1 for creating a linked list\n");
printf("\n Input 2 for displaying a linked list\n");
printf("\n Input 3 for deleting the first node from a linked list\n");
printf("\n Input 4 for deleting the last node from a linked list\n");
printf("\n Input 5 for deleting any node from a linked list\n");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: start = create_LL(start);
printf("\n Linked List is created.\n");
break;
case 2: start = display(start);
break;
case 3: start = delete_beg(start);
break;
case 4: start = delete_end(start);
break;
case 5: start = delete_mid(start);
break;
}
} while(choice != 6);
return 0;

}
struct node *create_LL(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end \n");
printf("\n Enter the data: ");
scanf("%d", &num);
while(num != -1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
if(start == NULL)
{
new_node -> prev = NULL;
new_node -> next = NULL;
start = new_node;
}
else
{
ptr = start;
while(ptr->next != NULL)
ptr = ptr->next;
ptr -> next = new_node;
new_node -> prev = ptr;
new_node -> next = NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf("\t %d", ptr->data);
ptr = ptr -> next;
}
return start;
}
struct node *delete_beg(struct node *start)
{
struct node *ptr;
ptr = start;
start = start->next;

start -> prev = NULL;


free(ptr);
return start;
}
struct node *delete_end(struct node *start)
{
struct node *ptr, *preptr;
ptr = start;
while(ptr -> next != NULL)
{
//preptr = ptr;
ptr = ptr->next;
}
ptr -> prev -> next= NULL;
free(ptr);
return start;
}
struct node *delete_mid(struct node *start)
{
struct node *ptr, *preptr;
int val;
printf("\nEnter the value of the node which has to be deleted: ");
scanf("%d", &val);
ptr = start;
if((ptr -> data == val) && (ptr -> next == NULL))
{
start = delete_beg(start);
return start;
}
else{
while(ptr -> data != val)
{
// preptr = ptr;
ptr = ptr ->next;
}
ptr -> prev -> next = ptr -> next;
ptr -> next -> prev = ptr -> prev;
free(ptr);
return start;
}
}

Output:
Input
1 for creating a linked list
sigje}eie
Input
Input
Input
Sago
Sagas
Sagas
Sagas
Sagas
gos
Saco
algae
Enter
2 for displaying a linked list
3 for deleting the first node from a linked list
4 for deleting the last node from a linked list
S
for deleting any node from a linked list
your choice: 1
-1 to end
the data:
the data :
the data :
the data :
the data :
the data :
the data : -1
Linked List is created.

Big)s)0ne
Bis)s)ene
Input
for creating a linked list
for displaying a linked list
for deleting the first node from a linked list
Input 4 for deleting the last node from a linked list
Input 5 for deleting any node from a linked list
Enter your choice: 3
Input 1
for creating a linked list
Input 2 for displaying a linked list
Input 3 for deleting the first node from a linked list
Input 4
for deleting the last node from a linked list
Input 5 for deleting any node from a linked list
Enter your choice: 4
Input 1 for creating a linked list
Input 2 for displaying a linked list
Input 3 for deleting the first node from a linked list
Input 4
for deleting the last node from a linked list
Input 5 for deleting any node from a linked list
Enter your choice: 5
Enter the value of the node which has to be deleted: 9
Input 1 for creating a linked list
Input 2 for displaying a linked list
Input 3
for deleting the first node from a linked list
Input 4 for deleting the last node from a linked list
Input 5
for deleting any node from a linked list
Enter your choice: 2
8
4

You might also like