#include <stdio.
h>
#include <stdlib.h>
#include <string.h>
int count = 0; *h=head,
*temp=tempcell,
struct node { *temp1=lastnode,
*temp2=traversal,
struct node *prev;
int ssn, phno;
float sal;
char name[20], dept[10], desg[20];
struct node *next;
} *h, *temp, *temp1, *temp2;
void create() {
int ssn, phno;
float sal;
char name[20], dept[10], desg[20];
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL) { Insert : &ssn, name, dept,
printf("Out of memory\n"); desg, &sal, &phno
exit(1);
}
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter ssn, name, department, designation, salary, and phno of
employee: ");
scanf("%d %s %s %s %f %d", &ssn, name, dept, desg, &sal, &phno);
temp->ssn = ssn;
strcpy(temp->name, name);
strcpy(temp->dept, dept);
strcpy(temp->desg, desg);
temp->sal = sal;
temp->phno = phno;
count++;
}
void insertbeg() {
1.empty
if (h == NULL) {
2.
create();
h
h = temp;
temp1 = h; temp
h
} else {
create();
temp->next = h; // The new node points to the current head
temp->prev = NULL; // The new node's previous pointer is NULL
h->prev = temp; // The current head's previous pointer points to the new
node
h = temp; // The new node becomes the new head of the list
}
}
void insertend() {
if (h == NULL) {
create();
h = temp;
temp1 = h;
} else {
create(); Temp1
temp1->next = temp;
tem
temp->prev = temp1; p
Temp1
temp->next = NULL;
temp1 = temp;
}
}
void displaybeg() {
temp2 = h;
if (temp2 == NULL) {
printf("List is empty\n");
return;
}
printf("\n Linked list elements from beginning:\n");
while (temp2 != NULL) {
printf("%d %s %s %s %f %d\n", temp2->ssn, temp2->name, temp2->dept,
temp2->desg, temp2->sal, temp2->phno);
temp2 = temp2->next;
}
printf("No of employees = %d\n", count);
}
int deleteend() {
if (h == NULL) {
printf("List is empty\n");
return -1; // Indicate an error
}
if (h->next == NULL) {
free(h); Fix temp1 as last.
h = NULL; Temp2 as the last before.
} else {
temp1 = h;
while (temp1->next != NULL) {
temp1 = temp1->next;
}
temp2 = temp1->prev;
if (temp2 != NULL) {
temp2->next = NULL;
}
printf("%d %s %s %s %f %d\n", temp1->ssn, temp1->name, temp1->dept,
temp1->desg, temp1->sal, temp1->phno);
free(temp1);
}
count--;
return 0;
}
int deletebeg() {
if (h == NULL) {
printf("List is empty\n");
return -1; // Indicate an error
}
temp = h;
if (h->next == NULL) {
H and temp are same.
free(h);
Next element is changed as h
h = NULL;
} else {
h = h->next;
h->prev = NULL;
printf("%d %s %s %s %f %d\n", temp->ssn, temp->name, temp->dept,
temp->desg, temp->sal, temp->phno);
free(temp);
}
count--;
return 0;
}
int main() {
int ch, n, i;
h = NULL;
temp = temp1 = NULL;
printf("-----------------MENU--------------------\n");
printf("\n 1 - Create a DLL of n employees");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - Delete at end");
printf("\n 5 - Insert at beginning");
printf("\n 6 - Delete at beginning");
printf("\n 7 - Exit\n");
printf("------------------------------------------\n");
while (1) {
printf("\n Enter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\n Enter number of employees: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
insertend();
}
break;
case 2:
displaybeg();
break;
case 3:
insertend();
break;
case 4:
deleteend();
break;
case 5:
insertbeg();
break;
case 6:
deletebeg();
break;
case 7:
exit(0);
default:
printf("Wrong choice\n");
}
}
return 0;
}
INSERTION AT THE SPECIFIC POINT
P P-
>NEX
T
TMP
TMP->next=P->Next;
P->Next->Prev = TMP;
P->Next=TMP;
TMP->Prev=P;
DELETION AT SPECIFIC POINT
P P-
TMP >NEX
T
P->Next=TMP->Next;
TMP->Next->Prev=P;
Free(TMP)