2024年10月15日总结

szxworld / 2024-10-19 / 原文

今天在课余之间理了理栈与队列的知识,以下是一个利用栈和队列数据存取差异制作的判断回文序列的c语言程序

include<stdio.h>

include<stdlib.h>

include<math.h>

define MAXSIZE 100 //能存储的字符串最大达到100

define OK 0

define ERROR -1

typedef int Status;
typedef struct SqStack {
char* top; //定义栈顶指针
char* base; //定义栈底指针
int stacksize; //栈可用的最大容量
}SqStack;
//定义顺序队列的存储结构
typedef struct SqQueue {
char* base; //存储空间的基地址
int front; //头指针
int rear; //尾指针
}SqQueue;

Status Creat_Stack(SqStack* S)
{
S->base = (char)malloc(sizeof(char) * MAXSIZE); //给栈动态分配内存,所分配的内存为100
if (!S->base)
{
printf("创建失败!\n");
exit(OVERFLOW);
}
S->top = S->base;
S->stacksize = MAXSIZE;
return OK;
}
int IsEmpty_S(SqStack
S)
{
if (S->top == S->base)
return 1; //若为空,则返回1
return 0;
}
Status Push(SqStack* S, char a)
{
if (S->top - S->base == S->stacksize) //若栈满
{
printf("栈已满,无法插入!\n");
return ERROR;
}
S->top++ = a; //将a压入栈中
return OK;
}
Status Pop(SqStack
S, char* a) //a为弹出的元素
{
int x = IsEmpty_S(S);
if (x) //判断栈是否为空
{
printf("栈为空,无法删除!\n");
return ERROR;
}
a = --S->top; //注意!!!!
return OK;
}
Status destory_S(SqStack
S)
{
free(S->base);
S->base = NULL;
S->top = NULL;
S->stacksize = 0;
return OK;
}
Status Creat_Q(SqQueue
Q)
{
Q->base = (char)malloc(sizeof(char) * MAXSIZE);
if (!Q->base)
{
printf("创建失败!\n");
exit(OVERFLOW); //若内存分配失败,则返回
}
Q->front = 0;
Q->rear = 0;
return OK;
}
Status In_Queue(SqQueue
Q, char a)
{
if ((Q->rear + 1) % MAXSIZE == Q->front) //尾指针在循环意义上加一后等于头指针,队满
{
printf("队列已满,无法入队!\n");
return ERROR;
}
Q->base[Q->rear] = a;
Q->rear = (Q->rear + 1) % MAXSIZE; //尾指针加一
return OK;
}
int IsEmpty_Q(SqQueue* Q)
{
if (Q->front == Q->rear)
return 1; //为空则返回1
return 0;
}
Status Out_Queue(SqQueue* Q, char* a) //用a记录出队的元素值
{
if (IsEmpty_Q(Q) == 1)
{
printf("队列为空,无法出队!\n");
return ERROR;
}
*a = Q->base[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return OK;
}

//顺序队列的销毁
Status destory_Q(SqQueue* Q)
{
free(Q->base);
Q->base = NULL;
Q->fr