Final Project
Final Project
Computer Science
Project Workbook
XI
Name : ____________________________
School : ________________________________
1
Acknowledgment
I would also like to acknowledge my family and friends for their continuous
motivation and support during this project. Their encouragement played a
significant role in successfully completing this work.
Lastly, I am grateful for the resources and learning materials that assisted me in
developing this menu-driven stack program, enhancing my programming skills.
Submitted by:
Soumydeep Saha
Class: XI(Science)
2
Coding
#include<stdio.h> void main()
#include<conio.h> {
int ar[5],i,top=-1,num; int ch;
void push() clrscr();
{ while(1)
clrscr(); {
if(top==4) clrscr();
printf("Stack overflow!\n"); printf("Menu\n");
else
{ printf("1.Display\n2.Push\n3.Pop\n4.Exit\nSelect=");
++top; scanf("%d", &ch);
printf("Enter a number="); if(ch==1) display();
scanf("%d", &num); if(ch==2) push();
ar[top]=num; if(ch==3) pop();
} if(ch==4) break;
} }
void pop() getch();
{ }
clrscr();
if(top==-1)
printf("\nStack underflow!\n");
else
{ printf("\n%d is popped\n", ar[top]);
getch();
top=top-1;
}
}
void display()
{
clrscr();
printf("\nStack elements\n");
for(i=top;i>=0;i--)
{
printf("%d\n", ar[i]);
}
getch();
}
3
This C program implements a stack using an array. The stack follows the LIFO (Last In, First
Out) principle. The program provides options to:
The stack has a fixed size of 5 elements. If the stack is full, it shows "Stack overflow", and if
it's empty, it shows "Stack underflow".
Algorithm
1. Push Operation
Step 1: Check if top == 4 (stack full). If true, print "Stack Overflow" and return.
Step 2: If not full, increment top by 1.
Step 3: Read the new element from the user.
Step 4: Store the element at ar[top].
2. Pop Operation
Step 1: Check if top == -1 (stack empty). If true, print "Stack Underflow" and return.
Step 2: Display the element at ar[top] as the popped value.
Step 3: Decrease top by 1.
3. Display Operation
Step 1: Check if top == -1 (stack empty). If true, print "Stack is empty" and return.
Step 2: Print all elements from top to 0.
4
5