Blank
Blank
h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
} * rst = NULL, *last = NULL, *temp, *ptr, *tt;
void create() {
int val;
printf("Enter the value: ");
scanf("%d", &val);
temp = (struct node *)malloc(sizeof(struct node));
temp->next = NULL;
temp->prev = NULL;
temp->data = val;
}
void insertatbeg() {
create();
if ( rst == NULL) {
rst = last = temp;
} else {
temp->next = rst;
rst->prev = temp;
rst = temp;
}
}
void delete() {
if ( rst == NULL) {
printf("The list is empty.\n");
return;
}
int node;
printf("Enter the node value to delete: ");
scanf("%d", &node);
temp = rst;
while (temp != NULL && temp->data != node) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with value %d not found.\n", node);
return;
}
if (temp == rst) {
printf("Deleted node is %d\n", temp->data);
if ( rst == last) {
rst = last = NULL;
} else {
rst = rst->next;
rst->prev = NULL;
}
}
else if (temp == last) {
printf("Deleted node is %d\n", temp->data);
if ( rst == last) {
rst = last = NULL;
} else {
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
last = last->prev;
last->next = NULL;
}
} else {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
printf("Deleted node is %d\n", temp->data);
free(temp);
}
}
void search(){
int sss;
temp= rst;
printf("enter the element to be searched:");
scanf("%d",&sss);
if( rst==NULL){
printf("list is empty\n");
}
else{
while(temp!=NULL && temp->data!=sss){
temp=temp->next;
}
if(temp== NULL){
printf("the element %d not found in the list\n",sss);
return;
}
printf("the element %dis in the list",sss );
}
}
void display() {
if ( rst == NULL) {
printf("The list is empty.\n");
return;
}
printf("Linked list elements: ");
temp = rst;
while (temp != NULL) {
printf("%d \t", temp->data);
temp = temp->next;
}
}
int main(){
int choice;
while(1){
printf("enter 1 for add at front\n");
printf("enter 2 to delete speci ed node\n");
printf("enter 3 to search a speci ed node\n");
printf("enter 4 to display\n");
printf("enter 5 to exit\n");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice){
case 1:
insertatbeg();
break;
case 2:
delete();
break;
fi
fi
fi
fi
fi
fi
case 3:
search();
break;
case 4:
display();
break;
case 5:
printf("exiting.....\n");
exit(0);
default:
printf("enter correct choice\n");
}
}
}