谁能告诉我C语言中的栈如何定义我
你这样试试:
typedef int Elem;
#define N 100/*N是栈的足够的空间*/
Elem stack[N];
int top;/*栈顶指针,当栈为空时top=1,即top指向栈顶元素的上一个元素*/
/*定义栈的一般方法*/
void Init_Stack()/*栈的初始化*/
{
top=1;/*清空栈*/
}
int IsEmpty()/*判断栈是否为空*/
{
if (top=N)return 1;
else return 0;
}
void Push_Stack(Elem x)/*入栈函数*/
{
if (IsFull()){printf("Stack is...全部
你这样试试:
typedef int Elem;
#define N 100/*N是栈的足够的空间*/
Elem stack[N];
int top;/*栈顶指针,当栈为空时top=1,即top指向栈顶元素的上一个元素*/
/*定义栈的一般方法*/
void Init_Stack()/*栈的初始化*/
{
top=1;/*清空栈*/
}
int IsEmpty()/*判断栈是否为空*/
{
if (top=N)return 1;
else return 0;
}
void Push_Stack(Elem x)/*入栈函数*/
{
if (IsFull()){printf("Stack is full,cannot push element!");exit(0);}
stack[top]=x;
top++;
}
Elem Pop_Stack()/*出栈函数*/
{
Elem ret;
if(IsEmpty()){printf("Stack is empty,cannot pop element!\n");exit(0);}
top--;
ret=stack[top];
return ret;
}
main()/*用作测试*/
{
int i=0;
Init_Stack();
for (i=0;i<10;i++)
Push_Stack(i);
for (i=0;i<10;i++)
printf("%4d",Pop_Stack());
}
/*------------------*/
可以根据你的需要丰富你的栈处理方法,如果用C++的话,写出来的就比较好了。
收起