目录
一、栈
1、栈的概念及结构
2、如何实现栈
3、代码实现
3.1 栈的定义
3.2 栈中将要实现的函数
3.3 函数实现
二、队列
1、队列的概念及结构
2、如何实现队列
3、代码实现
3.1 队列定义
3.2 队列中将要实现的函数
3.3 函数实现
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称作栈顶,另一端称为栈底。栈中的元素遵守后进先出LIFO(Last In Fisrt Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
栈作为线性表的一种,实现方式无非两种。一种基于数组实现的顺序结构,另一种则基于链表实现。相对而言我们在实现栈的结构时更倾向于用数组实现,数组结构实现起来更优,因为数组在尾上插入数据的代价比较小。
#include
#include
#include
#include typedef int STDataType;
typedef struct Stack
{STDataType* data; //动态数组实现int size; //栈中有效数据个数int capacity; //栈的容量
}ST;
//初始化队列
void StackInit(ST* ps);// 入栈
void StackPush(ST* ps, STDataType x);// 出栈
void StackPop(ST* ps);// 获取栈顶元素
STDataType StackTop(ST* ps);// 获取栈中有效元素个数
int StackSize(ST* ps);// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps);// 销毁栈
void StackDestroy(ST* ps);
//栈的初始化
void StackInit(ST* ps)
{assert(ps);int newcapacity = 4; //初识容量设置为4STDataType* a = (STDataType*)malloc(sizeof(STDataType) * newcapacity);if (a == NULL){perror("malloc fail");exit(-1);}ps->data = a;ps->size = 0;//初识化为零,代表的是栈顶元素的下一个位置,同时也表示栈内的元素个数ps->capacity = newcapacity;
}//压栈操作,将元素压入栈中
void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->size == ps->capacity){//如果栈中元素有效个数已经达到容量大小,就进行扩容STDataType* a = (STDataType*)realloc(ps->data, 2 * ps->capacity * sizeof(STDataType));if (a == NULL){perror("realloc fail");exit(-1);}ps->data = a;ps->capacity *= 2;}ps->data[ps->size] = x;ps->size++;
}//出栈,将栈顶元素弹出
void StackPop(ST* ps)
{assert(ps);//防止栈中没有元素仍进行出栈操作//如果对空栈进行出栈,下一次进栈就会造成越界问题assert(ps->size > 0);ps->size--;
}//取出栈顶元素
STDataType StackTop(ST* ps)
{assert(ps);return ps->data[ps->size - 1];
}//返回栈中元素个数
int StackSize(ST* ps)
{assert(ps);return ps->size;
}//判断栈是否为空栈
bool StackEmpty(ST* ps)
{assert(ps);return ps->size == 0;
}//销毁栈
void StackDestroy(ST* ps)
{assert(ps);free(ps->data);ps->data = NULL;ps->capacity = ps->size = 0;
}
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列中的元素具有先进先出(First In First Out)原则。
入队列:进行插入操作一端称为队尾。
出队列:进行删除操作的一端称为队头。
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,出完数据后需要挪动数据,效率会比较低。
#include
#include
#include
#include typedef int QDataType;
// 链式结构:表示队列
typedef struct QListNode
{struct QListNode* next;QDataType data;
}QNode;// 队列的结构
typedef struct Queue
{QNode* front; //队头QNode* rear; //队尾int size; //队列元素个数
}Queue;
// 初始化队列
void QueueInit(Queue* pq);// 队尾入队列
void QueuePush(Queue* pq, QDataType data);// 队头出队列
void QueuePop(Queue* pq);// 获取队列头部元素
QDataType QueueFront(Queue* pq);// 获取队列队尾元素
QDataType QueueBack(Queue* pq);// 获取队列中有效元素个数
int QueueSize(Queue* pq);// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* pq);// 销毁队列
void QueueDestroy(Queue* pq);
#include "Queue.h"
//初识化队列
void QueueInit(Queue* pq)
{assert(pq);pq->front = pq->rear = NULL;pq->size = 0;
}//入队列
void QueuePush(Queue* pq, QDataType data)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}newnode->data = data;newnode->next = NULL;if (pq->front == NULL){pq->front = pq->rear = newnode;}else{pq->rear->next = newnode;pq->rear = newnode;}pq->size++;
}//出队列
void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->front->next == NULL){free(pq->front);pq->front = pq->rear = NULL;}else{QNode* next = pq->front->next;free(pq->front);pq->front = next;}pq->size--;
}//取队头元素
QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->front->data;
}//取队尾元素
QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->rear->data;
}//返回队列中元素个数
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}//判断队列是否为空
int QueueEmpty(Queue* pq)
{assert(pq);return pq->front == NULL && pq->rear == NULL;
}//销毁队列
void QueueDestroy(Queue* pq)
{assert(pq);assert(pq->size > 0);QNode* cur = pq->front;while (cur){QNode* next = cur->next;free(cur);cur = next;}pq->size = 0;pq->front = pq->rear = NULL;
}
以上就是通过C语言实现的栈和队列了,希望能够帮到大家,如果有不对的地方请各位大佬评论区指正。
上一篇:15.2 浏览器中的进程
下一篇:Java语法:枚举