用数组表示堆栈的操作(c语言)
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
//数组表示堆栈
typedef struct stack
{
int data[MaxSize];
int top;
} stack;
stack * createStack()
{
stack * st = (stack*)malloc(sizeof(stack));
if(st == NULL)
return ;
st->top = -1;
return st;
}
void push(stack * st, int data)
{
st->top++;
st->data[st->top]= data;
}
void pop(stack * st, int data)
{
int n = st->top;
while(n != -1)
{
if(st->data[n] == data)
{
st->top = n;
break;
}
n--;
}
}
void display(stack * st)
{
int n = st->top;
while(n != -1)
{
printf("%d ",st->data[n]);
n--;
}
printf("\n");
}
int main()
{
int i,j;
stack * st;
st = createStack();
for(i=1;i<=5;i++)
{
push(st,i);
printf("%d入栈\n",i);
}
display(st);
// for(j=5;j>=1;j--)
// {
// pop(st,j);
// printf("%d出栈\n",j);
// }
return 0;
}