Program:
#include <stdio.h>
#include <stdlib.h>
struct student
{
int rollno;
char name[20];
float fee;
};
int top = -1;
struct student stk[5];
void push()
{
if (top == 4)
printf("Overflow: Stack is Already Full. You cant insert more items\n");
else
{
top = top + 1;
printf("\nenter student roll no: ");
scanf("%d", &stk[top].rollno);
printf("enter name: ");
fflush(stdin);
gets(stk[top].name);
printf("enter fee : ");
scanf("%f", &stk[top].fee);
}
}
void pop()
{
if (top == -1)
printf("underflow!!!. Stack is empty. No item to delete");
else
{
printf("Deleted item is :\n");
printf("Rollno\tname\tfee");
printf("\n%d\t%s\t%f\n", stk[top].rollno, stk[top].name, stk[top].fee);
top = top - 1;
}
}
void peek()
{
if (top == -1)
printf("Stack is empty. No item to display");
else
{
printf("Item at the Top is :\n");
printf("Rollno\tname\tfee");
printf("\n%d\t%s\t%f\n", stk[top].rollno, stk[top].name, stk[top].fee);
}
}
void display()
{
if (top == -1)
printf("Stack is empty. No item to display");
else
{
printf("\nInformation of stack is : \n");
printf("Rollno\tname\tfee");
for (int i = top; i >= 0; i--)
printf("\n%d\t%s\t%f\n", stk[i].rollno, stk[i].name, stk[i].fee);
}
}
void main()
{
int ch;
printf("****************************************\n");
printf("****************************************\n");
printf("** Static / Linear stack **\n");
printf("** Using array of Structures **\n");
printf("** Program Created By Sheetal Garg **\n");
printf("** Assistant professor **\n");
printf("** 9467863365 **\n");
printf("****************************************\n");
printf("****************************************\n");
while (1)
{
printf("\nOptions Available are\n");
printf("1. Push \n");
printf("2. Pop \n");
printf("3. Peek \n");
printf("4. Display \n");
printf("5. Exit \n");
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Wrong choice entered");
}
}
}
Output:
****************************************
****************************************
** Static / Linear stack **
** Using array of Structures **
** Program Created By Sheetal Garg **
** Assistant professor **
** 9467863365 **
****************************************
****************************************
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter student roll no: 1
enter name: Sheetal
enter fee : 10000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter student roll no: 2
enter name: Amit
enter fee : 14000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter student roll no: 3
enter name: Nidhi
enter fee : 20000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter student roll no: 4
enter name: Ansh
enter fee : 15000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
enter student roll no: 5
enter name: Saksham
enter fee : 50000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 1
Overflow: Stack is Already Full. You cant insert more items
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
Rollno name fee
5 Saksham 50000.000000
4 Ansh 15000.000000
3 Nidhi 20000.000000
2 Amit 14000.000000
1 Sheetal 10000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 3
Item at the Top is :
Rollno name fee
5 Saksham 50000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is :
Rollno name fee
5 Saksham 50000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
Rollno name fee
4 Ansh 15000.000000
3 Nidhi 20000.000000
2 Amit 14000.000000
1 Sheetal 10000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is :
Rollno name fee
4 Ansh 15000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
Rollno name fee
3 Nidhi 20000.000000
2 Amit 14000.000000
1 Sheetal 10000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is :
Rollno name fee
3 Nidhi 20000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
Rollno name fee
2 Amit 14000.000000
1 Sheetal 10000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is :
Rollno name fee
2 Amit 14000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Information of stack is :
Rollno name fee
1 Sheetal 10000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
Deleted item is :
Rollno name fee
1 Sheetal 10000.000000
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 4
Stack is empty. No item to display
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 2
underflow!!!. Stack is empty. No item to delete
Options Available are
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice : 5