/*
Program to implement a Stack using Array.
Created By: Pirate
*/
#include<stdio.h>
int stack[200];
int push(int top,int size);
int pop(int top);
int traverse(int top);
void main(){
int size,top=-1;
int ch=1;
printf("*** Stack Using Array ***");
do{
printf("\nEnter the size of Stack : ");
scanf("%d", &size);
if(size < 1){
printf("\nInvalid Stack size. \n Continue Y : 1/N : 0 - ");
scanf("%d",&ch);
if(ch == 0)
exit(0);
}
}while(size < 1 && ch == 1);
do{
printf("\n1 : Insert\n2 : Delete\n3 : Traverse\n4 : Exit\n");
scanf("%d",&ch);
switch(ch){
case 1:
top = push(top,size);
break;
case 2:
top = pop(top);
break;
case 3:
top = traverse(top);
break;
case 4:
exit(0);
break;
default:
printf("\n Invalid input. \n");
}
}while(1);
}
int push(int top,int size){
if(top == size - 1){
printf("\nStack is full");
return top;
}
top = top + 1;
printf("\nEnter the value to input : ");
scanf("%d", &stack[top]);
printf("\n Success.");
return top;
}
int pop(int top){
if(top == -1){
printf("\nStack is Empty.");
return top;
}
top = top - 1;
printf("\n Success.");
return top;
}
int traverse(int top){
int i;
if(top == -1){
printf("\nStack is Empty.");
return top;
}
printf("\nStack : \n");
for(i=top;i>=0;i++){
printf("%d\n",stack[i]);
}
return top;
}
Output :
Program to implement a Stack using Array.
Created By: Pirate
*/
#include<stdio.h>
int stack[200];
int push(int top,int size);
int pop(int top);
int traverse(int top);
void main(){
int size,top=-1;
int ch=1;
printf("*** Stack Using Array ***");
do{
printf("\nEnter the size of Stack : ");
scanf("%d", &size);
if(size < 1){
printf("\nInvalid Stack size. \n Continue Y : 1/N : 0 - ");
scanf("%d",&ch);
if(ch == 0)
exit(0);
}
}while(size < 1 && ch == 1);
do{
printf("\n1 : Insert\n2 : Delete\n3 : Traverse\n4 : Exit\n");
scanf("%d",&ch);
switch(ch){
case 1:
top = push(top,size);
break;
case 2:
top = pop(top);
break;
case 3:
top = traverse(top);
break;
case 4:
exit(0);
break;
default:
printf("\n Invalid input. \n");
}
}while(1);
}
int push(int top,int size){
if(top == size - 1){
printf("\nStack is full");
return top;
}
top = top + 1;
printf("\nEnter the value to input : ");
scanf("%d", &stack[top]);
printf("\n Success.");
return top;
}
int pop(int top){
if(top == -1){
printf("\nStack is Empty.");
return top;
}
top = top - 1;
printf("\n Success.");
return top;
}
int traverse(int top){
int i;
if(top == -1){
printf("\nStack is Empty.");
return top;
}
printf("\nStack : \n");
for(i=top;i>=0;i++){
printf("%d\n",stack[i]);
}
return top;
}
Output :
0 comments:
Post a Comment