#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char ssn[6];
char name[20];
char department[20];
char designation[20];
long long salary;
long long phno;
struct node *prev;
struct node *next;
};
struct node* insertatend(int n, struct node *tail);
void display(struct node *head);
struct node* deleteatend(struct node *tail);
struct node* insertatfront(struct node *head);
struct node* deleteatfront(struct node *head);
void main() {
int ch, n;
struct node *head = NULL, *tail = NULL;
while(1) {
printf("1: Create DLL of Employee Nodes\n2: DisplayStatus\n3: InsertAtEnd\n4: DeleteAtEnd\n5: InsertAtFront\n6: DeleteAtFront\n7: Exit\nPlease enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter no of Employees: ");
scanf("%d", &n);
if (n <= 0) {
printf("Number of employees should be positive\n");
break;
}
if (head == NULL) {
head = (struct node*)malloc(sizeof(struct node));
printf("Enter ssn, Name, Department, Designation, Salary, PhoneNo of employee: ");
scanf("%s %s %s %s %lld %lld", head->ssn, head->name, head->department, head->designation, &head->salary, &head->phno);
head->prev = NULL;
head->next = NULL;
tail = head;
tail = insertatend(n - 1, tail);
} else {
tail = insertatend(n, tail);
}
if (tail == NULL)
head = tail;
break;
case 2:
if (head == NULL)
printf("DLL is Empty\n");
else
display(head);
break;
case 3:
if (head == NULL) {
head = (struct node*)malloc(sizeof(struct node));
printf("Enter ssn, Name, Department, Designation, Salary, PhoneNo of employee: ");
scanf("%s %s %s %s %lld %lld", head->ssn, head->name, head->department, head->designation, &head->salary, &head->phno);
head->prev = NULL;
head->next = NULL;
tail = head;
} else {
tail = insertatend(1, tail);
}
break;
case 4:
if (head == NULL) {
printf("DLL is empty\n");
} else {
tail = deleteatend(tail);
if (tail == NULL)
head = tail;
}
break;
case 5:
if (head == NULL) {
head = insertatfront(head);
tail = head;
} else {
head = insertatfront(head);
}
break;
case 6:
if (head == NULL) {
printf("DLL is empty\n");
} else {
head = deleteatfront(head);
if (head == NULL)
tail = head;
}
break;
case 7:
exit(0);
break;
default:
printf("Please Enter valid choice\n");
break;
}
}
}
struct node* insertatend(int n, struct node *tail) {
int i;
struct node *newnode;
for (i = 1; i <= n; i++) {
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter ssn, Name, Department, Designation, Salary, PhoneNo of employee: ");
scanf("%s %s %s %s %lld %lld", newnode->ssn, newnode->name, newnode->department, newnode->designation, &newnode->salary, &newnode->phno);
newnode->next = NULL;
newnode->prev = tail;
tail->next = newnode;
tail = newnode;
}
return tail;
}
void display(struct node *head) {
int len = 0;
struct node *temp;
temp = head;
while (temp != NULL) {
printf("SSN:%s| Name:%s| Department:%s| Designation:%s| Salary:%lld| Phone no:%lld\n", temp->ssn, temp->name, temp->department, temp->designation, temp->salary, temp->phno);
len++;
temp = temp->next;
}
printf("No of employees: %d\n", len);
}
struct node* deleteatend(struct node *tail) {
struct node *temp;
if (tail == NULL) {
printf("DLL is empty\n");
return NULL;
}
temp = tail;
if (tail->prev != NULL) {
tail = tail->prev;
tail->next = NULL;
} else {
tail = NULL;
}
printf("employee with ssn: %s is deleted\n", temp->ssn);
free(temp);
return tail;
}
struct node* insertatfront(struct node *head) {
struct node *newnode;
newnode = (struct node*)malloc(sizeof(struct node));
printf("Enter ssn, Name, Department, Designation, Salary, PhoneNo of employee: ");
scanf("%s %s %s %s %lld %lld", newnode->ssn, newnode->name, newnode->department, newnode->designation, &newnode->salary, &newnode->phno);
if (head == NULL) {
head = newnode;
head->next = NULL;
head->prev = NULL;
} else {
newnode->next = head;
newnode->prev = NULL;
head->prev = newnode;
head = newnode;
}
return head;
}
struct node* deleteatfront(struct node *head) {
struct node *temp;
if (head == NULL) {
printf("DLL is empty\n");
return NULL;
}
temp = head;
head = head->next;
if (head != NULL)
head->prev = NULL;
printf("employee with ssn: %s is deleted\n", temp->ssn);
free(temp);
return head;
}