順序棧、鏈棧已及佇列的實現
阿新 • • 發佈:2018-12-01
#include<iostream>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define QUEUE_INIT_SIZE 100
#define MAXSIZE 100
typedef int SElemType;
typedef int QElemType;
//typedef int Status;
enum Status{ERROR = 0,OK = 1};
typedef struct {
SElemType * base;
SElemType * top;
int stacksize;
}SqStack;
// =======順序棧=====
Status SInitStack(SqStack &S) {
S.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!S.base)exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
Status SGetTop(SqStack S, SElemType&e) {
if (S.top == S.base)return ERROR;
e = *(S.top - 1);
return OK;
}
Status SPush(SqStack &S, SElemType e) {
if (S.top - S.base >= S.stacksize) {
S.base = (SElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
if (!S.base)exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S .top++ = e;
return OK;
}
Status SPop(SqStack &S, SElemType &e) {
if (S.top == S.base)return ERROR;
e = *--S.top;
return OK;
}
//==========鏈式棧========
typedef struct LElem {
SElemType data;
LElem *next;
};
typedef struct LStack {
LElem *top;
int stacksize;
};
Status LInitStack(LStack &S) {
S.stacksize = 0;
S.top = NULL;
return OK;
}
Status LPush(LStack &S, SElemType &e) {
LElem *p = (LElem *)malloc(sizeof(LElem));
if(!p)
exit(OVERFLOW);
p->data = e;
p->next = S.top;
S.top = p;
S.stacksize++;
return OK;
}
Status LPop(LStack &S, SElemType &e) {
LElem *p = S.top;
S.top = S.top->next;
e = p->data;
free(p);
S.stacksize--;
return OK;
}
Status LGetTop(LStack &S, SElemType &e) {
if (!S.top) {
printf("棧已空\n");
return ERROR;
}
e = S.top->data;
return OK;
}
//============順序佇列============
typedef struct SqQ {
int front;
int rear;
int len;
QElemType E[QUEUE_INIT_SIZE];
};
Status Init_SqQ(SqQ &Q) {
Q.front = 0;
Q.rear = 0;
Q.len = 0;
return OK;
}
Status SEnQ(SqQ &Q,QElemType e) {
if (Q.len == QUEUE_INIT_SIZE) {
printf("佇列已滿\n");
return ERROR;
}
Q.E[Q.rear] = e;
Q.rear++;
Q.len++;
return OK;
}
Status SGetQ(SqQ &Q, QElemType &e) {
if (Q.front == Q.rear) {
printf("佇列為空\n");
return OK;
}
e = Q.E[Q.front];
return OK;
}
Status SDeQ(SqQ &Q, QElemType &e) {
if (Q.front == Q.rear) {
printf("佇列為空\n");
return ERROR;
}
e = Q.E[Q.front];
Q.front++;
Q.len--;
return OK;
}
//==========鏈佇列==========
typedef struct QNode {
QElemType data;
struct QNode * next;
}QNode,* QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q) {
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front)exit(OVERFLOW);
Q.front->next = NULL;
return OK;
}
Status EnQueue(LinkQueue &Q, QElemType e) {
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (!p)exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
Status DeQueue(LinkQueue &Q, QElemType e) {
if (Q.front == Q.rear)return ERROR;
QueuePtr p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p)Q.rear = Q.front;
free(p);
return OK;
}
Status GetHead(LinkQueue Q, QElemType &e) {
if (Q.front == Q.rear)return ERROR;
e = Q.front->next->data;
return OK;
}
//=========迴圈佇列========
typedef struct {
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q) {
Q.base = (QElemType*)malloc(MAXSIZE * sizeof(QElemType));
if (!Q.base)exit(OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
Status EnQueue(SqQueue &Q, QElemType e) {
if (Q.rear == Q.front+MAXSIZE)return ERROR;
Q.base[Q.rear%MAXSIZE] = e;
Q.rear++;
return OK;
}
Status DeQueue(SqQueue &Q, QElemType &e) {
if (Q.front == Q.rear)return ERROR;
e = Q.base[Q.front%MAXSIZE];
Q.front++;
return OK;
}
Status GetHead(SqQueue Q, QElemType &e) {
if (Q.rear == Q.front)return ERROR;
e = Q.base[Q.front%MAXSIZE];
return OK;
}
int GetLength(SqQueue Q) {
return Q.rear - Q.front;
}
//指標迴圈佇列
typedef struct {
QElemType *base;
QElemType *front;
QElemType *rear;
int len;
}pQueue;
Status InitQueue(pQueue &Q) {
Q.base = (QElemType*)malloc(MAXSIZE * sizeof(QElemType));
if (!Q.base)exit(OVERFLOW);
Q.front = Q.rear = Q.base;
Q.len = 0;
return OK;
}
Status EnQueue(pQueue &Q, QElemType e) {
if (Q.len == MAXSIZE)return ERROR;
*Q.rear = e;
Q.rear = Q.base + (Q.rear - Q.base+1) % MAXSIZE;
Q.len++;
return OK;
}
Status DeQueue(pQueue &Q, QElemType e) {
if (Q.len == 0)return ERROR;
Q.front = Q.base + (Q.front - Q.base + 1) % MAXSIZE;
return OK;
}
Status GetHead(pQueue &Q, QElemType &e) {
if (Q.len == 0)return ERROR;
e = *Q.front;
return OK;
}
int main() {
//順序棧
/*printf("=========順序棧============\n");
SqStack s1;
SElemType e;
SInitStack(s1);
for (int i = 0; i < 3; i++) {
scanf("%d", &e);
SPush(s1, e);
}
printf("stacksize: %d\n", s1.stacksize);
for (int i = 0; i < 3; i++) {
SGetTop(s1, e);
printf("%d ", e);
SPop(s1, e);
}
printf("\n");
//鏈式棧
printf("==========鏈棧==========\n");
LStack s2;
LInitStack(s2);
for (int i = 0; i < 3; i++) {
scanf("%d", &e);
LPush(s2, e);
}
printf("stacksize: %d\n", s2.stacksize);
for (int i = 0; i < 3; i++) {
LGetTop(s2, e);
printf("%d ", e);
LPop(s2, e);
}
printf("\n");*/
//========順序佇列=========
QElemType t;
/*printf("============順序佇列==========\n");
SqQ q1;
Init_SqQ(q1);
for (int i = 0; i < 3; i++) {
scanf("%d", &t);
SEnQ(q1, t);
}
printf("len: %d\n",q1.len);
for (int i = 0; i < 3; i++) {
SGetQ(q1, t);
printf("%d ", t);
SDeQ(q1, t);
}
printf("\n");
//=======鏈式佇列==========
printf("========鏈式佇列=======\n");
LinkQueue q2;
InitQueue(q2);
for (int i = 0; i < 3; i++) {
scanf("%d", &t);
EnQueue(q2, t);
}
for (int i = 0; i < 3; i++) {
GetHead(q2, t);
printf("%d ", t);
DeQueue(q2, t);
}
printf("\n");
//=========迴圈佇列==========
printf("==========迴圈佇列=========\n");
SqQueue q3;
InitQueue(q3);
for (int i = 0; i < 3; i++) {
scanf("%d", &t);
EnQueue(q3, t);
}
printf("len: %d\n", GetLength(q3));
for (int i = 0; i < 3; i++) {
GetHead(q3, t);
printf("%d ", t);
DeQueue(q3, t);
}
printf("\n");*/
printf("========指標迴圈佇列========\n");
pQueue q4;
InitQueue(q4);
for (int i = 0; i < 3; i++) {
scanf("%d", &t);
EnQueue(q4, t);
}
printf("len: %d\n", q4.len);
for (int i = 0; i < 3; i++) {
GetHead(q4, t);
printf("%d ", t);
DeQueue(q4, t);
}
printf("\n");
return 0;
}