English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

can somone gave me a program(source code) in turbo c that implements the stack, how to use stack and its operations...

2007-03-03 23:07:11 · 3 answers · asked by jan 1 in Computers & Internet Programming & Design

3 answers

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--;".

2007-03-04 00:39:40 · answer #1 · answered by Rimio 2 · 0 0

There are plenty of code examples in books and on the internet .... try a search engine.

2007-03-04 09:12:23 · answer #2 · answered by lda 4 · 0 0

//Stack Implementation
#include
#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",&i);
switch(i)
{
case 1 :printf("\nGive the element");
scanf("%d",&x);
push(&stk,x);
break;
case 2 :pop(&stk);
break;
case 0 :printf("\nthe end");
break;
default:printf("\nInvalid choice");
break;
}}while(i);
getch();
}

void push(STK *p,int item)
{
if(p->top==MAX-1)
{
printf("\nStack Overflow");
return;
}
p->top++;
p->arr[p->top]=item;
}

void pop(STK *p)
{
if(p->top==-1)
{
printf("\nStack Underflow");
return;
}
printf("\npopped element is %d",p->arr[p->top]);
p->top--;
}

2007-03-04 18:24:20 · answer #3 · answered by Innocence Redefined 5 · 0 0

fedest.com, questions and answers