can somone gave me a program(source code) in turbo c that implements the stack, how to use stack and its operations...
Write a program in turbo c that implements stack?
//Stack Implementation
#include%26lt;stdio.h%26gt;
#define MAX 5
struct stack
{
int arr[MAX];
int top;
};
typedef struct stack STK;
void push(STK*,int);
void pop(STK*);
void main(void)
{
STK stk;
int i,x;
stk.top=-1;
clrscr();
do{
printf("\n1: to push a no.");
printf("\n2: to pop a no.");
printf("\n0: to end");
printf("\n\n\nEnter your choice");
scanf("%d",%26amp;i);
switch(i)
{
case 1 :printf("\nGive the element");
scanf("%d",%26amp;x);
push(%26amp;stk,x);
break;
case 2 :pop(%26amp;stk);
break;
case 0 :printf("\nthe end");
break;
default:printf("\nInvalid choice");
break;
}}while(i);
getch();
}
void push(STK *p,int item)
{
if(p-%26gt;top==MAX-1)
{
printf("\nStack Overflow");
return;
}
p-%26gt;top++;
p-%26gt;arr[p-%26gt;top]=item;
}
void pop(STK *p)
{
if(p-%26gt;top==-1)
{
printf("\nStack Underflow");
return;
}
printf("\npopped element is %d",p-%26gt;arr[p-%26gt;top]);
p-%26gt;top--;
}
Reply:There are plenty of code examples in books and on the internet .... try a search engine.
Reply:You shouldn't go around asking people for code ;). I will give you some suggestions. A stack is basically an array of values of some type that has a special property: only the last (or top most if you think vertically) value can be accessed. To put it simple, imagine a stack of plates. You can't just get a plate (value) from the middle of the stack because the other plates would fall off. The only one you can access is the top most one which you can securely remove it.
There are two operations: adding a new plate (value) on top of the stack and removing one for usage. Once you add a new plate, that's the only one you can take off next. You could find these operations called pushing and popping.
Anyway, if you have an array of integers defined as stack int[100] and a count as int count = 0, then you would have the push routine do "count++; stack[count] = value;" and the pop do "count--;".
spring flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment