目录
栈
栈的概念和结构
栈:一种特殊的线性表,只允许数据在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵循后进先出LIFO(Last In First Out)原则。
压栈:栈的插入操作叫做进栈,入数据在栈顶
出栈:栈的删除叫做出栈,出数据也在栈顶

栈的实现
栈的实现一般可以使用数组或者链表实现。因为栈的插入在栈顶,所以相对通过数组实现尾插更优。
//Stack.h 头文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a; //使用数组来模拟栈
int top; //记录栈内元素个数
int capacity; //记录栈总空间
}ST;
//栈的初始化
void STInit(ST* ps);
//栈的销毁
void STDestroy(ST* ps);
//入栈
void STPush(ST* ps, STDataType x);
//出栈
void STPop(ST* ps);
//返回栈顶的元素
STDataType STTop(ST* ps);
//返回当前栈内元素个数
int STSize(ST* ps);
//判断栈是否为空
bool STEmpty(ST* ps);
//Stack.c
#include"Stack.h"
void STInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void STDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void STPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->capacity == ps->top)
{
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
STDataType* tmp= (STDataType*)realloc(ps->a,sizeof(int) * newCapacity);
if (tmp == NULL)
{
perror("STPush::realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void STPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
--ps->top;
}
STDataType STTop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top-1];
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
队列
队列的概念和结构
队列:只允许在一段进行插入操作,在另一端进行数据的删除操作的线性表,队列具有先进先出FIFO(First In First Out)原则。
入队列:从队尾进行插入操作
出队列:从队头进行删除操作
队列的实现
队列的实现一般可以使用数组或者链表实现。但是因为队列需要在头部进行出队列,数组的效率可能较低,所以可以采用链表来实现。
//Queue.h 头文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next; //通过链表实现队列
QDataType data;
}QNode;
//在传入参数时,需要同时获取头结点和尾结点
//这里通过结构保存head和tail结点 传入结构的指针来获取
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Que;
//队列初始化
void QueueInit(Que* pq);
//队列销毁
void QueueDestroy(Que* pq);
//队列尾部插入数据
void QueuePush(Que* pq, QDataType x);
//队列头部删除数据
void QueuePop(Que* pq);
//获取队列头部数据
QDataType QueueFront(Que* pq);
//获取队列尾部数据
QDataType QueueBack(Que* pq);
//判断队列是否为空
bool QueueEmpty(Que* pq);
//返回队列元素个数
int QueueSize(Que* pq);
//Queue.c
#include "Queue.h"
void QueueInit(Que* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
pq->size = 0;
}
void QueueDestroy(Que* pq)
{
pq->head = pq->tail = NULL;
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
pq->size = 0;
}
void QueuePush(Que* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("QueuePush::malloc fail");
exit(-1);
}
newnode->next = NULL;
newnode->data = x;
if (pq->tail == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
pq->size++;
}
void QueuePop(Que* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* newhead = pq->head->next;
free(pq->head);
pq->head = newhead;
}
pq->size--;
}
QDataType QueueFront(Que* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
QDataType QueueBack(Que* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
bool QueueEmpty(Que* pq)
{
assert(pq);
return pq->tail == NULL;
}
int QueueSize(Que* pq)
{
assert(pq);
return pq->size;
}
30万+

被折叠的 条评论
为什么被折叠?



