LinkStack 链表实现栈 C&C++
#include<stdio.h>
#include<iostream>
typedef struct LinkStack {
int data;
LinkStack* next;
void Init(LinkStack*& Ls);
void Pop(LinkStack*& Ls, int& data);
void Push(LinkStack*&Ls,int data);
}*LinStackP;
void LinkStack::Init(LinkStack*&Ls) {
Ls = NULL;
}
void LinkStack::Pop(LinkStack*& Ls, int& data) {
if (!Ls)std::cout << "error";//当LS==NULL时不可以POP
LinStackP s;
s = (LinStackP)malloc(sizeof(LinkStack));
data = Ls->data;
s = Ls;
Ls = Ls->next;
free(s);
}
void LinkStack::Push(LinkStack*& Ls, int data) {
LinStackP s;
s = (LinStackP)malloc(sizeof(LinkStack));
s->data = data;
s->next = Ls;
Ls = s;
}
int main() {
//for test
LinStackP test1;
test1->Init(test1);//初始化
test1->Push(test1, 2019);
int testout;
test1->Pop(test1, testout);
std::cout << testout << std::endl;
system("pause");
}
转载:https://blog.csdn.net/weixin_43943281/article/details/101466289
查看评论