顺序栈
#include <iostream>
using namespace std;
#define maxSize 10
typedef struct {
int data[maxSize];
int top;
}SqStack;
void InitStack(SqStack &S) {
S.top = -1;
}
bool StackEmpty(SqStack S) {
if (S.top == -1) return true;
return false;
}
bool Push(SqStack &S,int x) {
if (S.top == maxSize-1) return false;
S.data[++S.top] = x;
return true;
}
bool Pop(SqStack &S,int &x) {
if (S.top == -1) return false;
x = S.data[S.top--];
return true;
}
bool GetTop(SqStack S,int &x) {
if (S.top == -1) return false;
x = S.data[S.top];
return true;
}
void PrintStack(SqStack S) {
if (S.top == -1) {
cout<<"顺序栈为空!"<<endl;
} else {
for (int i = 0; i < S.top + 1; i++) {
cout<<S.data[i]<<" ";
}
cout<<endl;
}
}
int main() {
SqStack S;
InitStack(S);
Push(S,1);
Push(S,3);
Push(S,2);
PrintStack(S);
int x;
GetTop(S,x);
cout<<"栈顶元素为:"<<x<<endl;
Pop(S,x);
cout<<"弹出元素为:"<<x<<endl;
Pop(S,x);
cout<<"弹出元素为:"<<x<<endl;
system("pause");
return 0;
}
链栈
#include <iostream>
using namespace std;
typedef struct Linknode{
int data;
struct Linknode *next;
}Linknode,*LinkStack;
void InitStack(LinkStack &S) {
S = (LinkStack)malloc(sizeof(Linknode));
S->next = NULL;
}
bool StackEmpty(LinkStack S) {
if (S->next == NULL) return true;
return false;
}
bool Push(LinkStack &S,int x) {
Linknode *p = (Linknode *)malloc(sizeof(Linknode));
p->data = x;
p->next = S->next;
S->next = p;
return true;
}
bool Pop(LinkStack &S,int &x) {
if (S->next == NULL) return false;
Linknode *p;
x = S->next->data;
p = S->next;
S->next = p->next;
free(p);
return true;
}
bool GetTop(LinkStack S,int &x) {
if (S->next == NULL) return false;
x = S->next->data;
return true;
}
void DestroyStack(LinkStack &S) {
Linknode *p;
while(S->next != NULL) {
p = S->next;
S->next = p->next;
free(p);
}
free(S);
}
void PrintStack(LinkStack S) {
if (S->next == NULL) {
cout<<"链栈为空!"<<endl;
} else {
while(S->next != NULL) {
cout<<S->next->data<<" ";
S = S->next;
}
cout<<endl;
}
}
void InitStack2(LinkStack &S) {
S = NULL;
}
bool StackEmpty2(LinkStack S) {
if (S == NULL) return true;
return false;
}
bool Push2(LinkStack &S,int x) {
Linknode *p = (Linknode *)malloc(sizeof(Linknode));
p->data = x;
p->next = S;
S = p;
return true;
}
bool Pop2(LinkStack &S,int &x) {
if (S == NULL) return false;
Linknode *p;
x = S->data;
p = S;
S = p->next;
free(p);
return true;
}
bool GetTop2(LinkStack S,int &x) {
if (S == NULL) return false;
x = S->next->data;
return true;
}
void DestroyStack2(LinkStack &S) {
Linknode *p;
while(S != NULL) {
p = S->next;
S = p->next;
free(p);
}
free(S);
}
void PrintStack2(LinkStack S) {
if (S == NULL) {
cout<<"链栈为空!"<<endl;
} else {
while(S!= NULL) {
cout<<S->data<<" ";
S = S->next;
}
cout<<endl;
}
}
int main() {
LinkStack S;
InitStack2(S);
if (StackEmpty2(S)) cout<<"链栈为空!"<<endl;
else PrintStack2(S);
Push2(S,1);
Push2(S,3);
Push2(S,2);
if (StackEmpty2(S)) cout<<"链栈为空!"<<endl;
else PrintStack2(S);
int x;
for (int i = 0; i < 5; i++) {
if (Pop2(S,x)) {
cout<<"弹出栈顶元素为:"<<x<<endl;
} else {
cout<<"弹出栈顶元素失败!"<<endl;
}
}
DestroyStack2(S);
system("pause");
return 0;
}
带头结点
不带头结点