提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:以下是本篇文章正文内容,下面案例可供参考
一、栈
1.1 栈的概念及结构
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行 数据插入 和 删除操作 的一端称为 栈顶 ,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶
生活中的栈:盘子,书堆等
1.1 栈的实现
1.1.1栈的接口
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
//栈的初始化
void StackInit(ST* ps);
//栈的销毁
void StackDestroy(ST* ps);
//栈的插入
void Stcakpush(ST* ps, STDataType x);
//栈的删除,只能在栈顶进行删除
void Stackpop(ST* ps);
//查看栈顶元素
STDataType StackTop(ST* ps);
//判断栈为空?
bool StackEmpty(ST* ps);
//获取栈里面有多少数据
int StackSize(ST* ps);
1.1.2接口的实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
//栈的初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
//栈的销毁
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
//栈的插入
void Stcakpush(ST* ps, STDataType x)
{
assert(ps);
if (ps->capacity == ps->top)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a,sizeof(ps->a)*newcapacity);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
//栈的删除,只能在栈顶进行删除
void Stackpop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
//查看栈顶元素
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
//判断栈为空?
bool StackEmpty(ST* ps)
{
assert(ps);
if (ps->top >0)
{
return false;
}
return true;
//或者直接
//return ps->top == 0;
}
//获取栈里面有多少数据
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
二、队列
2.1 队列的概念及结构
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出
FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头
2.2队列的实现
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数
组头上出数据,效率会比较低
2.1.1 队列的接口
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
//前置声明
struct BinaryTreeNode;
typedef struct BinaryTreeNode* QDataType;
typedef struct QueueNode
{
QDataType* Data;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;
//队列初始化
void QueueInit(Queue* q);
// 销毁队列
void QueueDestroy(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
int QueueEmpty(Queue* q);
2.1.2 接口的实现
#include "Queue.h"
//队列初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->tail = pq->head = NULL;
}
// 销毁队列
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
if (cur == NULL)
{
return;
}
//判断最后一个的下一个是否为空,以防最后一个没删
while (cur->next != NULL)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
}
// 队尾入队列
void QueuePush(Queue* pq, QDataType data)
{
assert(pq);
QNode* NewNode = (QNode*)malloc(sizeof(QNode));
if (NewNode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
NewNode->Data = data;
NewNode->next = NULL;
if (pq->tail == NULL)
{
pq->head = pq->tail = NewNode;
}
else
{
pq->tail->next = NewNode;
pq->tail = NewNode;
}
}
// 队头出队列
void QueuePop(Queue* 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;
}
}
// 获取队列头部元素
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->Data;
}
// 获取队列队尾元素
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->Data;
}
// 获取队列中有效元素个数
int QueueSize(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
int size = 0;
while(cur)
{
size++;
cur = cur->next;
}
return size;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* pq)
{
assert(pq);
if (pq->head == NULL)
{
return 1;
}
else
{
return 0;
}
}