目录
队列的模拟实现
队列的分析和实现
结论为:先进的先出。
我们使用不带头单向不循环链表。
定义Queue.h文件
链式结构表示队列:
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
typedef struct QueueNode//链表结构体
{
int val;
struct QueueNode* next;
}QNode;
typedef struct Queue//变量结构体,pq是变量的结构体指针
{
QNode* phead;
QNode* ptail;
int size;
}Queue;
//初始化和销毁
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);
定义Queue.c文件
队列的初始化和销毁
//初始化和销毁
void QueueInit(Queue* pq)
{
pq->phead = NULL;
pq->ptail = NULL;
pq->size = 0;
}
//销毁
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->phead;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
入队列
//入队列
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->val = x;
newnode->next = NULL;
//分两种情况插入
if (pq->ptail)
{
pq->ptail->next = newnode;//ptail的next指向newnode
pq->ptail = newnode;//ptail是尾节点,往后走
}
pq->size++;
}
堆分两种情况插入的分析:
出队列
void QueuePop(Queue* pq)//出队列
{
assert(pq);
assert(pq->phead);
//当有一个节点和多个节点时
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--;
}
取头和尾节点的值
//头、尾节点的值
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(pq->phead != NULL);
return pq->phead->val;//取头的数据
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
asser(pq->ptail != NULL);
return pq->ptail->val;//取尾的数据
}
队列是否为空,队列的个数
//队列是否为空、队列个数
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->size == 0;
}
int QueueSize(Queue* pq)
{
assert(pq);
return pq->size;
}
定义Text.c文件
#include"Queue.h"
int main()
{
Queue q;
QueueInit(&q);
QueueInit(&q,1);
QueueInit(&q,2);
QueueInit(&q,3);
QueueInit(&q,4);
while (!QueueEmpty(&q))
{
printf("%d ", QueueFront(&q));
QueuePop(&q);
}
QueueDestroy(&q);
return 0;
}
栈的模拟实现
用顺序表(数组)来实现
Stack.h文件
#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* 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文件
栈的初始化和销毁
//初始化
void STInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
//销毁
void STDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
入栈和出栈
//入栈
void STPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)//栈满了
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a, newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;//top设为x
ps->top++;
}
//出栈
void STPop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));//栈不能为空
ps->top--;//直接让top--直接去掉
}
获取栈顶元素、栈内元素、栈是否为空
//获取栈顶元素
STDataType STTop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
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;
}
Text.c文件
int main()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
int top = STTop(&s);
printf("%d ", top);
STPop(&s);
STPush(&s, 4);
STPush(&s, 5);
while (!STEmpty(&s))
{
int top = STTop(&s);
printf("%d ", top);
STPop(&s);
}
STDestroy(&s);
return 0;
}