数据结构(严蔚敏版)配套实验报告册--栈与队列基本操作
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
#define STACK_INT_SIZE 100
#define STACKINCREMENT 10
//动态顺序栈
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
} SqStack;
Status InitStack(SqStack &S)
{ //初始化一个空栈
S.base = (ElemType *)malloc(STACK_INT_SIZE * sizeof(ElemType));
if (!S.base)
exit(OVERFLOW);
S.top = S.base; //表示栈空
S.stacksize = STACK_INT_SIZE;
return OK;
}
Status Push(SqStack &S, ElemType e)
{ //插入元素e为新的栈顶元素
if (S.top - S.base >= S.stacksize)
{
S.base = (ElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(ElemType));
if (!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize; //栈已满所以追加存储空间
S.stacksize += STACKINCREMENT;
}
*(S.top++) = e;
printf("S.base[%d]=%c\n", S.top - S.base, e);
return OK;
}
Status Pop(SqStack &S, ElemType &e)
{ //删除栈顶元素,用e返回其值
if (S.top == S.base)
return ERROR;
e = *(S.top--); //先赋值元素,再将栈顶指针减一
return OK;
}
Status StackEmpty(SqStack S)
{
if (S.top == S.base)
return TRUE;
else
return FALSE;
}
ElemType GetTop(SqStack S)
{ //得到栈顶元素
// ElemType e;
if (S.top == S.base)
exit(ERROR); // 不使用return ERROR 而使用exti()可以不论main()的返回值
// e = *(S.top - 1);
else
return *(S.top - 1);
}
//队列链式存储结构
typedef struct QNode
{
ElemType 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 DestroyQueue(LinkQueue &Q)
{ if(!Q.front)
return FALSE;
while (Q.front)
{
Q.rear = Q.front->next;
free(Q.front);
Q.front = Q.rear;
}
return OK;
}
Status EnQueue(LinkQueue &Q, ElemType e)
{//插入元素e为新的队尾元素
QueuePtr p;
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, ElemType &e)
{//删除Q的队头元素,并用e值返回
QueuePtr p;
if (Q.front == Q.rear)
return ERROR;
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 LengthQueue(LinkQueue Q)
{//返回队列的长度
int len = 0;
QueuePtr p;
p = Q.front->next;
while (p)
{
p = p->next;
len++;
}
return len;
}
ElemType GetheadQueue(LinkQueue Q)
{
if (Q.front == Q.rear)
exit(ERROR);
else
return Q.front->next->data;
}
Status TraverLinkQueue(LinkQueue &Q)
{
QueuePtr p;
p = Q.front->next;
while (p)
{
printf("%2c", p->data);
p = p->next;
}
printf("\n");
return OK;
}
int main()
{
SqStack S;
LinkQueue Q;
ElemType ch, e1, e2;
int state;
InitStack(S);
InitQueue(Q);
printf("input a string edding by #:");
scanf("%c", &ch);
while (ch != '#')
{
Push(S, ch);
EnQueue(Q, ch);
scanf("%c", &ch);
}
// printf("\nThe Queue Head is:%c", GetheadQueue(Q));
// printf("\nThe Queuelength is:%d",LengthQueue(Q));
printf("\nThe Queue is:");
TraverLinkQueue(Q);
printf("\n");
state = TRUE;
while (!StackEmpty(S) && state)
{
if (GetTop(S) == GetheadQueue(Q))
{
Pop(S, e1);
DeQueue(Q, e2);
}
else
state = FALSE;
}
if (state)
printf("This string is HuiWen!\n");
else
printf("This string is not HuiWen!\n");
}
本次代码时间较紧没有太多注释,望读者认真阅读,有错误请指正。