0% found this document useful (0 votes)
42 views18 pages

SLL Operations for Student Data in C

The document describes a menu driven C program to perform operations on a singly linked list (SLL) of student data. The program allows the user to create an SLL, display the SLL, insert and delete nodes at the front and end of the SLL, and demonstrate how the SLL can be used as a stack using insertion and deletion at the front. The program uses functions like create(), insertfront(), deletefront(), insertend(), deleteend(), and display() to implement these operations on the SLL.

Uploaded by

kashafaznain5
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)
42 views18 pages

SLL Operations for Student Data in C

The document describes a menu driven C program to perform operations on a singly linked list (SLL) of student data. The program allows the user to create an SLL, display the SLL, insert and delete nodes at the front and end of the SLL, and demonstrate how the SLL can be used as a stack using insertion and deletion at the front. The program uses functions like create(), insertfront(), deletefront(), insertend(), deleteend(), and display() to implement these operations on the SLL.

Uploaded by

kashafaznain5
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

Program 7

7. Develop a menu driven Program in C for the following operations on Singly


Linked List (SLL) of Student Data with the fields: USN, Name, Programme,
Sem, [Link]

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 / Deletion at End of SLL

d. Perform Insertion / Deletion at Front of SLL (Demonstration of stack)

e. Exit

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 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 singly linked list can be used as stack.

Step 11: Demonstrate how singly linked list can be used as queue.

Step 12: Stop.

1
Program 7

Program:-
#include<stdio.h>

#include<stdlib.h>

struct node

char usn[25],name[25],branch[25];

int sem;

long int phone;

struct node *link;

};

typedef struct node * NODE;

NODE start = NULL;

int count=0;

NODE create()

NODE snode;

snode = (NODE)malloc(sizeof(struct node));

if(snode == NULL)

printf("\nMemory is not available");

exit(1);

printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");

scanf("%s %s %s %d %ld",snode->usn, snode->name, snode->branch,


&snode->sem, &snode->phone);

2
Program 7

snode->link=NULL;

count++;

return snode;

NODE insertfront()

NODE temp;

temp = create();

if(start == NULL)

return temp;

temp->link = start;

return temp;

NODE deletefront()

NODE temp;

if(start == NULL)

printf("\nLinked list is empty");

return NULL;

if(start->link == NULL)

3
Program 7

printf("\nThe Student node with usn:%s is deleted ",start->usn);

count--;

free(start);

return NULL;

temp = start;

start = start->link;

printf("\nThe Student node with usn:%s is deleted",temp->usn);

count--;

free(temp);

return start;

NODE insertend()

NODE cur,temp;

temp = create();

if(start == NULL)

return temp;

cur = start;

while(cur->link !=NULL)

4
Program 7

cur = cur->link;

cur->link = temp;

return start;

NODE deleteend()

NODE cur,prev;

if(start == NULL)

printf("\nLinked List is empty");

return NULL;

if(start->link == NULL)

printf("\nThe student node with the usn:%s is deleted",start->usn);

free(start);

count--;

return NULL;

prev = NULL;

cur = start;

while(cur->link!=NULL)

5
Program 7

prev = cur;

cur = cur->link;

printf("\nThe student node with the usn:%s is deleted",cur->usn);

free(cur);

prev->link = NULL;

count--;

return start;

void display()

NODE cur;

int num=1;

if(start == NULL)

printf("\nNo Contents to display in SLL \n");

return;

printf("\nThe contents of SLL: \n");

cur = start;

while(cur!=NULL)

printf("\n||%d|| USN:%s| Name:%s| Branch:%s| Sem:%d|


Ph:%ld|",num,cur->usn, cur->name,cur->branch, cur->sem,cur->phone);

cur = cur->link;

6
Program 7

num++;

printf("\n No of student nodes is %d \n",count);

void stackdemo()

int ch;

while(1)

printf("\n~~~Stack Demo using SLL~~~\n");

printf("\n1:Push operation \n2: Pop operation \n3: Display \n4:Exit \n");

printf("\nEnter your choice for stack demo");

scanf("%d",&ch);

switch(ch)

case 1: start = insertfront();

break;

case 2: start = deletefront();

break;

case 3: display();

break;

default : return;

7
Program 7

return;

int main()

int ch,i,n;

while(1)

printf("\n~~~Menu~~~");

printf("\nEnter your choice for SLL operation \n");

printf("\n1:Create SLL of Student Nodes");

printf("\n2:DisplayStatus");

printf("\n3:InsertAtEnd");

printf("\n4:DeleteAtEnd");

printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");

printf("\n6:Exit \n");

printf("\nEnter your choice:");

scanf("%d",&ch);

switch(ch)

case 1 : printf("\nEnter the no of students: ");

scanf("%d",&n);

for(i=1;i<=n;i++)

start = insertfront();

break;

8
Program 7

case 2: display();

break;

case 3: start = insertend();

break;

case 4: start = deleteend();

break;

case 5: stackdemo();

break;

case 6: exit(0);

default: printf("\nPlease enter the valid choice");

Output:-
~~~Menu~~~
Enter your choice for SLL operation
1: Create SLL of Student Nodes
2: DisplayStatus
3: InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)

9
Program 7

6:Exit
Enter your choice:1
Enter the no of students: 3
Enter the usn,Name,Branch, sem,PhoneNo of the student:
111
aaa
cs
1
111111
Enter the usn,Name,Branch, sem,PhoneNo of the student:
222
bbb
ec
2
222222
Enter the usn,Name,Branch, sem,PhoneNo of the student:
333
ccc
ec
3
333333
~~~Menu~~~

10
Program 7

Enter your choice for SLL operation


1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:2
The contents of SLL:
||1|| USN:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||2|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
||3|| USN:111| Name:aaa| Branch:cs| Sem:1| Ph:111111|
No of student nodes is 3
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:3

11
Program 7

Enter the usn,Name,Branch, sem,PhoneNo of the student:


444
ddd
ec
4
444444
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:2
The contents of SLL:
||1|| USN:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||2|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
||3|| USN:111| Name:aaa| Branch:cs| Sem:1| Ph:111111|
||4|| USN:444| Name:ddd| Branch:ec| Sem:4| Ph:444444|
No of student nodes is 4
~~~Menu~~~

12
Program 7

Enter your choice for SLL operation


1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:4
The student node with the usn: 444 is deleted
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:2
The contents of SLL:
||1|| USN:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||2|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
||3|| USN:111| Name:aaa| Branch:cs| Sem:1| Ph:111111|

13
Program 7

No of student nodes is 3
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:4
The student node with the usn: 111 is deleted
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:5
~~~Stack Demo using SLL~~~
1:Push operation

14
Program 7

2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 1
Enter the usn,Name,Branch, sem,PhoneNo of the student:
555
eee
cs
1
555555
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo:3
The contents of SLL:
||1|| USN:555| Name:eee| Branch:cs| Sem:1| Ph:555555|
||2|| USN:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||3|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
No of student nodes is 3
~~~Stack Demo using SLL~~~

15
Program 7

1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 1
Enter the usn,Name,Branch, sem,PhoneNo of the student:
666
fff
cs
6
666666
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 3
The contents of SLL:
||1|| USN:666| Name:fff| Branch:cs| Sem:6| Ph:666666|
||2|| USN:555| Name:eee| Branch:cs| Sem:1| Ph:555555|
||3|| USN:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||4|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|

16
Program 7

No of student nodes is 4
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 2
The Student node with usn: 666 is deleted

~~~Stack Demo using SLL~~~


1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 3
The contents of SLL:
||1|| USN:555| Name:eee| Branch:cs| Sem:1| Ph:555555|
||2|| USN:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||3|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
No of student nodes is 3
~~~Stack Demo using SLL~~~

17
Program 7

1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 4
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:6

18

Common questions

Powered by AI

Using the `start` pointer as a global variable simplifies managing the linked list across different functions without needing to pass it explicitly as a parameter. This approach aids in the seamless manipulation of the linked list. However, it poses challenges related to encapsulation and can lead to potential conflicts and errors in larger programs due to its global accessibility .

The process of deleting a node at the end of the linked list involves checking if the list is empty, in which case a message is printed and the function returns NULL. If the list has only one node, the program frees that node and updates the list start to NULL. For lists with multiple nodes, it traverses to the second last node, frees the last node, sets the second last node's `link` to NULL, and adjusts the node count accordingly .

The program's approach for displaying the singly linked list involves traversing the list starting from the `start` and printing each node's information sequentially. This is achieved by iterating through the nodes using a loop until the current node pointer is `NULL`. While straightforward, this method has a time complexity of O(n), where n is the number of nodes, which is efficient given that only one pass through the list is needed .

To insert a node at the end, the program first creates a new node with dynamically allocated memory. It then traverses the list to reach the last node and updates its `link` to point to the new node. However, this method can be inefficient for long lists as it requires traversing the entire list each time, leading to O(n) time complexity for each insertion .

The program maintains a global variable `count` to track the number of nodes in the singly linked list. This variable is incremented each time a node is inserted and decremented when a node is deleted, thereby keeping an accurate count reflecting the current number of nodes at any given time .

Enhancements to the menu-driven interface could include implementing input validation to handle incorrect user inputs gracefully, providing more descriptive prompts and error messages, and adding functionality such as search and update operations for nodes. Additionally, implementing a sub-menu for operations like stack and queue demos could improve organization and ease of navigation through the program's functions .

Front insertion for creating a singly linked list results in nodes being added to the head of the list. This means that the last inserted node becomes the first node in the list, effectively reversing the order of the input sequence when the list is displayed. This ordering is because each new node's `link` points to the previous start of the list, making it the new head .

The key operations performed on the singly linked list (SLL) in the program include creation of the list using front insertion, displaying the status and counting the nodes, insertion and deletion at both the front and end of the list, demonstrating the use of SLL as a stack using front insertion and deletion (push and pop operations), and displaying the list contents .

The program uses dynamic memory allocation to manage memory for nodes in the singly linked list. It employs the `malloc` function to allocate memory when new nodes are inserted and uses `free` to deallocate memory when nodes are deleted. This ensures efficient memory usage and helps prevent memory leaks .

The program demonstrates the use of a singly linked list (SLL) as a stack by using front insertion as the push operation and front deletion as the pop operation. This is reflected in the "stackdemo" function, where the stack operations are menu-driven, and the SLL behaves like a stack through these operations .

You might also like