DLL 8
DLL 8
h>
#include<stdlib.h>
struct node
{
char ssn[25], name[25], dept[25], desig[25];
int salary;
long int phone;
struct node *rlink; // Right link (next node)
struct node *llink; // Left link (previous node)
};
typedef struct node *NODE;
NODE getnode()
{
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("\nMemory is not available");
exit(1);
}
printf("Enter SSN, Name, Dept, Desig, Salary, Phone of the Employee:\n");
scanf("%s %s %s %s %d %ld", temp->ssn, temp->name, temp->dept, temp->desig,
&temp->salary, &temp->phone);
temp->rlink = NULL; // Initialize right link as NULL
temp->llink = NULL; // Initialize left link as NULL
count++;
return temp;
}
NODE insertfront()
{
NODE temp;
temp = getnode();
if (first == NULL)
{
return temp;
}
temp->rlink = first;
first->llink = temp; // Set the left link of the old first node to the new node
return temp;
}
NODE deletefront()
{
NODE cur;
if (first == NULL)
{
printf("\nLinked list is empty");
return NULL;
}
if (first->rlink == NULL)
{
printf("\nThe Employee node with SSN:%s is deleted ", first->ssn);
count--;
free(first);
return NULL;
}
cur = first;
first = first->rlink;
first->llink = NULL; // Set the left link of the new first node to NULL
printf("\nThe Employee node with SSN:%s is deleted", cur->ssn);
count--;
free(cur);
return first;
}
NODE insert_rear()
{
NODE cur, temp;
temp = getnode();
if (first == NULL)
{
return temp;
}
cur = first;
while (cur->rlink != NULL)
{
cur = cur->rlink;
}
cur->rlink = temp;
temp->llink = cur; // Set the left link of the new node to the current last node
return first;
}
NODE delete_rear()
{
NODE cur, prev;
if (first == NULL)
{
printf("\nLinked List is empty");
return NULL;
}
if (first->rlink == NULL)
{
printf("\nThe employee node with SSN:%s is deleted", first->ssn);
free(first);
count--;
return NULL;
}
prev = NULL;
cur = first;
while (cur->rlink != NULL)
{
prev = cur;
cur = cur->rlink;
}
prev->rlink = NULL; // Set the right link of the previous node to NULL
printf("\nThe employee node with SSN:%s is deleted", cur->ssn);
free(cur);
count--;
return first;
}
void display()
{
NODE cur;
int num = 1;
if (first == NULL)
{
printf("\nNo Contents to display in DLL \n");
return;
}
printf("\nThe contents of DLL: \n");
cur = first;
while (cur != NULL)
{
printf("\n|%d| |SSN:%s| |Name:%s| |Dept:%s| |Desig:%s| |Salary:%d| |Ph:%ld|", num,
cur->ssn, cur->name, cur->dept, cur->desig, cur->salary, cur->phone);
cur = cur->rlink;
num++;
}
printf("\n No of employee nodes is %d \n", count);
}
int main()
{
int ch, i, n;
while (1)
{
printf("\n--------Menu--------");
printf("\nEnter your choice for DLL operation \n");
printf("\n1:Create DLL of Employee Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtFront");
printf("\n4:InsertAtRear");
printf("\n5:DeleteAtFront");
printf("\n6:DeleteAtRear");
printf("\n7:Exit \n");
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter the number of employees: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
first = insertfront();
break;
case 2:
display();
break;
case 3:
first = insertfront();
break;
case 4:
first = insert_rear();
break;
case 5:
first = deletefront();
break;
case 6:
first = delete_rear();
break;
case 7:
exit(0);
default:
printf("\nPlease enter a valid choice");
}
}
}