顺序栈的实现
#include<iostream>
#define MaxSize 20
using namespace std;
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 + 1 == MaxSize) 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];
S.top--;
return true;
}
bool GetTop(SqStack S, int& x) {
if (S.top == -1) return false;
x = S.data[S.top];
return true;
}
void Destroy(SqStack& S) {
S.top = -1;
}
int main() {
int N = 24;
SqStack S;
InitStack(S);
int x;
for (int i = 0; i < N; i++) {
x = i * 10 + 7;
if (Push(S, x)) {
cout << "入栈成功,元素为:"<<x << endl;
}
else {
cout << "入栈失败"<<endl;
}
}
if (GetTop(S, x)) {
cout << "此时栈顶元素为" << x << endl;
}
else {
if (StackEmpty(S)) {
cout << "此时栈为空" << endl;
}
}
for (int i = 0; i < N; i++) {
if (Pop(S, x)) {
cout << "出栈成功,出栈元素为:" << x << endl;
}
else {
cout << "出栈失败" << endl;
}
}
if (GetTop(S, x)) {
cout << "此时栈顶元素为" << x << endl;
}
else {
if (StackEmpty(S)) {
cout << "此时栈为空" << endl;
}
}
Destroy(S);
return 0;
}
链栈的实现
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct LinkNode{
int data;
struct LinkNode* next;
}*LiStack;
void InitStack(LiStack& S) {
S->next = NULL;
}
bool EmptyStack(LiStack S) {
if (S->next == NULL) return true;
return false;
}
void Push(LiStack& S, int x) {
S->data = x;
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
p->next = S;
S = p;
}
bool Pop(LiStack& S, int& x) {
if (S->next == NULL) return false;
LinkNode*p = S->next;
x = p->data;
S->next = p->next;
free(p);
return true;
}
bool GetTop(LiStack S, int& x) {
if (S->next == NULL) return false;
x = S->next->data;
}
void Destroy(LiStack& S) {
while (S) {
LinkNode* p = S;
S = S->next;
free(p);
}
}
int main() {
LinkNode* S=(LinkNode*)malloc(sizeof(LinkNode));
InitStack(S);
int x;
int N = 12;
if (EmptyStack(S)) cout << "栈为空" << endl;
else cout << "栈非空" << endl;
for (int i = 0; i < N; i++) {
x = i * 12 * 7;
Push(S, x);
cout << "入栈元素为:" << x<<endl;
}
if (EmptyStack(S)) cout << "栈为空" << endl;
else cout << "栈非空" << endl;
if (GetTop(S, x)) cout << "栈顶元素为:" << x << endl;
else cout << "栈为空" << endl;
for (int i = 0; i < N + 5; i++) {
if (Pop(S, x)) {
cout << "出栈元素为:" << x << endl;
}
else{
cout << "栈为空,出栈失败" << endl;
}
}
if (GetTop(S, x)) cout << "栈顶元素为:" << x << endl;
else cout << "栈为空" << endl;
Destroy(S);
return 0;
}
循环队列
#include<iostream>
#define MaxSize 12
using namespace std;
typedef struct {
int data[MaxSize];
int front, rear;
}SqQueue;
void InitQueue(SqQueue& Q) {
Q.front = Q.rear = 0;
}
bool isEmpty(SqQueue Q) {
if (Q.front == Q.rear) return true;
return false;
}
bool EnQueue(SqQueue& Q, int x) {
if ((Q.rear + 1) % MaxSize == Q.front) return false;
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
bool DeQueue(SqQueue& Q, int &x) {
if (isEmpty(Q)) return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
int main() {
SqQueue Q;
InitQueue(Q);
int N = 15;
int x;
for (int i = 0; i < N; i++) {
x = i * 18 + 12;
if (EnQueue(Q, x)) cout << "入队成功,元素为:" << x<<endl;
else cout << "入队失败"<<endl;
}
for (int i = 0; i < N -8; i++) {
if (DeQueue(Q, x)) cout << "出队成功,元素为:" << x << endl;
else cout << "出队失败" << endl;
}
for (int i = 0; i < N-3; i++) {
x = i ;
if (EnQueue(Q, x)) cout << "入队成功,元素为:" << x << endl;
else cout << "入队失败" << endl;
}
for (int i = 0; i < N ; i++) {
if (DeQueue(Q, x)) cout << "出队成功,元素为:" << x << endl;
else cout << "出队失败" << endl;
}
return 0;
}
队列的链式存储
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct LinkNode {
int data;
struct LinkNode* next;
}LinkNode;
typedef struct {
LinkNode* front, *rear;
}LinkQueue;
void InitQueue(LinkQueue& Q) {
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
bool IsEmpty(LinkQueue Q) {
if (Q.front==Q.rear) return true;
return false;
}
void EnQueue(LinkQueue& Q,int x) {
Q.rear->data = x;
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
}
bool DeQueue(LinkQueue& Q, int& x) {
if (Q.front == Q.rear) return false;
x = Q.front->data;
LinkNode* p = Q.front;
Q.front = Q.front->next;
free(p);
return true;
}
int N = 15;
int main() {
LinkQueue Q;
InitQueue(Q);
int x;
for (int i = 0; i < N; i++) {
x = 3 * i + 13;
cout << "入队元素:" << x << endl;
EnQueue(Q, x);
}
for (int i = 0; i < N; i++) {
DeQueue(Q, x);
cout << "出队元素:" << x << endl;
}
return 0;
}