栈的顺序存储
#include <iostream>
using namespace std;
typedef char ElemType;
const int MaxSize = 50;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack *&s);
void DestroyStack(SqStack *&s);
void ClearStack(SqStack *&s);
bool StackEmpty(SqStack *s);
bool Push(SqStack *&s, ElemType e);
bool Pop(SqStack *&s, ElemType &e);
bool Top(SqStack *s, ElemType &e);
bool symmetry(SqStack *&s, ElemType str[]);
int main(void)
{
SqStack *s;
InitStack(s);
if (StackEmpty(s))
cout << "栈空!" << endl;
ElemType e;
while (cin >> e)
{
if (e == '0')
break;
Push(s, e);
}
while (!StackEmpty(s))
{
Pop(s, e);
cout << e << ' ';
}
cout << endl;
DestroyStack(s);/*
char str[100] = "chqhc";
if (symmetry(s, str))
cout << "对称!" << endl;
else
cout << "不对称!" << endl;
if (StackEmpty(s))
cout << "栈空!" << endl;
DestroyStack(s);*/
return 0;
}
void InitStack(SqStack *&s)
{
s = new SqStack;
s->top = -1;
}
void DestroyStack(SqStack *&s)
{
delete s;
s = NULL;
}
void ClearStack(SqStack *&s)
{
ElemType e;
while (!StackEmpty(s))
Pop(s, e);
}
bool StackEmpty(SqStack *s)
{
return (s->top == -1);
}
bool Push(SqStack *&s, ElemType e)
{
if (s->top == MaxSize-1)
return false;
s->top++;
s->data[s->top] = e;
return true;
}
bool Pop(SqStack *&s, ElemType &e)
{
if (s->top == -1)
return false;
e = s->data[s->top];
s->top--;
return true;
}
bool Top(SqStack *s, ElemType &e)
{
if (s->top == -1)
return false;
e = s->data[s->top];
return true;
}
bool symmetry(SqStack *&s, ElemType str[])
{
ElemType e;
bool status = true;
for (int i = 0; str[i] != '\0'; i++)
Push(s, str[i]);
for (int i = 0; str[i] != '\0'; i++)
{
Pop(s, e);
if (e != str[i])
{
status = false;
break;
}
}
ClearStack(s);
return status;
}
栈的链式存储
#include <iostream>
using namespace std;
typedef char ElemType;
const int MaxSize = 100;
typedef struct linknode
{
ElemType data;
struct linknode *next;
}LinkStack;
void InitStack(LinkStack *&s);
void DestroyStack(LinkStack *&s);
void ClearStack(LinkStack *&s);
bool StackEmpty(LinkStack *s);
void Push(LinkStack *&s, ElemType e);
bool Pop(LinkStack *&s, ElemType &e);
bool Top(LinkStack *s, ElemType &e);
int main(void)
{
LinkStack *s;
InitStack(s);
if (StackEmpty(s))
cout << "栈空!" << endl;
ElemType e;
while (cin >> e)
{
if (e == '0')
break;
Push(s, e);
}
while (!StackEmpty(s))
{
Pop(s, e);
cout << e << ' ';
}
cout << endl;
ClearStack(s);
if (StackEmpty(s))
cout << "栈空!" << endl;
DestroyStack(s);
return 0;
}
void InitStack(LinkStack *&s)
{
s = new LinkStack;
s->next = NULL;
}
void DestroyStack(LinkStack *&s)
{
LinkStack *pre = s, *p = s->next;
while (p != NULL)
{
delete pre;
pre = p;
p = p->next;
}
delete pre;
}
void ClearStack(LinkStack *&s)
{
LinkStack *p = s->next, *q;
s->next = NULL;
while (p != NULL)
{
q = p->next;
delete p;
p = q;
}
}
bool StackEmpty(LinkStack *s)
{
return (s->next == NULL);
}
void Push(LinkStack *&s, ElemType e)
{
LinkStack *q = new LinkStack;
q->data = e;
q->next = s->next;
s->next = q;
}
bool Pop(LinkStack *&s, ElemType &e)
{
if (s->next == NULL)
return false;
else
{
LinkStack *q = s->next;
e = q->data;
s->next = q->next;
delete q;
q = NULL;
return true;
}
}
bool Top(LinkStack *s, ElemType &e)
{
if (s->next == NULL)
return false;
else
{
e = s->next->data;
return true;
}
}
共享栈
#include <iostream>
using namespace std;
typedef char ElemType;
const int MaxSize = 100;
typedef struct
{
ElemType data[MaxSize];
int top1, top2;
}DStack;
void InitStack(DStack *&s);
void DestroyStack(DStack *&s);
void ClearStack(DStack *&s, int i);
bool StackEmpty(DStack *s, int i);
bool Push(DStack *&s, ElemType e, int i);
bool Pop(DStack *&s, ElemType &e, int i);
bool Top(DStack *s, ElemType &e, int i);
int main(void)
{
DStack *s;
InitStack(s);
ElemType e;
if (StackEmpty(s, 1))
cout << "栈1为空!" << endl;
if (StackEmpty(s, 2))
cout << "栈2为空!" << endl;
while (cin >> e)
{
if (e == '0')
break;
Push(s, e, 1);
}
while (cin >> e)
{
if (e == '0')
break;
Push(s, e, 2);
}
while (!StackEmpty(s, 2))
{
Pop(s, e, 2);
cout << e << ' ';
}
cout << endl;
while (!StackEmpty(s, 1))
{
Pop(s, e, 1);
cout << e << ' ';
}
cout << endl;
if (StackEmpty(s, 1))
cout << "栈1为空!" << endl;
if (StackEmpty(s, 2))
cout << "栈2为空!" << endl;
DestroyStack(s);
return 0;
}
void InitStack(DStack *&s)
{
s = new DStack;
s->top1 = -1;
s->top2 = MaxSize;
}
void DestroyStack(DStack *&s)
{
delete s;
s = NULL;
}
bool StackEmpty(DStack *s, int i)
{
if (i == 1)
{
if (s->top1 == -1)
return true;
else
return false;
}
else
{
if (s->top2 == MaxSize)
return true;
else
return false;
}
}
bool Push(DStack *&s, ElemType e, int i)
{
if (i == 1)
{
if (s->top1 == s->top2-1)
return false;
s->top1++;
s->data[s->top1] = e;
return true;
}
else
{
if (s->top2 == s->top1+1)
return false;
s->top2--;
s->data[s->top2] = e;
return true;
}
}
bool Pop(DStack *&s, ElemType &e, int i)
{
if (StackEmpty(s, i))
return false;
if (i == 1)
{
e = s->data[s->top1];
s->top1--;
}
else
{
e = s->data[s->top2];
s->top2++;
}
return true;
}
bool Top(DStack *s, ElemType &e, int i)
{
if (StackEmpty(s, i))
return false;
if (i == 1)
e = s->data[s->top1];
else
e = s->data[s->top2];
return true;
}