C Stack data structure
What is a stack
A stack is a list of elements with insertions and deletions at one end. A stack data structure has the LIFO (last in first out) property.
Example
Array Implementation of a Stack
#include <stdio.h>
#define MAX 10// w w w .j a v a2 s . co m
#include <stdlib.h>
void push(int stack[], int *top, int value){
if(*top < MAX ){
*top = *top + 1;
stack[*top] = value;
}else{
printf("The stack is full can not push a value\n");
exit(0);
}
}
void pop(int stack[], int *top, int * value)
{
if(*top >= 0 )
{
*value = stack[*top];
*top = *top - 1;
}
else
{
printf("The stack is empty can not pop a value\n");
exit(0);
}
}
void main()
{
int stack[MAX];
int top = -1;
int n,value;
push(stack,&top,1);
push(stack,&top,2);
push(stack,&top,3);
pop(stack,&top,&value);
printf("The value poped is %d\n",value);
pop(stack,&top,&value);
printf("The value poped is %d\n",value);
}
The code above generates the following result.