/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/
/**
* program for stack using array
*/
#include<stdio.h>
#define SIZE 100
int peep(int a[], int top, int i)
{
if( i < 0 && i>= top)
{
printf("This is invalid operation. Index out of range.");
}
else
{
return a[i];
}
}
int push(int *a , int top, int ele)
{
if(top >= SIZE)
{
printf("\nStack Overflow");
}
else
{
a[++top] = ele;
}
return top;
}
int pop(int *a,int *top)
{
if((*top) >=0)
{
(*top) = (*top) -1 ;
return a[(*top)+1];
}
else
{
printf("Stack underflow\n");
return 0;
}
}
void change(int *a,int top, int index, int element)
{
if(index < 0 || index > top)
{
printf("The index is out of range");
}
else
{
a[index] = element;
}
}
void main()
{
// void push(int [] ,int ,int );
int i= 0 ;
int top = -1;
int a[SIZE];
int d = 1;
int temp,temp1;
do
{
printf("\n\nSelect any option");
printf("\n1 Display stack");
printf("\n2 push");
printf("\n3 pop");
printf("\n4 peep");
printf("\n5 change");
printf("\n6 exit\n");
scanf("%d",&d);
switch(d)
{
case 1:
if(top>=0)
{
for(i = 0 ; i <= top ; i++ )
{
printf("\nelement at %d is %d" , i , peep(a,top,i));
}
}
else
{
printf("\nThe stack is empty");
}
break;
case 2:
printf("Enter number you want to insert in the stack: ");
scanf("%d",&temp);
top = push( a , top , temp);
break;
case 3:
printf("The popped element is: %d" , pop(a,&top));
break;
case 4:
printf("At which position you want to peep ? ");
scanf("%d",&temp);
printf("Peeped element is: %d" , peep(a,top,temp) );
break;
case 5:
printf("Enter the index at which you want to change: ");
scanf("%d",&temp);
printf("Enter he actual element that you want to enter: ");
scanf("%d",&temp1);
change(a,top,temp,temp1);
break;
default:
printf("Please enter a valid choice.");
break;
}
}
while(d != 6);
// and yes, i think you will be able to writ getch here (on TC3)
// i am on linux so that thing is not available here
}
Tuesday, December 28, 2010
stack using array
Labels:
C,
C program,
D.S,
data structure,
DS,
DS data structure,
stack,
stack using array
Subscribe to:
Post Comments (Atom)
and yes,
ReplyDeleteaccording to programming rules, i have written break after default case. But if you remove that, still it will give correct output.
however i prefer to write break after default: