#include <iostream>
using namespace std
;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
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
= NULL;
p
= new StackNode
;
p
->data
= e
;
p
->next
= S
;
S
= p
;
return OK
;
}
Status
Pop(LinkStack
&S
, SElemType
&e
){
if (S
== nullptr) return ERROR
;
StackNode
*p
= nullptr;
e
= S
->data
;
p
= S
;
S
= S
->next
;
delete p
;
return OK
;
}
SElemType
GetTop(LinkStack S
){
if (S
!= nullptr){
return S
->data
;
}
}
Status
DestroyStack(LinkStack
&S
){
StackNode
*p
= nullptr;
while (S
){
p
= S
;
S
= S
->next
;
delete p
;
}
return OK
;
}
Status
ClearStack(LinkStack
&S
StackNode
*p
, *q
;
p
= S
;
while (p
){
q
= p
->next
;
delete p
;
p
= q
;
}
S
= nullptr;
return OK
;
}
int StackLength(LinkStack S
){
int length
= 0;
while (S
){
length
++;
S
= S
->next
;
}
return length
;
}
int main() {
LinkStack S
;
SElemType e
;
InitStack(S
);
Push(S
, 11);
Push(S
, 22);
Push(S
, 33);
int length
= StackLength(S
);
cout
<< "链栈长度: " << length
<< endl
;
Status ret
= StackEmpty(S
);
if (ret
== TRUE
) cout
<< "空栈" << endl
;
else cout
<< "非空" << endl
;
SElemType temp
= GetTop(S
);
cout
<< "栈顶元素: " << temp
<< endl
;
Pop(S
, e
);
cout
<< "弹出的栈顶元素: " << e
<< endl
;
ClearStack(S
);
ret
= StackEmpty(S
);
if (ret
== TRUE
) cout
<< "空栈" << endl
;
else cout
<< "非空" << endl
;
DestroyStack(S
);
return 0;
}
转载请注明原文地址: https://lol.8miu.com/read-38682.html