进栈、出栈的C++实现过程程序:

来源:百度知道 编辑:UC知道 时间:2024/06/03 17:25:30

#include<iostream.h>
class STACK{
private:
int base[100];
public:
int size;
STACK(){size=0;};
void push(int a)//入栈
{
base[size++]=a;
}
int pop()//出栈
{
return base[--size];
}
};
int main()
{
STACK s;
s.push(3);
s.push(4);
cout<<s.pop()<<endl;
cout<<s.pop()<<endl;
system("pause");
}

/*此例主要理会建栈和地址传递(y)的应用*/
#include<stdio.h>
typedef struct stack_node{
char data;
struct stack_node *next;
}SLnode;/*栈结点结构*/
typedef struct {
SLnode *top;
}LS;/*栈顶指针*/
void InitStack(LS *s)/*建空栈*/
{
s->top=NULL;
}
void Push(LS *s,char x)/*入栈*/
{
SLnode *p;
p=(SLnode *)malloc(sizeof(SLnode));/*结点空间生成*/
p->data=x;
p->next=s->top;/*指针指向*/
s->top=p;
}
char pop(LS *s,char *y)
{ SL