#include <stdlib.h>
#include <iostream>
using namespace std;
typedef int ElemType;
#define false 0
#define true 1
#define MaxSize 10
//have a head node
//带头结点链栈,创建,初始化,判空,判满,销毁,增加,减少,查找
//Create
typedef struct LNode {
ElemType data;
int count;
struct LNode* top;
}LNode, *LinkStack;
//Initate
bool InitLinkStack(LinkStack &S) {
S = (LinkStack)malloc(sizeof(LNode));
if (S == NULL)
return false;
S->top = NULL;
S->count = 0;
return true;
}
//IsEmpty
bool IsEmpty(LinkStack& S) {
if (S->top == NULL)
return true;
return false;
}
//IsFull
bool IsFull(LinkStack& S) {
if (S->count == MaxSize)
return true;
return false;
}
//Pop/Delete
bool Pop(LinkStack& S, ElemType& x) {
if (S->count == 0)
return false;
LNode* p = S->top;
if (S->top == NULL)
return false;
S->top = p->top;
x = p->data;
free(p);
S->count--;
return true;
}
//Destroy
void DestroyLinkStack(LinkStack& S) {
int x = 0;
while (S->top != NULL)
Pop(S,x);
free(S);
S = NULL;
}
//Push/Add
bool Push(LinkStack& S,ElemType x) {
if (S->count == MaxSize)
return false;
LNode* p = (LNode*)malloc(sizeof(LNode));
if (p == NULL)
return false;
p->top = S->top;
S->top = p;
p->data = x;
S->count++;
return true;
}
bool GetElem(LinkStack &S,ElemType &x) {
if (S->count == 0)
return false;
x = S->top->data;
return true;
}
int main()
{
LinkStack S;
InitLinkStack(S);
if (IsEmpty(S))
std::cout << "Empty" << endl;
std::cout << endl;
//Push Element
int a[] = { 1,2,3,4,5,6,7,8,9,10 };
int b[10];
int x;
for (int i = 0; i <= 9; i++) {
Push(S, a[i]);
std::cout << "PushCount:" << S->count << " ";
}
std::cout << endl;
if (IsFull(S))
std::cout << "Full" << endl;
std::cout << endl;
//Pop Element
for (int j = 0; j <= 9; j++) {
Pop(S, b[j]);
std::cout << "b[" << j << "]=" << b[j]<<" ";
std::cout << "PopCount:" << S->count << " ";
}
std::cout << endl;
Push(S, b[1]);
if (GetElem(S, x)) {
std::cout << x;
}
DestroyLinkStack(S);
return 0;
}
结果见上图
#include <stdlib.h>
#include <iostream>
#define MaxSize 10
#define true 1
#define false 0
typedef int ElemType;
using namespace std;
//Without a Head
//创建,初始化,判空,判满,销毁,增加,删除,查找,求表长
//alt+shift+;更改所有同名变量
//Fn+右边数字0键或者关闭NumLK再按一下数字0
//Create
typedef struct LNode {
ElemType data;
struct LNode *next;
}LNode,*LinkStack;
//length
int Length(LinkStack& S) {
int i = 0;
if (S == NULL)
return 0;
LNode* p = S;
while (p != NULL) {
p = p->next;
i++;
}
return i;
}
//Init
void InitLinkStack(LinkStack& S) {
S = NULL;
}
//IsFull
bool IsFullLinkStack(LinkStack& S) {
if (Length(S) == MaxSize)
return true;
return false;
}
//IsEmpty
bool IsEmptyLinkStack(LinkStack& S) {
if (S == NULL)
return true;
return false;
}
//Add
bool Push(LinkStack& S,ElemType x) {
if (IsFullLinkStack(S))
return false;
LNode* p = (LNode*)malloc(sizeof(LNode));
if (p == NULL)
return false;
p->data = x;
if (S == NULL)
S = p;
p->next = S->next;
S->next = p;
return true;
}
//Delete
bool Pop(LinkStack& S, ElemType& x) {
if (IsEmptyLinkStack(S))
return false;
if (S->next == NULL) {
x = S->data;
free(S);
S == NULL;
}
LNode* p = S->next;
x = S->data;
S->next = p->next;
free(p);
return true;
}
//Get
bool GetElem(LinkStack& S,ElemType& x) {
if (IsEmptyLinkStack(S))
return false;
x = S->data;
return true;
}
//Destroy
void Destroy(LinkStack& S) {
int x;
if (S == NULL);
while (S != NULL)
Pop(S, x);
}
上图为不带头结点的链栈创建初始化判空判满销毁增删查