如有错误欢迎指正
文章目录
前言
一.栈
1.头文件Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
// 支持动态增长的栈
typedef int STDataType;
#define InitCapacity 4
typedef struct Stack
{
STDataType* array;
int top; // 栈顶
int capacity ; // 容量
}Stack;
//打印
void StackPrint(Stack* ps);
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
2.Stack.ctack.c
2.1打印
#include"Stack.h"
void StackPrint(Stack* ps)
{
int i = 0;
for (i = 0; i < ps->top; i++)
{
printf("%d ", ps->array[i]);
}
printf("\n");
}
2.2 初始化栈
void StackInit(Stack* ps)
{
STDataType* arr = (STDataType*)malloc(sizeof(STDataType) * InitCapacity);
if (arr == NULL)
{
perror("malloc fail");
exit(-1);
}
ps->array = arr;
ps->capacity = InitCapacity;
ps->top = 0;//top初始为零,表示栈顶的下一个。top初始化为-1,表示栈顶元素
}
2.3入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->top == ps->capacity)
{
STDataType* arr = (STDataType*)realloc(ps->array,sizeof(STDataType) * ps->capacity * 2);
if (arr == NULL)
{
perror("malloc fail");
exit(-1);
}
ps->array = arr;
ps->capacity *= 2;
}
ps->array[ps->top] = data;
ps->top++;
}
2.4 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
2.5 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->array[ps->top - 1];
}
2.6 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
2.7 检测栈是否为空
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
2.8 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->array);
ps->array = NULL;
ps->top = 0;
ps->capacity = 0;
}
二、队列
1.头文件Queue.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
// 链式结构:表示队列
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* pNext;
QDataType data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* front;
QNode* rear;
int size;
}Queue;
//打印
void QueuePrint(Queue* q);
// 初始化队列
void QueueInit(Queue* q);
// 队尾入队列
void QueuePush(Queue* q, QDataType data);
// 队头出队列
void QueuePop(Queue* q);
// 获取队列头部元素
QDataType QueueFront(Queue* q);
// 获取队列队尾元素
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数
int QueueSize(Queue* q);
// 检测队列是否为空
bool QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);
2.Queue.c
2.1初始化队列
#include"Queue.h"
void QueueInit(Queue* q)
{
assert(q);
q->front = q->rear = NULL;
q->size = 0;
}
2.2队尾入队列
void QueuePush(Queue* q, QDataType data)
{
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return 0;
}
newnode->data = data;
newnode->pNext = NULL;
if (q->front == NULL)
{
assert(q->rear == NULL);//这里头尾应该都为空,所以if语句时判断谁都可以,但要是他们不都为空就出错了,所以断言一下
q->front = q->rear = newnode;
}
else
{
q->rear->pNext = newnode;
q->rear = newnode;
}
q->size++;
}
2.3 队头出队列
void QueuePop(Queue* q)
{
assert(q);
assert(q->front != NULL);
if (q->front->pNext == NULL)
{
free(q->front);
q->front = q->rear = NULL;
}
else
{
Queue* tmp = q->front->pNext;
free(q->front);
q->front = tmp;
}
q->size--;
}
2.4 获取队列头部元素
QDataType QueueFront(Queue* q)
{
assert(q);
assert(q->front != NULL);
return q->front->data;
}
2.5获取队列队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->rear->data;
}
2.6 获取队列中有效元素个数
int QueueSize(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->size;
}
2.7 检测队列是否为空
bool QueueEmpty(Queue* q)
{
assert(q);
return q->size == 0;
}
2.8 销毁队列
void QueueDestroy(Queue* q)
{
assert(q);
QNode* cur = q->front;
while (cur)
{
QNode* next = cur->pNext;
free(cur);
cur = next;
}
q->front = q->rear = NULL;
q->size = 0;
}
2.9打印
void QueuePrint(Queue* q)
{
QNode* cur = q->front;
int i = 0;
for (i = 0; i < q->size; i++)
{
printf("%d->", cur->data);
cur = cur->pNext;
}
printf("\n");
}
总结
本文是用c语言实现栈和队列,栈以数组形式表示,队列以链式结构表示。