Lab 7
Lab 7
PROGRAM-7
Vandana U
Asst. Professor, Dept of AI-DS
7. Design, Develop and Implement a menu driven Program in C
for the following operations on Singly Linked List (SLL) of
Student Data with the fields: USN, Name, Branch, Sem, PhNo.
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of SLL
d. Perform Insertion and Deletion at Front of SLL
e. Demonstrate how this SLL can be used as STACK and QUEUE
f. Exit
About the Experiment:
Linked List is a linear data structure and it is very common data
structure which consists of group of nodes in a sequence which
is divided in two parts. Each node consists of its own data and
the address of the next node and forms a chain. Linked Lists are
used to create trees and graphs.
❖They are a dynamic in nature which allocates the memory
when required.
❖Insertion and deletion operations can be easily implemented.
❖Stacks and queues can be easily executed.Linked List reduces
the access time.
❖Linked lists are used to implement stacks, queues, graphs,
etc.
❖Linked lists let you insert elements at the beginning and end
of the list. In Linked Lists we don t need to know the size in
advance.
Types of Linked List:
1. Singly Linked List: Singly linked lists contain nodes which
have a data part as well as an address part i.e. next, which
points to the next node in sequence of nodes. The operations
we can perform on singly linked lists are insertion, deletion
and traversal.
Types of Linked List:
2. Circular Linked List: In the circular linked list the last node of
the list contains the address of the first node and forms a
circular chain.
Types of Linked List:
3. 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.
Algorithm:
Step 1: Start.
Step 2: Read the value of N. (N student s information) ‟
Step 3: Create a singly linked list. (SLL)
Step 4: Display the status of SLL.
Step 5: Count the number of nodes.
Step 6: Perform insertion at front of list.
Step 7: Perform insertion at end of the list.
Step 8: Perform deletion at the front of the list.
Step 9: Perform deletion at the end of the list.
Step 10: Demonstrate how singly linked list can be used as stack.
Step 11: Demonstrate how singly linked list can be used as queue.
Step 12: Stop.
#include<stdio.h> Defines a structure named node that contains fields for storing
information about a student.
#include<stdlib.h>
struct node
{ Fields include usn (student's USN), name (student's
char usn[20],name[10],branch[5]; name), branch (student's branch), phno (student's
unsigned long long int phno; phone number), sem (student's semester), and a self-
int sem; referential pointer link to the next node in the linked
struct node *link; list.
};
typedef struct node *NODE;
Uses typedef to create an alias NODE for the pointer to the
structure struct node.
NODE temp,FIRST=NULL;
Declares global variables temp (a temporary node pointer) and
NODE getnode() FIRST (a pointer to the first node in the linked list). FIRST is
{ initially set to NULL.
NODE x;
x=(NODE)malloc(sizeof(struct node));
x->link=NULL; Defines a function named getnode that allocates memory for a new
return x; node using malloc and returns a pointer to the allocated node.
} Sets the link field of the node to NULL.
void read()
The read function you provided is designed to read user input
{ and populate the fields of a node with the entered information.
temp=getnode();
Calls the getnode function to allocate memory for a new node
printf("Enter USN"); and assigns the pointer to ‘temp’.
scanf("%s",temp->usn);
printf("Enter the name"); ‘printf("Enter USN: "); scanf("%s", temp->usn);
scanf("%s",temp->name); Prompts the user to enter the USN (University Serial Number)
and reads it into the usn field of the node.
printf("Enter Branch");
Likewise, even for other fields inputs can be done.
scanf("%s",temp->branch);
printf("Enter the phone number");
scanf("%llu",&temp->phno);
printf("Enter the semester");
scanf("%d",&temp->sem);
}
The create_SSL function appears to be designed for creating a singly
linked list (SSL) of student records. It prompts the user to enter the
void create_SSL() number of students and then iteratively collects details for each student
{ using the read function.
int n,i;
printf("Enter the number of students:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the details of %d students:\n",i);
read(i); if (FIRST == NULL) FIRST = temp;
if(FIRST==NULL)
If the linked list is empty, sets FIRST to the newly created temp node.
FIRST=temp;
else
{
If the linked list is not empty, sets the link field of temp to the current
temp->link=FIRST;
FIRST node and updates FIRST to point to temp.
FIRST=temp;
}}}
void display_count() The display_count function is designed to display the details of students in a
{ linked list along with counting the number of students.
int count=1;
temp=FIRST;
printf("Student details:\n"); Checks if the linked list is empty. If so, prints a message indicating that
if(FIRST==NULL) the student detail is NULL, and the count is 0.
printf("Student detail is NULL and the count is 0\n");
else
{
("\n USN \t NAME \t BRANCH \t PHNO \t SEMESTER\n");
while(temp->link!=NULL) Iterates through the linked list and prints the details of each student.
{
count++; Increments the counter (count) for each student.
printf("%s\t%s\t%s\t%llu\t%d\n",temp->usn,temp->name,temp`->branch,
temp->phno,temp->sem);
temp=temp->link;
}
printf("%s\t%s\t%s\t%llu\t%d\n",temp->usn,temp->name,temp->branch,
temp->phno,temp->sem); After the loop, prints the details of the last student
printf("\nStudent count is %d\n",count); (outside the loop since the loop condition checks for
} temp->link != NULL).
return; } Prints the total count of students.
The insert_front function is designed to insert a new
void insert_front() student node at the front (beginning) of the linked list
{
printf("Enter the details of the student\n");
Calls the read function to read the details and populate the temp node with the entered information
read();
if(FIRST==NULL) If the linked list is empty, sets FIRST to the newly created temp node.
FIRST=temp;
If the linked list is not empty, sets the link field of temp to the current
else FIRST node and updates FIRST to point to temp.
{
temp->link=FIRST;
FIRST=temp;
}}
The insert_end function is designed to insert a new student node at the
NODE insert_end() end of the linked list
{ Declares a pointer last and initializes it with the address of the first node
NODE last=FIRST; (FIRST).
printf("Enter the details of the student:\n");
read();
if(FIRST==NULL) If the linked list is empty, sets FIRST to the newly created temp node.
FIRST=temp;
else
{ If the linked list is not empty, it iterates through the list until
while(last->link!=NULL) the last node is found, and then links the new node temp to
last=last->link;
the link field of the last node.
last->link=temp;
}
return FIRST;
}
The delete_front function is designed to delete the first node from
void delete_front() the linked list.
{
temp=FIRST; Sets the temporary pointer temp to point to the first node in the linked list.
if(FIRST==NULL)
printf("List is empty\n");
else
{
printf("Deleted item is %s\n",temp->usn);
FIRST=FIRST->link;
free(temp); Moves the FIRST pointer to the next node, effectively skipping the first node.
} Frees the memory occupied by the first node.
return;
}
This function deletes the first node from the linked list, and if the list is initially empty, it
prints a message indicating that the list is empty. Note that it assumes the existence of
the free function to release the memory of the deleted node.
void delete_end() The delete_end function is designed to delete the last node from the linked list.
{
NODE last=NULL; Initializes a pointer last to NULL. It will be used to keep track of the second-to-last
temp=FIRST; node.
if(FIRST==NULL) Sets the temporary pointer temp to point to the first node in the linked list.
printf("List is empty\n");
else if(FIRST->link==NULL) Checks if there is only one node in the list. If so, prints the
{ details of the node, frees the memory, and sets FIRST to
printf("Deleted item is %s",temp->usn); NULL
free(FIRST);
FIRST=NULL;
}
else
{ Iterates through the list until temp points to the last node.
while(temp->link!=NULL)
{
last=temp;
Sets the link field of the second-to-last node (last) to NULL, effectively
temp=temp->link;
detaching the last node.
}
last->link=NULL;
printf("Printed element is %s\n",temp->usn);
free(temp);
} return; }
void main()
{
int choice;
while(1)
{
printf("\n1.Create SSL \n2.Display SSL \n3.Insert front \n4.Insert end\n5.Delete front \n6.Delete End
\n7.Exit\n");
printf("Enter your choice");
scanf("%d",&choice);
switch(choice) The code you provided is a simple menu-driven program for
{
case 1: create_SSL();break; performing operations on a singly linked list (SSL) of student records.
case 2: display_count(); The program provides options for creating the list, displaying the list
break;
case 3: insert_front(); break; along with the count of students, inserting a new student at the front,
case 4: insert_end(); inserting a new student at the end, deleting the first student, deleting
break;
case 5:delete_front(); the last student, and exiting the program.
break;
case 6:delete_end(); break;
case 7:return;
default: printf("\nInvalid choice");
}}}
Output:
Number of Students : 3
Input valid information for those 3 students as per your choice.
Choose option:2 and check if the details are getting displayed
correctly.
Choose option : 5 and check if the first element is deleted from
the list.
Choose option : 3 . Insert the elements
Choose option : 2 , Display
Choose option : 7 and exit
Thank you