关于栈
栈是一种特殊的线性表,只允许在固定的一端插入和删除元素操作
栈是一种先进后出的顺序表
进行数据插入和删除操作的一端称为栈顶,另一端为栈底 (可以理解为类似羽毛球桶的结构后存的先取)
栈的插入叫做压栈 在栈顶
栈的删除叫做出栈 也在栈顶
用顺序表来实现栈:
头文件定义
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
typedef int STDataType;
typedef struct stack
{
STDataType* a;
int top;
int capacity;
}ST;
//栈的创建与销毁
void STInit(ST* pst);
void STDestory(ST* pst);
//栈的插入与删除
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
//取出栈顶的数据
STDataType STTop(ST* pst);
//判空
bool STEmpty(ST* pst);
//获取数据个数
int STSize(ST* pst);
具体实现:
//栈的创建与销毁
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0;
pst->capacity = 0;
}
void STDestory(ST* pst)
{
assert(pst);
free(pst->a);
pst->top = 0;
pst->capacity = 0;
}
//栈的插入与删除
void STPush(ST* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc:");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
void STPop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
pst->top--;
}
//取出栈顶的数据
STDataType STTop(ST* pst)
{
assert(pst);
return pst->a[pst->top-1];
}
//判空
bool STEmpty(ST* pst)
{
assert(pst);
return pst->top == 0;
}
//获取数据个数
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
关于队列
队列是只允许在一端进行插入操作,在另一端进行删除操作的线性表,简称“队”。
队列是一种先进先出的线性表
允许插入的一端称为队尾,允许删除的一端称为队头。
向队列中插入新的数据元素称为入队,新入队的元素就成为了队列的队尾元素。
从队列中删除队头元素称为出队,其后继元素成为新的队头元素。
用链表实现队列:
头文件
typedef int QDataType;
// 链式结构:表示队列
typedef struct QListNode
{
struct QListNode* next;
QDataType data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
// 初始化队列
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);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);
队列的实现:
// 初始化队列
void QueueInit(Queue* q)
{
assert(q);
q->head = NULL;
q->tail = NULL;
q->size = 0;
}
// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
assert(q);
QNode* new = (QNode*)malloc(sizeof(QNode));
if (new == NULL)
{
perror("malloc");
return;
}
new->next = NULL;
new->data = data;
if (q->tail == NULL)
{
q->head = q->tail = new;
}
else
{
q->tail->next = new;
q->tail = new;
}
q->size++;
}
// 头删
void QueuePop(Queue* q)
{
assert(q);
assert(q->size != 0);
if (q->head->next == NULL)
{
free(q->head);
q->head = q->tail = NULL;
}
else
{
QNode* next = q->head->next;
free(q->head);
q->head = next;
}
q->size--;
}
// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
assert(q);
assert(q->head);
return q->head->data;
}
// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
assert(q->tail);
return q->tail->data;
}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
assert(q);
return q->size;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* q)
{
assert(q);
return q->size==0;
}
// 销毁队列
void QueueDestroy(Queue* q)
{
assert(q);
QNode* cur = q->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
q->head = q->tail = NULL;
q->size = 0;
}