Implement basic operations on Stacks.
Source Code:
#include<stdio.h>
void push(int);
int pop();
int peek();
void display();
int n, stack[20], top = -1;
int main(){
int choice, ele;
printf("Enter stack size:\n");
scanf("%d",&n);
printf("::Stack Operations::\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
do{
printf("Select operation:\n");
scanf("%d",&choice);
switch(choice){
case 1:
if(top==n-1){
printf("Stack Overflow.\n");
}else{
printf("Enter new element:\n");
scanf("%d",&ele);
push(ele);
}
break;
case 2:
if(top==-1){
printf("Stack Underflow.\n");
}else{
ele = pop();
printf("Deleted element is:%d\n",ele);
}
break;
case 3:
if(top==-1){
printf("Stack is empty.\n");
}else{
ele = peek();
printf("Top most element is %d\n",ele);
}
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid operation.\n");
}
}while(choice!=5);
return 0;
}
void push(int ele){
stack[++top] = ele;
}
int pop(){
return(stack[top--]);
}
int peek(){
return(stack[top]);
}
void display(){
int i;
if(top==-1){
printf("Stack is empty.\n");
}else{
printf("Stack elements are:\n");
for(i=top;i>=0;i--){
printf("%d ",stack[i]);
}
printf("\n");
}
}
void push(int);
int pop();
int peek();
void display();
int n, stack[20], top = -1;
int main(){
int choice, ele;
printf("Enter stack size:\n");
scanf("%d",&n);
printf("::Stack Operations::\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
do{
printf("Select operation:\n");
scanf("%d",&choice);
switch(choice){
case 1:
if(top==n-1){
printf("Stack Overflow.\n");
}else{
printf("Enter new element:\n");
scanf("%d",&ele);
push(ele);
}
break;
case 2:
if(top==-1){
printf("Stack Underflow.\n");
}else{
ele = pop();
printf("Deleted element is:%d\n",ele);
}
break;
case 3:
if(top==-1){
printf("Stack is empty.\n");
}else{
ele = peek();
printf("Top most element is %d\n",ele);
}
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid operation.\n");
}
}while(choice!=5);
return 0;
}
void push(int ele){
stack[++top] = ele;
}
int pop(){
return(stack[top--]);
}
int peek(){
return(stack[top]);
}
void display(){
int i;
if(top==-1){
printf("Stack is empty.\n");
}else{
printf("Stack elements are:\n");
for(i=top;i>=0;i--){
printf("%d ",stack[i]);
}
printf("\n");
}
}
Enter stack size:
3
::Stack Operations::
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Select operation:
1
Enter new element:
5
Select operation:
1
Enter new element:
10
Select operation:
1
Enter new element:
15
Select operation:
4
Stack elements are:
15 10 5
Select operation:
2
Deleted element is:15
Select operation:
4
Stack elements are:
10 5
Select operation:
3
Top most element is 10
Select operation:
5
3
::Stack Operations::
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Select operation:
1
Enter new element:
5
Select operation:
1
Enter new element:
10
Select operation:
1
Enter new element:
15
Select operation:
4
Stack elements are:
15 10 5
Select operation:
2
Deleted element is:15
Select operation:
4
Stack elements are:
10 5
Select operation:
3
Top most element is 10
Select operation:
5