DSA Lab Program - 7
DSA Lab Program - 7
e. Exit
Algorithm:-
Step 1: Start.
Step 10: Demonstrate how singly linked list can be used as stack.
Step 11: Demonstrate how singly linked list can be used as queue.
1
Program 7
Program:-
#include<stdio.h>
#include<stdlib.h>
struct node
char usn[25],name[25],branch[25];
int sem;
};
int count=0;
NODE create()
NODE snode;
if(snode == NULL)
exit(1);
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)
return NULL;
if(start->link == NULL)
3
Program 7
count--;
free(start);
return NULL;
temp = start;
start = start->link;
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)
return NULL;
if(start->link == NULL)
free(start);
count--;
return NULL;
prev = NULL;
cur = start;
while(cur->link!=NULL)
5
Program 7
prev = cur;
cur = cur->link;
free(cur);
prev->link = NULL;
count--;
return start;
void display()
NODE cur;
int num=1;
if(start == NULL)
return;
cur = start;
while(cur!=NULL)
cur = cur->link;
6
Program 7
num++;
void stackdemo()
int ch;
while(1)
scanf("%d",&ch);
switch(ch)
break;
break;
case 3: display();
break;
default : return;
7
Program 7
return;
int main()
int ch,i,n;
while(1)
printf("\n~~~Menu~~~");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n6:Exit \n");
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
for(i=1;i<=n;i++)
start = insertfront();
break;
8
Program 7
case 2: display();
break;
break;
break;
case 5: stackdemo();
break;
case 6: exit(0);
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
11
Program 7
12
Program 7
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
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