目录
本文给大家介绍栈和队列,到这里,线性表已经基本上有了初步的了解和运用,之后就是进入数据结构的下一阶段的内容,也就是非线性表。
一、栈
1、概念和结构
是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作,进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的元素遵守后进先出的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶
出栈:栈的删除操作叫出栈,出数据也在栈顶
如图所示:
2、栈的实现
栈可以用数组或者链表实现,一般用数组实现更优,因为数组尾部插入数据代价较小。
图示:
3、实现栈的接口
//初始化
void InitSK(SK* pst);
//销毁
void SKDestroy(SK* pst);
//入栈
void SKPush(SK* pst, SKDateType x);
//出栈
void SKPop(SK* pst);
//栈顶元素
SKDateType SKTop(SK* pst);
//判断空
bool ISEmpty(SK* pst);
//栈大小
int SKSize(SK* pst);
初始化
//初始化
void InitSK(SK* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0; //给0指向栈顶后一个位置,给-1指向栈顶
pst->capacity = 0;
}
销毁
//销毁
void SKDestroy(SK* pst)
{
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
入栈
//入栈
void SKPush(SK* pst, SKDateType x)
{
if (pst->top == pst->capacity)
{//检查容量
//为0就给4个空间,不为0两倍扩容
int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
SKDateType* tmp = (SKDateType*)realloc(pst->a, sizeof(SKDateType) * newcapacity);
if (tmp == NULL)
{
perror("realloc");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
出栈
//出栈
void SKPop(SK* pst)
{
assert(pst);
assert(!ISEmpty(pst));
pst->top--;
}
访问栈顶元素
//访问栈顶元素
SKDateType SKTop(SK* pst)
{
assert(pst);
assert(!ISEmpty(pst));
return pst->a[pst->top - 1];
}
判空
//判断空
bool ISEmpty(SK* pst)
{
assert(pst);
return pst->top == 0;
}
栈大小
int SKSize(SK* pst)
{
assert(pst);
return pst->top;
}
4、完整代码
声明:
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int SKDateType;
typedef struct Stack
{
SKDateType* a;
int top;
int capacity;
}SK;
//初始化
void InitSK(SK* pst);
//销毁
void SKDestroy(SK* pst);
//入栈
void SKPush(SK* pst, SKDateType x);
//出栈
void SKPop(SK* pst);
//访问栈顶元素
SKDateType SKTop(SK* pst);
//判断空
bool ISEmpty(SK* pst);
//栈大小
int SKSize(SK* pst);
定义:
#include"Stack.h"
//判断空
bool ISEmpty(SK* pst)
{
assert(pst);
return pst->top == 0;
}
//初始化
void InitSK(SK* pst)
{
assert(pst);
pst->a = NULL;
pst->top = 0; //给0指向栈顶后一个位置,给-1指向栈顶
pst->capacity = 0;
}
//销毁
void SKDestroy(SK* pst)
{
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
//入栈
void SKPush(SK* pst, SKDateType x)
{
if (pst->top == pst->capacity)
{//检查容量
//为0就给4个空间,不为0两倍扩容
int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
SKDateType* tmp = (SKDateType*)realloc(pst->a, sizeof(SKDateType) * newcapacity);
if (tmp == NULL)
{
perror("realloc");
return;
}
pst->a = tmp;
pst->capacity = newcapacity;
}
pst->a[pst->top] = x;
pst->top++;
}
//出栈
void SKPop(SK* pst)
{
assert(pst);
assert(!ISEmpty(pst));
pst->top--;
}
//访问栈顶元素
SKDateType SKTop(SK* pst)
{
assert(pst);
assert(!ISEmpty(pst));
return pst->a[pst->top - 1];
}
int SKSize(SK* pst)
{
assert(pst);
return pst->top;
}
二、队列
1、概念及结构
只允许在一端进行插入操作,在另一端进行删除操作的特殊的线性表,具有先进后出的特性
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头
2、队列的实现
队列可以用数组或者链表实现,一般用链表实现更优,因为链表头部插入数据代价较小。
3、实现的接口
//初始化
void InitQueue(Qe* qe);
//销毁
void DestroyQueue(Qe* qe);
//插入
void QueuePush(Qe* qe, QDateType x);
//删除
void QueuePop(Qe* qe);
//队头
QDateType Queuefront(Qe* qe);
//队尾
QDateType Queueback(Qe* qe);
//队列大小
int Queuesize(Qe* qe);
//判空
bool isEmpty(Qe* qe);
初始化
//初始化
void InitQueue(Qe* qe)
{
assert(qe);
qe->phead = NULL;
qe->ptail = NULL;
qe->size = 0;
}
销毁
//销毁
void DestroyQueue(Qe* qe)
{
QN* cur = qe->phead;
while (cur)
{
QN* next = cur->next;
free(cur);
cur = next;
}
qe->phead = qe->ptail = NULL;
qe->size = 0;
}
插入
//插入
void QueuePush(Qe* qe, QDateType x)
{
assert(qe);
QN* newnode = (QN*)malloc(sizeof(QN));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->next = NULL;
newnode->data = x;
if (qe->ptail == NULL)
{
assert(qe->phead == NULL);
qe->phead = qe->ptail = newnode;
}
else
{
qe->ptail->next = newnode;
qe->ptail = qe->ptail->next;
}
qe->size++;
}
删除
//删除 头删
void QueuePop(Qe* qe)
{
assert(qe);
assert(!isEmpty(qe));
//一个节点
if (qe->phead->next == NULL)
{
free(qe->phead);
qe->phead = qe->ptail = NULL;
}
else
{
QN* next = qe->phead->next;
free(qe->phead);
qe->phead = next;
}
qe->size--;
}
访问队头
//队头
QDateType Queuefront(Qe* qe)
{
assert(qe);
assert(!isEmpty(qe));
return qe->phead->data;
}
访问队尾
//队尾
QDateType Queueback(Qe* qe)
{
assert(qe);
assert(!isEmpty(qe));
return qe->ptail->data;
}
队列大小
//队列大小
int Queuesize(Qe* qe)
{
assert(qe);
return qe->size;
}
判空
//判空
bool isEmpty(Qe* qe)
{
assert(qe);
//return qe->phead == NULL && qe->ptail == NULL;
return qe->size == 0;
}
4、完整代码
声明:
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDateType;
typedef struct QueueNode
{//表示节点
struct QueueNode* next;
QDateType data;
}QN;
typedef struct Queue
{//表示整个队列
struct QueueNode* phead;
struct QueueNode* ptail;
int size;
}Qe;
//初始化
void InitQueue(Qe* qe);
//销毁
void DestroyQueue(Qe* qe);
//插入
void QueuePush(Qe* qe, QDateType x);
//删除
void QueuePop(Qe* qe);
//队头
QDateType Queuefront(Qe* qe);
//队尾
QDateType Queueback(Qe* qe);
//队列大小
int Queuesize(Qe* qe);
//判空
bool isEmpty(Qe* qe);
定义:
#include"queue.h"
//初始化
void InitQueue(Qe* qe)
{
assert(qe);
qe->phead = NULL;
qe->ptail = NULL;
qe->size = 0;
}
//销毁
void DestroyQueue(Qe* qe)
{
QN* cur = qe->phead;
while (cur)
{
QN* next = cur->next;
free(cur);
cur = next;
}
qe->phead = qe->ptail = NULL;
qe->size = 0;
}
//插入
void QueuePush(Qe* qe, QDateType x)
{
assert(qe);
QN* newnode = (QN*)malloc(sizeof(QN));
if (newnode == NULL)
{
perror("malloc");
return;
}
newnode->next = NULL;
newnode->data = x;
if (qe->ptail == NULL)
{
assert(qe->phead == NULL);
qe->phead = qe->ptail = newnode;
}
else
{
qe->ptail->next = newnode;
qe->ptail = qe->ptail->next;
}
qe->size++;
}
//删除 头删
void QueuePop(Qe* qe)
{
assert(qe);
assert(!isEmpty(qe));
//一个节点
if (qe->phead->next == NULL)
{
free(qe->phead);
qe->phead = qe->ptail = NULL;
}
else
{
QN* next = qe->phead->next;
free(qe->phead);
qe->phead = next;
}
qe->size--;
}
//队头
QDateType Queuefront(Qe* qe)
{
assert(qe);
assert(!isEmpty(qe));
return qe->phead->data;
}
//队尾
QDateType Queueback(Qe* qe)
{
assert(qe);
assert(!isEmpty(qe));
return qe->ptail->data;
}
//队列大小
int Queuesize(Qe* qe)
{
assert(qe);
return qe->size;
}
//判空
bool isEmpty(Qe* qe)
{
assert(qe);
//return qe->phead == NULL && qe->ptail == NULL;
return qe->size == 0;
}