📄 stack.cpp
字号:
#include <stdlib.h>
#define MaxSize 10
typedef int ElemType;
typedef struct
{
int top;
ElemType data[MaxSize];
}SqStack;
typedef struct linknode
{
ElemType data;
struct linknode *next;
}LiStack;
void InitStack_sq(SqStack *&S)
{
S=(SqStack *)malloc(sizeof(SqStack));
S->top=-1;
}
bool Push_sq(SqStack *&S,ElemType e)
{
if(S->top==MaxSize-1)
return false;
else
{ S->top++;
S->data[S->top]=e;
return true;
}
}
bool Pop_sq(SqStack *&S,ElemType &e)
{
if(S->top==-1)
return false;
e=S->data[S->top];
S->top--;
return true;
}
void InitStack_L(LiStack *&S)
{
S=(LiStack *)malloc(sizeof(LiStack));
S->next=NULL;
}
void Push_L(LiStack *&S,ElemType e)
{ LiStack *p;
p=(LiStack *)malloc(sizeof(LiStack));
p->data=e;
p->next=S->next ;
S->next=p;
}
bool Pop_L(LiStack *&S,ElemType &e)
{
if(S->next ==NULL)
return false;
LiStack *p;
p=S->next ;
e=p->data;
S->next=p->next;
free(p);
return true;
}
void main()
{
SqStack *S;
ElemType e;
bool b;
InitStack_sq(S);
/*for(int i=1;i<=5;i++)
b=Push_sq(S,16+i);
b=Pop_sq(S,e);*/
int m=12345;
while(m!=0)
{
Push_sq(S,m%8);
m=m/8;
}
while(Pop_sq(S,e))
;
LiStack *St;
InitStack_L(St);
Push_L(St,100);
Push_L(St,200);
b=Pop_L(St,e);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -