Design and Implement a menu driven Program in C for the operations on Singly Linked List of Student Data with the fields

Program Operations are:
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

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.

ALGORITHM:
Step 1: Start.
Step 2: Read the value of N. (N student’s information)
Step 2: Create a singly linked list. (SLL)
Step 3: Display the status of SLL.
Step 4: Count the number of nodes.
Step 5: Perform insertion at front of list.
Step 6: Perform deletion at the front of the list.
Step 7: Perform insertion at end of the list.
Step 8: Perform deletion at the end of the list.
Step 9: Demonstrate how singly linked list can be used as stack.
Step 10: Demonstrate how singly linked list can be used as queue.
Step 11: Stop

				
					#include<stdio.h>
#include<conio.h>
int MAX=4, count;
struct student
{
char usn[10];
char name[30];
char branch[5];
int sem;
char phno[10];
struct student *next; //Self referential pointer.
};
typedef struct student NODE;
int countnodes(NODE *head)
{
NODE *p;
count=0;

p=head;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}
NODE* getnode(NODE *head)
{
NODE *newnode;
newnode=(NODE*)malloc(sizeof(NODE)); //Create first NODE
printf("\nEnter USN, Name, Branch, Sem, Ph.No\n");
flushall();
gets(newnode->usn);
flushall();
gets(newnode->name);
flushall();
gets(newnode->branch);
scanf("%d",&(newnode->sem));
flushall();
gets(newnode->phno);
newnode->next=NULL; //Set next to NULL...
head=newnode;
return head;
}
NODE* display(NODE *head)
{
NODE *p;
if(head == NULL)
printf("\nNo student data\n");
else
{
p=head;
printf("\n----STUDENT DATA----\n");
printf("\nUSN\tNAME\t\tBRANCH\tSEM\tPh.NO.");
while(p!=NULL)
{
printf("\n%s\t%s\t\t%s\t%d\t%s", p->usn, p->name, p->branch, p->sem, p->phno);
p = p->next; //Go to next node...
}
printf("\nThe no. of nodes in list is: %d",countnodes(head));
}

return head;
}
NODE* create(NODE *head)
{
NODE *newnode;
if(head==NULL)
{
newnode=getnode(head);
head=newnode;
}
else
{
newnode=getnode(head);
newnode->next=head;
head=newnode;
}
return head;
}
NODE* insert_front(NODE *head)
{
if(countnodes(head)==MAX)
printf("\nList is Full / Overflow!!");
else
head=create(head); //create()insert nodes at front.
return head;
}
NODE* insert_rear(NODE *head)
{
NODE *p, *newnode;
p=head;
if(countnodes(head) == MAX)
printf("\nList is Full(QUEUE)!!");
else
{
if(head == NULL)
{
newnode=getnode(head);
head=newnode; //set first node to be head
}
else
{

newnode=getnode(head);
while(p->next!=NULL)
{
p=p->next;
}
p->next=newnode;
}
}
return head;
}
NODE* insert(NODE *head)
{
int ch;
do
{
printf("\n1.Insert at Front(First)\t2.Insert at End(Rear/Last)\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: head=insert_front(head); break;
case 2: head=insert_rear(head); break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}
NODE* delete_front(NODE *head)
{
NODE *p;
if(head==NULL)
printf("\nList is Empty/Underflow (STACK/QUEUE)");
else
{
p=head;
head=head->next; //Set 2nd NODE as head
free(p);
printf("\nFront(first)node is deleted");
}
return head;
}

NODE* delete_rear(NODE *head)
{
NODE *p, *q;
p=head;
while(p->next!=NULL)
{
p=p->next; //Go upto -1 NODE which you want to delete
}
q=p->next;
free(q);//Delete last NODE...
p->next=NULL;
printf("\nLast(end) entry is deleted");
return head;
}
NODE* del(NODE *head)
{
int ch;
do{
printf("\n1.Delete from Front(First)\t2. Delete from End(Rear/Last))\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: head=delete_front(head);
break;
case 2: head=delete_rear(head);
break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}
NODE* stack(NODE *head)
{
int ch;
do
{
printf("\nSSL used as Stack...");
printf("\n 1.Insert at Front(PUSH) \t 2.Delete from Front(POP))\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{

case 1: head=insert_front(head); break;
case 2: head=delete_front(head); break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}
NODE* queue(NODE *head)
{
int ch;
do
{
printf("\nSSL used as Queue...");
printf("\n 1.Insert at Rear(INSERT) \t 2.Delete from Front(DELETE))\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: head=insert_rear(head); break;
case 2: head=delete_front(head); break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}
void main()
{
int ch, i, n;
NODE *head;
head=NULL;
clrscr();
printf("\n*----------Studednt Database-----------*");
do
{
printf("\n 1.Create\t 2.Display\t 3.Insert\t 4.Delete\t 5.Stack\t 6.Queue\t 7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nHow many student data you want to create: ");
scanf("%d", &n);

for(i=0;i<n;i++)
head=create(head);//Call to Create NODE...
break;
case 2: head=display(head); //Call to Display...
break;
case 3: head=insert(head); //Call to Insert...
break;
case 4: head=del(head); //Call to delete
break;
case 5: head=stack(head);
break;
case 6: head=queue(head);
break;
case 7: exit(); //Exit...
}
}while(ch!=7);
}
				
			
OUTPUT:
1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7. Exit
Enter your choice: 1
How many student data you want to create: 2
Enter USN, Name, Branch, Sem, Ph.No
1kt12cs001 kumar cs 3 9900099000
Enter USN, Name, Branch, Sem, Ph.No
1kt12is002 ravi is 3 9900099111
1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7. Exit
Enter your choice: 2
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO.
1kt12is002 ravi is 3 9900099111
1kt12cs001 kumar cs 3 9900099000
The no. of nodes in list is: 2

1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7. Exit
Enter your choice: 3
1.Insert at Front(First) 2.Insert at End(Rear/Last) 3.Exit

Enter your choice: 1
Enter USN, Name, Branch, Sem, Ph.No
1kt12cs003 suresh cs 3 99000992222
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO.
1kt12cs003 suresh cs 3 99000992222
1kt12is002 ravi is 3 9900099111
1kt12cs001 kumar cs 3 9900099000
The no. of nodes in list is: 3
1.Insert at Front(First) 2.Insert at End(Rear/Last) 3.Exit
Enter your choice: 2
Enter USN, Name, Branch, Sem, Ph.No
1kt12is004 naresh is 3 99000993333
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO.
1kt12cs003 suresh cs 3 99000992222
1kt12is002 ravi is 3 9900099111
1kt12cs001 kumar cs 3 9900099000
1kt12is004 naresh is 3 99000993333
The no. of nodes in list is: 4
1.Insert at Front(First) 2.Insert at End(Rear/Last) 3.Exit
Enter your choice: 3
1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7. Exit
Enter your choice: 4
1. Delete from Front (First) 2. Delete from End (Rear/Last) 3.Exit
Enter your choice: 1
Front (first) node is deleted
----STUDENT DATA----
USN          NAME     BRANCH SEM Ph.NO.
1kt12is002   ravi       is     3 9900099111
1kt12cs001   kumar      cs     3 9900099000
1kt12is004   naresh     is     3 99000993333
The no. of nodes in list is: 3
1. Delete from Front (First) 2. Delete from End (Rear/Last) 3.Exit
Enter your choice: 2
Last (end) node is deleted
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO.
1kt12is002 ravi is 3 9900099111
1kt12cs001 kumar cs 3 9900099000
The no. of nodes in list is: 2
1. Delete from Front (First) 2. Delete from End (Rear/Last) 3.Exit
Enter your choice: 3
1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7. Exit
Enter your choice: 5
SSL used as Stack...
1.Insert at Front(PUSH) 2.Delete from Front(POP)) 3.Exit
Enter your choice: 1
Enter USN, Name, Branch, Sem, Ph.No
1kt12is010 vikky is 3 9900011111
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO.
1kt12is010 vikky is 3 9900011111
1kt12is002 ravi is 3 9900099111
1kt12cs001 kumar cs 3 9900099000

1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7. Exit
Enter your choice: 7

Leave a Reply