8月26日
顺序栈
头文件
#ifndef __STACK_H__
#define __STACK_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 7
typedef struct
{
int data[MAX]; //存储数据的数组
int top; //记录可以出栈的位置
}seq_stack,*stack_p;
//创建栈
stack_p create_seq_stack();
//压入栈
void push_stack(stack_p S,int value);
//判空
break;
int empty_stack(stack_p S);
//判满
int full_stack(stack_p S);
//弹栈/出栈,需要返回出栈的元素
int pop_stack(stack_p S);
//输出栈
void show(stack_p S);
#endif
自定义函数
#include "stack.h"
stack_p create_seq_stack()
{
stack_p S = (stack_p)malloc(sizeof(seq_stack));
if(S==NULL)
{
printf("申请空间失败\n");
return NULL;
}
S->top=-1; //栈顶位置的初始值
bzero(S,sizeof(S->data));
return S;
}
//2、将数据压入栈
void push_stack(stack_p S,int value)
{
if(S==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(full_stack(S))
{
printf("栈已满,不能入栈\n");
return;
}
//先加再压
//先加栈顶位置,再将元素压入栈
S->data[++(S->top)] = value;
}
//3、判空
int empty_stack(stack_p S)
{
if(S==NULL)
{
printf("入参为空,请检查\n");
return -1;
}
return S->top==-1;
}
//4、判满
int full_stack(stack_p S)
{
if(S==NULL)
{
printf("入参为空,请检查\n");
return -1;
}
return S->top==MAX-1;
}
//5、弹栈/出栈,需要返回出栈的元素
int pop_stack(stack_p S)
{
if(S==NULL)
{
return -1;
}
if(empty_stack(S))
{
return -2;
}
return S->data[S->top--];
}
//6、输出栈
void show(stack_p S)
{
if(S==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(empty_stack(S))
{
printf("栈为空,无需输出\n");
return;
}
int i;
for(i=S->top;i>=0;i--)
{
printf("%d\n",S->data[i]);
}
}
队列
头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//定义队列的结构体
typedef struct queue
{
//队头:删除
int front;
//队尾:插入
int rear;
//数据
int data[MAX];
}queue,*queue_p;
#endif
自定义函数
#include "head.h"
queue_p create_queue()
{
queue_p S=(queue_p)malloc(sizeof(queue));
if(NULL==S)
return NULL;
if(S->data==NULL)
return NULL;
S->front=S->rear=0;
memset(S->data,0,sizeof(data));
return S;
}
//插入
int inser_tdata(int value,queue_p S)
{
//1.判断是否存在
//2.判满
if(NULL==S || S->rear==MAX)
{
printf("满\n");
return -1;
}
//插入
S->data[S->rear]=value;
S->rear++;
return 0;
}
//输出
int output(queue_p S)
{
//1.判断队列是否存在
//2.判断队列是否为空
if(NULL==S || S->rear==S->front)
{
return -1;
}
//3.从队头到队尾部
for(int i=S->front;i<S->rear;i++)
{
printf("%d\n",S->data[i]);
}
return 0;
}
//删除
int dele_data(queue_p S)
{
//1.判断队列是否存在
//2.判断队列是否为空
if(NULL==S || S->rear==S->front)
{
return -1;
}
printf("出队元素是:%d\n",S->data[S->front]);
S->front++;
return 0;
}
循环队列
当队尾(rear
)或队头(front
)到达数组末尾(MAX_SIZE-1
)时,通过 =(x + 1) % MAX_SIZE
=0回到下标0实现循环
头文件
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 7
typedef struct
{
int data[MAX];
int front; //记录队头位置
int rear; //记录队尾位置
}queue,*queue_p;
#endif
自定义函数文件
#include "queue.h"
//1、创建循环队列
queue_p create_queue()
{
queue_p Q = (queue_p)malloc(sizeof(queue));
if(Q==NULL)
{
printf("申请空间失败\n");
return -1;
}
//初始化
Q->front=Q->rear=0;
bzero(Q,sizeof(Q->data));
return Q;
}
//2、判满
int full_queue(queue_p Q)
{
if(Q==NULL)
{
printf("入参为空,请检查\n");
return -1;
}
return (Q->rear+1)%MAX==Q->front;
}
//3、判空
int empty_queue(queue_p Q)
{
if(Q==NULL)
{
printf("入参为空,请检查\n");
return -1;
}
return Q->rear==Q->front;
}
//4、入队
void push_queue(queue_p Q,int value)
{
if(Q==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(full_queue(Q))
{
printf("队列已满,不能入队\n");
return;
}
//将元素入队
Q->data[Q->rear]=value;
//让队尾位置移动
Q->rear = (Q->rear+1)%MAX;
}
//5、出队
int pop_queue(queue_p Q)
{
if(Q==NULL)
{
printf("入参为空,请检查\n");
return -1;
}
if(empty_queue(Q))
{
printf("队列为空,无需出队\n");
return -2;
}
//先保留原来的队头元素
int ret = Q->data[Q->front];
//让队头位置向后移动
Q->front = (Q->front+1)%MAX;
//返回刚刚出队的元素
return ret;
}
//6、输出
void show_que(queue_p Q)
{
if(Q==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(empty_queue(Q))
{
printf("队列为空,无需出队\n");
return;
}
int i=Q->front;
while(i!=Q->rear)
{
printf("%d\n",Q->data[i]);
i=(i+1)%MAX;
}
}
//7、求个数
int count_que(queue_p Q)
{
if(Q==NULL)
{
printf("入参为空,请检查\n");
return -1;
}
if(empty_queue(Q))
{
printf("队列为空,无需出队\n");
return -2;
}
return (Q->rear+MAX-Q->front)%MAX;
}
//8、释放循环队列
void free_que(queue_p *Q)
{
if(Q==NULL||*Q==NULL)
{
printf("入参为空,请检查\n");
return;
}
free(*Q);
*Q=NULL;
}