1.栈的实现
栈是一种先入后出的数据结构,插入方便,不支持随机访问,读取非栈顶数据较慢。
栈可通过顺序表和链表的形式实现。
1.1顺序表(可动态增长)
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
基本操作有:
1.1.1栈的初始化
void StackInit(Stack* ps)
{
assert(ps);
ps->_a = NULL;
ps->_top = 0;
ps->_capacity = 0;
}
1.1.2入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
//扩容
int newCapacity = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
int* p = (int*)realloc(ps->_a, sizeof(Stack) * newCapacity);
if (p == NULL)
{
perror("realloc fail");
return;
}
ps->_a = p;
}
ps->_a[ps->_top] = data;
ps->_top++;
}
1.1.3出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->_top--;
}
1.1.4获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->_a[ps->_top - 1];
}
1.1.5获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->_top;
}
1.1.6判断栈是否为空,为空返回0
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top == 0;
}
1.1.7销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);
ps->_a = NULL;
ps->_top = 0;
ps->_capacity = 0;
}
1.2链表操作同上
typedef int STDataType
typedef struct StackNode
{
STDataType _a;
StackNode* next;
}STNode;
struct Stack
{
QNode* pbottom;
QNode* ptop;
int size;
};
2.队列的实现
队列是一种先入先出的数据结构,队列同样可以用顺序表和链表实现。从队尾插入元素,队头获取元素。不支持随机访问。
2.1使用链表实现队列
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
struct Queue
{
QNode* phead;
QNode* ptail;
int size;
};
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
bool QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
2.1.1队列初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = NULL;
pq->ptail = NULL;
pq->size = 0;
}
2.1.2销毁队列
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = NULL;
while (pq->phead)
{
cur = pq->phead->next;
free(pq->phead);
pq->phead = cur;
}
pq->phead = NULL;
pq->ptail = NULL;
pq->size = 0;
}
2.1.3入队列
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newNode = (QNode*)malloc(sizeof(QNode));
if (newNode == NULL)
{
perror("malloc fail");
return;
}
newNode->next = NULL;
newNode->data = x;
if (QueueEmpty(pq))
{
pq->phead = pq->ptail = newNode;
}
else
{
pq->ptail->next = newNode;
pq->ptail = newNode;
}
pq->size++;
}
2.1.3出队列
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->phead->next == NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
2.1.4获取队头元素
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
2.1.5获取队尾元素
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
2.1.6判断队列是否为空,为空返回0
bool QueueEmpty(Queue* pq)
{
assert(pq);
if (pq->size == 0)
{
return true;
}
return false;
}
2.1.7获取队列元素个数
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
2.2使用顺序表实现同上
typedef int QDataType;
struct Queue
{
QDataType* data;
int front;
int rear;
int size;
int capacity;
};
2.3循环队列
如其名,这种队列是一种循环结构,有一个固定长度N,最多能存储N-1个元素。
初始时:Queue->front = Queue->rear = 0;
队空时:Queue->front == Queue->rear;
队满时:(Queue->rear+1)%(N-1) == Queue->front;
队内有效长度为: (rear - front + N) % N + 1;
循环队列结构体:
typedef struct {
int rear;
int front;
int k; //队列长度
int* a;
} MyCircularQueue;
2.3.1初始化
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* p = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
if(p == NULL)
{
perror("malloc fail");
return NULL;
}
int* pa = (int*)malloc(sizeof(int) * (k + 1));
if(pa == NULL)
{
perror("malloc fail");
return NULL;
}
p->rear = p->front = 0;
p->a = pa;
p->k = k;
return p;
}
2.3.2向循环队列插入一个元素。如果成功插入则返回真
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if(myCircularQueueIsFull(obj))
{
return false;
}
obj->a[obj->rear] = value;
obj->rear = (obj->rear + 1) % (obj->k + 1);
return true;
}
2.3.3从循环队列中删除一个元素。如果成功删除则返回真。
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return false;
}
obj->front = (obj->front + 1) % (obj->k + 1);
return true;
}
2.3.4从队首获取元素。如果队列为空,返回 -1
int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
return obj->a[obj->front];
}
2.3.5获取队尾元素。如果队列为空,返回 -1 。
int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
{
return -1;
}
if(obj->rear != 0)
{
return obj->a[obj->rear - 1];
}
return obj->a[obj->k];
}
2.3.6检查循环队列是否为空。
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->front == obj->rear;
}
2.3.7检查循环队列是否已满。
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->rear + 1) % (obj->k + 1) == obj->front;
}
2.3.8循环队列销毁
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->a);
obj->a = NULL;
obj->front = 0;
obj->rear = 0;
obj->k = 0;
}