0% found this document useful (0 votes)
91 views13 pages

Prog - 8 Doubly Linked List

Dsa lab program 8 doubly linked list

Uploaded by

bheemanna171147
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)
91 views13 pages

Prog - 8 Doubly Linked List

Dsa lab program 8 doubly linked list

Uploaded by

bheemanna171147
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/ 13

Develop a menu driven Program in C for the following operations on Doubly Linked

List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal,
PhNo

a) Create a DLL of N Employees Data by using end insertion.


b) Display the status of DLL and count the number of nodes in it
c) Perform Insertion and Deletion at End of DLL
d) Perform Insertion and Deletion at Front of DLL
e) Demonstrate how this DLL can be used as Double Ended Queue.
f) Exit

#include<stdio.h>

#include<stdlib.h>

struct Employee {

int SSN;

char Name[20];

char Dept[10];

char Designation[20];

float Sal;

long int PhNo;

struct Employee *prev;

struct Employee *next;

};

typedef struct Employee NODE;

NODE *head=NULL,*newEmp,*temp,*prvnode,*curr; // declaring of structure


variables

int count=0;

void create_node() {

int ele;
newEmp=(NODE *)malloc(sizeof(NODE)); // Memory allocation of size NODE

if (newEmp == NULL)

printf("Memory allocation failed!\n");

exit(0);

printf("Enter SSN: ");

scanf("%d", &newEmp->SSN);

printf("Enter Name: ");

scanf("%s", newEmp->Name);

printf("Enter Department: ");

scanf("%s", newEmp->Dept);

printf("Enter Designation: ");

scanf("%s", newEmp->Designation);

printf("Enter Salary: ");

scanf("%f", &newEmp->Sal);

printf("Enter Phone Number: ");

scanf("%ld", &newEmp->PhNo);

newEmp->prev = NULL;

newEmp->next = NULL;

void displayCount()

if(head==NULL)

printf("Doubly Linked List is Empty\n");

else
{

temp=head;

printf("\nEmployee Data:\n");

printf("SSN\tName\tDept\tDesignation\tSal\tPhNo\n");

while(temp!=NULL)

printf("%d\t%s\t%s\t%s\t%.2f\t%ld\n", temp->SSN, temp->Name, temp-


>Dept, temp->Designation, temp->Sal, temp->PhNo);

temp=temp->next;

printf("\nNumber of employees: %d\n", count);

void insert_front() {

create_node();

if(head==NULL) {

head=newEmp;

else {

newEmp->next = head;

head->prev=newEmp;

head=newEmp;

count++;

displayCount();

void insert_end() {
create_node();

if(head==NULL) {

head=newEmp;

else {

temp=head;

while(temp->next!=NULL) {

temp=temp->next;

temp->next=newEmp;

newEmp->prev=temp;

count++;

displayCount();

void delete_front() {

if(head == NULL) {

printf("\nDoubly Linked List is EMPTY\n");

else if(head->next == NULL) {

printf("\nDeleted SSN %d:\n ",head->SSN);

free(head);

head = NULL;

else {

temp = head;

printf("\nDeleted SSN Node %d: \n",temp->SSN);

head = head -> next;

head -> prev = NULL;


free(temp);

count--;

displayCount();

void delete_end() {

if(head == NULL) {

printf("\nDoubly Linked List is EMPTY\n");

else if(head->next == NULL) {

printf("\nDeleted SSN Node %d: \n",head->SSN);

head = NULL;

free(head);

else {

temp=head;

while(temp->next!=NULL) {

temp=temp->next;

printf("\nDeleted SSN Node %d: \n",temp->SSN);

temp->prev->next=NULL;

free(temp);

count--;

displayCount();

// Function to demonstrate DLL as a Double Ended Queue

void doubleEndedQueueDemo()
{

int choice;

printf("DLL as Double Ended Queue\n");

do {

printf("\n1. Insert at Front\n");

printf("2. Insert at End\n");

printf("3. Delete from Front\n");

printf("4. Delete from End\n");

printf("5. Display DLL\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

insert_front();

break;

case 2:

insert_end();

break;

case 3:

delete_front();

break;

case 4:

delete_end();

break;

case 5:

displayCount();

break;

case 6:
printf("Exiting Double Ended Queue Demo.\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

} while (choice != 6);

int main() // main function

int ch,n, choice;

do {

printf("\nDoubly Linked List Operations\n");

printf("******************************\n");

printf("1. Create DLL of N Employees\n");

printf("2. Display DLL and Count of Nodes\n");

printf("3. Insert at End\n");

printf("4. Insert at Front\n");

printf("5. Delete from End\n");

printf("6. Delete from Front\n");

printf("7. Demonstrate DLL as Double Ended Queue\n");

printf("8. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Enter the number of employees: ");

scanf("%d", &n);
for (int i = 0; i < n; i++)

insert_end();

break;

case 2:

displayCount();

break;

case 3:

insert_end();

break;

case 4:

insert_front();

break;

case 5:

delete_end();

break;

case 6:

delete_front();

break;

case 7:

doubleEndedQueueDemo();

break;

case 8:

printf("Exiting program.\n");

break;

default:

printf("Enter Valid Choice\n");

}while (choice != 8);


return 0;

Output:

Doubly Linked List Operations

******************************

1. Create DLL of N Employees

2. Display DLL and Count of Nodes

3. Insert at End

4. Insert at Front

5. Delete from End

6. Delete from Front

7. Demonstrate DLL as Double Ended Queue

8. Exit

Enter your choice: 1

Enter the number of employees: 2

Enter SSN: 101

Enter Name: ravi

Enter Department: AIML

Enter Designation: AP

Enter Salary: 23456

Enter Phone Number: 12345

Employee Data:

SSN Name Dept Designation Sal PhNo

101 ravi AIML AP 23456.00 12345

Number of employees: 1

Enter SSN: 102


Enter Name: RAHUL;

Enter Department: AIML

Enter Designation: AP

Enter Salary: 45345

Enter Phone Number: 32123

Employee Data:

SSN Name Dept Designation Sal PhNo

101 ravi AIML AP 23456.00 12345

102 RAHUL AIML AP 45345.00 32123

Number of employees: 2

Enter your choice: 3

Enter SSN: 104

Enter Name: RAMESH

Enter Department: AIML

Enter Designation: AP

Enter Salary: 32321

Enter Phone Number: 34512

Employee Data:

SSN Name Dept Designation Sal PhNo

101 ravi AIML AP 23456.00 12345

102 RAHUL AIML AP 45345.00 32123

104 RAMESH AIML AP 32321.00 34512

Number of employees: 3

Enter your choice: 4


Enter SSN: 100

Enter Name: RAKESH

Enter Department: AIML

Enter Designation: AP

Enter Salary: 32546

Enter Phone Number: 54678

Employee Data:

SSN Name Dept Designation Sal PhNo

100 RAKESH AIML AP 32546.00 54678

101 ravi AIML AP 23456.00 12345

102 RAHUL AIML AP 45345.00 32123

104 RAMESH AIML AP 32321.00 34512

Number of employees: 4

Enter your choice: 5

Deleted SSN Node 104:

Employee Data:

SSN Name Dept Designation Sal PhNo

100 RAKESH AIML AP 32546.00 54678

101 ravi AIML AP 23456.00 12345

102 RAHUL AIML AP 45345.00 32123

Number of employees: 3

Enter your choice: 6

Deleted SSN Node 100:


Employee Data:

SSN Name Dept Designation Sal PhNo

101 ravi AIML AP 23456.00 12345

102 RAHUL AIML AP 45345.00 32123

Number of employees: 2

Enter your choice: 7

DLL as Double Ended Queue

1. Insert at Front

2. Insert at End

3. Delete from Front

4. Delete from End

5. Display DLL

6. Exit

Enter your choice: 1

Enter SSN: 300

Enter Name: AMULYA

Enter Department: AIML

Enter Designation: AP

Enter Salary: 23900

Enter Phone Number: 56543

Employee Data:

SSN Name Dept Designation Sal PhNo

300 AMULYA AIML AP 23900.00 56543

101 ravi AIML AP 23456.00 12345

102 RAHUL AIML AP 45345.00 32123

Number of employees: 3
Enter your choice: 4

Deleted SSN Node 102:

Employee Data:

SSN Name Dept Designation Sal PhNo

300 AMULYA AIML AP 23900.00 56543

101 ravi AIML AP 23456.00 12345

Number of employees: 2

Enter your choice: 6

You might also like