0% found this document useful (0 votes)
6 views6 pages

8 DLL

..............

Uploaded by

introvertb46
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)
6 views6 pages

8 DLL

..............

Uploaded by

introvertb46
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/ 6

Department of Computer Science & Engineering, GMIT, Davangere.

8. 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>
#include<string.h>
struct node
{
int SSN;
char Name[30],Dept[4],Designation[30],PhNo[11];
int Salary;
struct node *plink;
struct node *nlink;
};
typedef struct node* NODEPTR;
NODEPTR GetNode(void);
void FreeNode(NODEPTR);
NODEPTR InsRear(NODEPTR);
NODEPTR DelFront(NODEPTR);
NODEPTR InsFront(NODEPTR);
NODEPTR DelRear(NODEPTR);
void Display(NODEPTR);

DSA Lab (BCSL305) Programs: Prepared by Mr. Shankar Shastri, Assistant Professor.
Department of Computer Science & Engineering, GMIT, Davangere.

int main()
{
NODEPTR first = NULL;
int Choice,Num,i;
printf("\n Enter the number of Employees N : ");
scanf("%d",&Num);
for(i=0;i<Num;i++)
{
printf("\n Enter Data for Node %d :\n", i+1);
first = InsRear(first);
}
for(;;)
{
printf("\n DLL OPERATIONS\n");
printf("====================");
printf("\n 1.Insert Rear\n 2.Delete Front\n 3.Insert
Front\n 4.Delete Rear\n 5.Display\n 6.Exit\n");
printf("\n Enter your choice\n");
scanf("%d",&Choice);
switch(Choice)
{
case 1: first = InsRear(first);
break;
case 2: first = DelFront(first);
break;
case 3: first = InsFront(first);
break;
case 4: first = DelRear(first);
break;
case 5: Display(first);
break;
case 6: exit(0);
}

DSA Lab (BCSL305) Programs: Prepared by Mr. Shankar Shastri, Assistant Professor.
Department of Computer Science & Engineering, GMIT, Davangere.

}
return 0;
}
NODEPTR GetNode()
{
NODEPTR newborn;
newborn = (NODEPTR)malloc(sizeof(struct node));
if(newborn == NULL)
{
printf("\n Memory Overflow");
exit(0);
}
printf("\n Enter SSN : ");
scanf("%d",&newborn->SSN);
printf("\nEnter name : ");
scanf("%s",newborn->Name);
printf("\n Enter Department : ");
scanf("%s", newborn->Dept);
printf("\n Enter Designation : ");
scanf("%s", newborn->Designation);
printf("\n Enter Salary : ");
scanf("%d",&newborn->Salary);
printf("\n Enter Phone no : ");
scanf("%s",newborn->PhNo);
return newborn;
}
void FreeNode(NODEPTR x)
{
free(x);
}

DSA Lab (BCSL305) Programs: Prepared by Mr. Shankar Shastri, Assistant Professor.
Department of Computer Science & Engineering, GMIT, Davangere.

NODEPTR InsRear(NODEPTR first)


{
NODEPTR temp,cur;
temp = GetNode();
temp->plink = temp->nlink = NULL;
if(first == NULL)
return temp;
cur = first;
while(cur->nlink != NULL)
{
cur = cur->nlink;
}
cur->nlink = temp;
temp->plink = cur;
return first;
}
NODEPTR InsFront(NODEPTR first)
{
NODEPTR temp;
temp = GetNode();
temp->plink = temp->nlink = NULL;
temp->nlink = first;
first = temp;
return first;
}

NODEPTR DelRear(NODEPTR first)


{
NODEPTR cur, prev;
if(first == NULL)
{
printf("\n DLL is empty\n");
return first;

DSA Lab (BCSL305) Programs: Prepared by Mr. Shankar Shastri, Assistant Professor.
Department of Computer Science & Engineering, GMIT, Davangere.

}
cur = first;
if(cur->nlink == NULL)
{
printf("\n Node deleted for %s\n",cur->Name);
FreeNode(cur);
return NULL;
}
while(cur->nlink != NULL)
{
cur = cur->nlink;
}
prev = cur->plink;
prev->nlink = NULL;
printf("\n Node deleted for %s\n",cur->Name);
FreeNode(cur);
return first;
}

NODEPTR DelFront(NODEPTR first)


{
NODEPTR temp;
if(first == NULL)
{
printf("\n DLL is empty\n");
return first;
}
if(first->nlink == NULL)
{
printf("\n Node deleted for %s\n",first->Name);
FreeNode(first);
return NULL;
}

DSA Lab (BCSL305) Programs: Prepared by Mr. Shankar Shastri, Assistant Professor.
Department of Computer Science & Engineering, GMIT, Davangere.

temp = first;
first = first->nlink;
first->plink = NULL;
printf("\n Node deleted for %s\n",temp->Name);
FreeNode(temp);
return first;
}

void Display(NODEPTR first)


{
NODEPTR curr;
int count = 0;
if(first == NULL)
{
printf("\n DLL is empty\n");
return;
}
printf("\n The contents of DLL are :\n");
curr = first;
// printf("\n");
printf("\nSSN\tName\tDept\tDesignation\tSalary\t\tPhone
No");
while(curr != NULL)
{
printf("\n%-5d\t%s\t%s\t%s\t\t%-7d\t\t%-11s",curr->SSN,
curr->Name,curr->Dept,curr->Designation,curr->Salary,curr-
>PhNo);
curr = curr->nlink;
count++;
}
printf("\n\n DLL has %d nodes\n", count);
}

DSA Lab (BCSL305) Programs: Prepared by Mr. Shankar Shastri, Assistant Professor.

You might also like