0% found this document useful (0 votes)
8 views8 pages

Program 7

The document is a C program that implements a singly linked list (SLL) to manage student records, allowing operations such as insertion, deletion, and display of student information. It includes a menu-driven interface for creating the SLL, inserting and deleting nodes at both ends, and a stack demonstration using the SLL. The program handles memory allocation and provides feedback on operations performed on the linked list.

Uploaded by

kinkajou129900
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)
8 views8 pages

Program 7

The document is a C program that implements a singly linked list (SLL) to manage student records, allowing operations such as insertion, deletion, and display of student information. It includes a menu-driven interface for creating the SLL, inserting and deleting nodes at both ends, and a stack demonstration using the SLL. The program handles memory allocation and provides feedback on operations performed on the linked list.

Uploaded by

kinkajou129900
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

Input:

#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("%24s %24s %24s %d %ld",
snode->usn, snode->name, snode->branch, &snode->sem, &snode-
>phone);

snode->link = NULL;
count++;
return snode;
}

NODE insertfront() {
NODE 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) {
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) {
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) {
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;
num++;
}
printf("\nNo of student nodes is %d\n", count);
}

void stackdemo() {
int ch;
while (1) {
printf("\n--------Stack Demo using SLL--------\n");
printf("1: Push operation\n");
printf("2: Pop operation\n");
printf("3: Display\n");
printf("4: Exit\n");
printf("Enter your choice for stack demo: ");
scanf("%d", &ch);

switch (ch) {
case 1:
start = insertfront();
break;
case 2:
start = deletefront();
break;
case 3:
display();
break;
case 4:
return;
default:
printf("\nPlease enter a valid choice\n");
}
}
}

int main() {
int ch, i, n;

while (1) {
printf("\n--------Menu--------\n");
printf("Enter your choice for SLL operation\n");
printf("1: Create SLL of Student Nodes\n");
printf("2: Display Status\n");
printf("3: Insert At End\n");
printf("4: Delete At End\n");
printf("5: Stack Demo using SLL (Insertion and Deletion at
Front)\n");
printf("6: Exit\n");
printf("Enter 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;

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 a valid choice\n");
}
}
}

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)
6:Exit

Enter your choice:1

Enter the no of students: 3


Enter the usn,Name,Branch, sem,PhoneNo of the student:
1ME21CS017
Braham
CSE
5
8768586443

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


1ME21CS015
Bikash
CSE
5
8734687996
Enter the usn,Name,Branch, sem,PhoneNo of the student:
1ME21AI015
Shoaib
AI&ML
5
6748353877

--------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:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|


|2| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
|3| |USN:1ME21CS017| |Name:Braham| |Branch:CSE | |Sem:5| |Ph:8768586443|
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

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


1ME21CS068
Rajan
CSE
5
3426527765

--------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:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|


|2| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
|3| |USN:1ME21CS017| |Name:Braham| |Branch:CSE | |Sem:5| |Ph:8768586443|
|4| |USN:1ME21CS068| |Name:Rajan | |Branch:CSE | |Sem:5| |Ph:3426527765|
No of student nodes is 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:4


The student node with the usn:1ME21CS068 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:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|


|2| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
|3| |USN:1ME21CS017| |Name:Braham| |Branch:CSE | |Sem:5| |Ph:8768586443|
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:1ME21CS017 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
2: Pop operation
3: Display
4:Exit

Enter your choice for stack demo:1

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


1ME21CS005
Aman
CSE
5
6587594335

--------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:1ME21CS005| |Name:Aman | |Branch:CSE | |Sem:5| |Ph:6587594335|


|2| |USN:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|
|3| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
No of student nodes is 3

--------Stack Demo using SLL--------

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:


1ME21CS092
Shubham
CSE
5
9869754354

--------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:1ME21CS092| |Name:Shubham| |Branch:CSE | |Sem:5| |Ph:9869754354|


|2| |USN:1ME21CS005| |Name:Aman | |Branch:CSE | |Sem:5| |Ph:6587594335|
|3| |USN:1ME21AI015| |Name:Shoaib | |Branch:AI&ML| |Sem:5| |Ph:6748353877|
|4| |USN:1ME21CS015| |Name:Bikash | |Branch:CSE | |Sem:5| |Ph:8734687996|
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:1ME21CS092 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:1ME21CS005| |Name:Aman | |Branch:CSE | |Sem:5| |Ph:6587594335|


|2| |USN:1ME21AI015| |Name:Shoaib| |Branch:AI&ML| |Sem:5| |Ph:6748353877|
|3| |USN:1ME21CS015| |Name:Bikash| |Branch:CSE | |Sem:5| |Ph:8734687996|
No of student nodes is 3

--------Stack Demo using SLL--------

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

You might also like