#include <iostream>
using namespace std;
#define Elemtype int
#define maxSize 100
typedef struct LSNode{
Elemtype data;
struct LSNode *next;
}linkStackNode, *linkStack;
bool initLinkStack(linkStack &S) {
S = new linkStackNode;
if(S == NULL) return false;
S->next = NULL;
return true;
}
bool isEmptyLinkStack(linkStack S) {
if(S->next == NULL) return true;
else return false;
}
bool pushLinkStack(linkStack &S, Elemtype e) {
linkStackNode *newp;
newp = new linkStackNode;
newp->data = e;
newp->next = S->next;
S->next = newp;
return true;
}
bool popLinkStack(linkStack &S, Elemtype &e) {
if(isEmptyLinkStack(S)) return false;
linkStackNode *p;
p = S->next;
e = p->data;
S->next = p->next;
delete p;
return true;
}
bool destroyLinkStack(linkStack &S) {
while(S->next) {
linkStackNode *p;
p = S->next;
S->next = p->next;
delete p;
}
return true;
}
int main() {
linkStack S;
initLinkStack(S);
int a[] = {1,2,3,4,5};
int length = 5;
while(length --) {
pushLinkStack(S, a[length]);
}
Elemtype e;
while(!isEmptyLinkStack(S)) {
popLinkStack(S, e);
cout << e << " ";
}
cout << endl;
return 0;
}
数据结构链式栈C语言
最新推荐文章于 2024-08-20 15:59:15 发布