0% found this document useful (0 votes)
19 views

Lab 8

Uploaded by

vandana u
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lab 8

Uploaded by

vandana u
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

LAB

PROGRAM-8
Vandana U
Assistant Professor
Dept. of AI-DS
Program 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
ABOUT THE EXPERIMENT: Doubly Linked List: In a doubly linked
list, each node contains two links the first link points to the previous
node and the next link points to the next node in the sequence. In
computer science, a doubly linked list is a linked data structure that
consists of a set of sequentially linked records called nodes. Each
node contains two fields, called links, that are references to the
previous and to the next node in the sequence of nodes.
The beginning and ending nodes' previous and next links,
respectively, point to some kind of terminator, typically a sentinel
node or null, to facilitate traversal of the list. If there is only one
sentinel node, then the list is circularly linked via the sentinel node.
The two node links allow traversal of the list in either
direction. While adding or removing a node in a doubly
linked list requires changing more links than the same
operations on a singly linked list, the operations are
simpler and potentially more efficient (for nodes other
than first nodes) because there is no need to keep track
of the previous node during traversal or no need to
traverse the list to find the previous node, so that its link
can be modified.
ALGORITHM:
Step 1: Start.
Step 2: Read the value of N. (N student s information) ‟
Step 3: Create a doubly linked list. (DLL)
Step 4: Display the status of DLL.
Step 5: Count the number of nodes.
Step 6: Perform insertion at front of list.
Step 7: Perform deletion at the front of the list.
Step 8: Perform insertion at end of the list.
Step 9: Perform deletion at the end of the list.
Step 10: Demonstrate how doubly linked list can be used as dou
queue.
#include<stdio.h> This structure is suitable for creating a
struct node // structure to store employee linked list of employee records, where
each node in the list contains
details
information about an employee and
{ pointers to the previous and next
char nodes in the list.
ssn[12],name[20],dept[25],desig[20];
unsigned long long int phno;
float sal; The getnode function you provided is a
struct node *prev; common function used to dynamically
struct node *next; allocate memory for a new node in a
};
doubly linked list. It allocates memory for
typedef struct node *NODE;
NODE temp, FIRST=NULL,END=NULL; a struct node, initializes the prev and
next pointers to NULL, and returns the
NODE getnode() // Create node
created node.
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
x->prev=NULL;
x->next=NULL;
void read() // read details of employee
{
The read function you provided is designed to read
temp=getnode(); details of an employee and populate the
corresponding fields of a node in a linked list. It
printf("Enter SSN:"); appears to be part of a program managing
scanf("%s",temp->ssn); employee information using a linked list.

printf("Enter Name:");
scanf("%s",temp->name); Assumes that getnode() is a function that allocates
printf("Enter Dept:"); memory for a new node and returns a pointer to it.

scanf("%s",temp->dept);
printf("Enter Designation:");
scanf("%s",temp->desig);
printf("Enter Phno:");
scanf("%llu",&temp->phno);
printf("Enter Salary:");
scanf("%f",&temp->sal);
return; }
void Create_DLL() The Create_DLL function you provided is designed to create a doubly linked list (DLL) of N
{ employees' data by using end insertion. It prompts the user to enter details for each employee, reads
the details using the read function, and inserts a new node at the end of the DLL.
int n,i=1;
printf("Enter the number of Employees \n");
scanf("%d",&n); A while loop is used to iterate through each employee (i) and prompt the user to enter details
while(i<=n) using the read function.
{
printf("Enter the details of the %d employee\n", i++);
read();
If the list is empty
if(FIRST==NULL) // if empty list new node will be the first node
(FIRST == NULL),
the new node {
(temp) becomes FIRST=temp;
both the first and END=temp;
last node. }
else //otherwise find the last node and insert the new node
{
END->next=temp; If the list is not empty, the new node is inserted at the end of
the DLL by updating the next and prev pointers accordingly.
temp->prev=END;
END=temp;
}}}
void display_count() The display_count function you provided is designed to display the details of
{ employees in a doubly linked list (DLL) and count the number of employees in the list.
temp=FIRST;
int count=0; If the list is not empty, it enters the else block and
displays the details of each employee in the DLL.
if(FIRST==NULL) // check for empty list It increments the count variable for each employee.
printf("the Employee detail is NULL and count is %d\n", count);
else
{
printf("Employee details:\n");
printf("SSN \t EMPLOYEE NAME \t DEPARTMENT \t DESIGNATION \t PHONE NUMBER \t SALARY");
while(temp!=NULL)
{
count++;
printf("\n%s\t%s\t%s\t%s\t%llu\t%0.2f",temp->ssn, temp->name, temp->dept,temp->desig,temp->phno,temp->sal);
temp=temp->next;
}
printf("\n Employee count is %d\n",count);
}
return;
}
void Insertionfront() The Insertionfront function you provided is designed to insert
a new employee node at the front of a doubly linked list
{
(DLL). This function assumes that the employee details are
printf("Enter the details of the employee\n"); entered using the read function.
read();
if(FIRST==NULL) If the list is empty (FIRST == NULL), the new node
{ (temp) becomes both the first and last node in the DLL.
FIRST=temp;
Links the new node to the current first node.
END=temp;
}
else
{
temp->next=FIRST; Updates the prev pointer of the current first node to point to the new node.
FIRST->prev=temp;
FIRST=temp; Updates the FIRST pointer to point to the new node, making it the new first
} node.
}
void Insertionend() The Insertionend function you provided is designed to insert a new
employee node at the end of a doubly linked list (DLL).
{
printf("Enter the details of the new employee\n");
read();
if(FIRST==NULL) If the list is empty (FIRST == NULL), the new node
{ (temp) becomes both the first and last node in the DLL.
FIRST=temp;
END=temp; Links the current last node to the new node.
}
else
{
END->next=temp;
temp->prev=END;
END=temp; Updates the END pointer to point to the new node, making it the new last node.
}
return ;
}
.void Deletionfront() The Deletionfront function you provided is designed to delete the first
{ employee node from a doubly linked list (DLL).
temp=FIRST;
if(FIRST==NULL) Check for Empty List:
printf("List is empty\n"); if (FIRST == NULL): Checks if the doubly linked list is empty. If
else if(FIRST==END) it is, it prints a message indicating that the list is empty.
{ If the list is not empty:If there is only one node in the
printf("deleted element is %s\n", temp->ssn); list (FIRST == END), it deletes the node and sets
FIRST=NULL; both FIRST and END to NULL.
END=NULL;
free(temp);
}
else
If there are more than one node, it deletes the first
{ node and updates FIRST to point to the next node.
printf("deleted element is %s\n", temp->ssn); The prev pointer of the new first node is set to NULL.
FIRST =FIRST->next;
FIRST->prev=NULL;
free(temp); The free function is used to deallocate the
memory occupied by the deleted node.
}
return;
}
The Deletionend function you provided is designed to delete
void Deletionend()
the last employee node from a doubly linked list (DLL).
{
temp = END;
if(FIRST==NULL) Check for Empty List:
printf("List is empty\n"); if (FIRST == NULL): Checks if the doubly linked list is empty. If it is,
else if(FIRST==END) it prints a message indicating that the list is empty.
{
printf("deleted element is %s\n", temp->ssn); If the list is not empty:If there is only one node in the
FIRST=NULL; list (FIRST == END), it deletes the node and sets
END=NULL; both FIRST and END to NULL.
free(temp);
}
else If there are more than one node, it deletes the last
{ node and updates END to point to the previous
printf("deleted element is %s\n", temp->ssn); node. The next pointer of the new last node is set to
END=END->prev; NULL.
END->next=NULL;
free(temp);
} The free function is used to deallocate the memory occupied by the deleted
return ; node.
}
void main()
{
int choice; The main function you provided seems to be a menu-driven program for
while(1) managing a doubly linked list (DLL) of employee records.
{
printf(" \n 1 - Create DLL of N Employees \n 2 - Display DLL \n 3 - Insertion at front \n 4 - Insertion at end");
printf("\n 5 - Deletion at front \n 6 - Deletion at end \n 7 - Exit\n");
printf("Enter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1 : Create_DLL();
break;
case 2 : display_count();
break;
case 3 : Insertionfront();
break;
case 4 : Insertionend();
break;
case 5 : Deletionfront();
break;
case 6 : Deletionend();
break;
case 7 : return;
default: printf("Invalid Choice\n"); } } }
OUTPUT
Run the code:
Choose option 1:Number of employees=3 ,
keep the college in mind... and enter data accordingly.
Once data is entered, enter option : 2 to display the contents
then choose option 3: insert the element
Choose option 5
Choose option 6
Choose option 2
Choose option 7
THANK YOU

You might also like