0% found this document useful (0 votes)
18 views3 pages

Blank

c code 2

Uploaded by

dd7devdilip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Blank

c code 2

Uploaded by

dd7devdilip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

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");
}
}
}

You might also like