《数据结构与算法》 青岛大学-王卓 栈(链式存储)C++
1.数据类型:栈 2.存储方式:链式存储 3.常用名称:链栈
(注:图片截取自《数据结构与算法》-青岛大学王卓bilibili视频)
#include<iostream>
using namespace std;
#define MAX_SIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int SElemType;
typedef struct StackNode
{
SElemType data;
struct StackNode* next;
}StackNode, *LinkStack;
Status InitStack(LinkStack& S)
{
S = NULL;
return OK;
}
Status StackEmpty(LinkStack S)
{
if (S == NULL) return TRUE;
else return FALSE;
}
Status Push(LinkStack& S, SElemType e)
{
StackNode* p = new StackNode;
p->data = e;
p->next = S;
S = p;
return OK;
}
Status Pop(LinkStack& S, SElemType& e)
{
if (S == NULL) return ERROR;
e = S->data;
LinkStack p = S;
S = S->next;
delete p;
return OK;
}
SElemType GetTop(LinkStack S)
{
if (S != NULL) return S->data;
}
int main()
{
LinkStack S;
InitStack(S);
Push(S, 10);
Push(S, 30);
Push(S, 50);
Push(S, 60);
int e;
while (S!=NULL)
{
Pop(S, e);
cout << e << endl;
}
return 0;
}
|