You are on page 1of 4

Program - 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
#include <stdio.h>
#include <stdlib.h>

struct node
{
char usn[20];
char name[50];
char branch[25];
int sem;
char phno[13];
struct node *link;
};
typedef struct node *NODE;

/*Macro*/

#define MALLOC(p, n, type) \


p = (type *)malloc(n * sizeof(type)); \
if (p == NULL) \
{ \
printf("insufficient Memory\n"); \
exit(0); \
}

NODE insert_front(NODE first)


{
NODE temp;

MALLOC(temp, 1, struct node);

printf("\n Enter the Name:");


scanf("%s", temp->name);

printf("\n Enter the USN:");


scanf("%s", temp->usn);
printf("\n Enter the Semester:");
scanf("%d", &temp->sem);

printf("\n Enter the Branch:");


scanf("%s", temp->branch);

printf("\n Enter the Contact Number:");


scanf("%s", temp->phno);

temp->link = first;
return temp;
}

NODE insert_rear(NODE first)


{
NODE temp, cur;
MALLOC(temp, 1, struct node);

printf("\n Enter the Name:");


scanf("%s", temp->name);

printf("\n Enter the USN:");


scanf("%s", temp->usn);

printf("\n Enter the Semester:");


scanf("%d", &temp->sem);

printf("\n Enter the Branch:");


scanf("%s", temp->branch);

printf("\n Enter the Contact Number:");


scanf("%s", temp->phno);
temp->link = NULL;

if (first == NULL)
return temp;

cur = first;
while (cur->link != NULL)
cur = cur->link;
cur->link = temp;

return first;
}
void display(NODE first)
{
NODE temp;
if (first == NULL)
{
printf("****List is empty****\n");
return;
}
printf("\n The contents of the list are\n");
printf("|----Name----|----USN----|--Branch--|-Sem-|---Phone NO---
|\n");
temp = first;
while (temp != NULL)
{
printf("%s\t\t%s\t%s\t%d\t%s\n", temp->name, temp->usn, temp-
>branch, temp->sem, temp->phno);
temp = temp->link;
}
}

NODE delete_front(NODE first)


{
NODE temp;
if (first == NULL)
{
printf("\n ***List is Empty***");
return first;
}
temp = first;
temp = temp->link;
printf("\n***Record Deleted***");
free(first);
return temp;
}

NODE delete_rear(NODE first)


{
NODE cur, prev;

if (first == NULL)
{
printf("\n***List is Empty***");
return first;
}
if (first->link == NULL)
{
printf("\n***Record Deleted***");
free(first);
return NULL;
}
prev = NULL;
cur = first;
while (cur->link != NULL)
{
prev = cur;
cur = cur->link;
}

printf("\n***Record deleted***\n");
free(cur);
prev->link = NULL;
return first;
}
void main()
{
NODE first = NULL;
int ch, item, i;
//clrscr();
for (;;)
{
printf("-------MAIN MENU--------\n");
printf("1: Insert front \n2: Insert Rear\n");
printf("3: Delete front \n4: Delete Rear\n");
printf("5: Display \n6: exit \n");
printf("------------------------\n");
printf("Enter your choice--->");
scanf("%d", &ch);

switch (ch)
{
case 1: first = insert_front(first); break;
case 2: first = insert_rear(first); break;
case 3: first = delete_front(first); break;
case 4: first = delete_rear(first); break;
case 5: display(first); break;
case 6: exit(0);
}
}
}

You might also like