C语言数据结构-栈和队列
- 1.栈
- 1.1概念与结构
- 1.2栈的实现
- 1.2.1结构体定义
- 1.2.2初始化
- 1.2.3销毁
- 1.2.4入栈
- 1.2.5出栈
- 1.2.6取栈顶处的元素
- 1.2.7获取栈中有效的个数
- 2.队列
- 2.1概念与结构
- 2.2队列的实现
- 2.2.1结构体定义
- 2.2.2入队列
- 2.2.3判断是否为空
- 2.2.4队列中的有效元素个数
- 2.2.5删除结点
- 2.2.6取对头数据
- 2.2.7取队尾的数据
- 2.2.8 销毁队列
- 3.栈的源代码
- 4.队列的源代码
1.栈
1.1概念与结构
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守先进后出或后进先出的原则。
栈不能实现任意位置的插入和删除。
栈底不进行任何的操作。
线性表:逻辑结构一定是线性的,物理结构不一定是线性的。
压栈:栈的插入操作叫做进栈/压栈/入栈,如数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
栈的底层结构:
栈的实现一般可以使用数组或者链表,相对而言数组的结构实现更优一点。因为数组在尾上插入数据的代价比较小。
1.2栈的实现
接下来,使用数组来实现栈:
入栈:往栈顶的位置插入数据,所以栈顶需要++。栈顶就相当于当前数组的大小size,所以让size++,就就可以实现入栈。
出栈:size- -。
需要增容所以需要把当前数组的容量实时记录下来。
1.2.1结构体定义
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int STDataType;
//定义栈的数据结构
typedef struct Stack
{
STDataType* arr;
int top;//指向栈顶的位置
int capacity;//空间的大小
}ST;
1.2.2初始化
void STInit(ST* pt)//传一级指针
{
assert(pt);
pt->arr = NULL;
pt->top = pt->capacity = 0;
}
有初始化一定就会有销毁。
1.2.3销毁
void STDestroy(ST* pt)
{
if (pt->arr != NULL)
free(pt->arr);
pt->arr = NULL;
pt->top = pt->capacity = 0;
}
1.2.4入栈
首先我们需要判断数组是否已经满了。如果已经满了,我们需要增容。
//入栈--栈顶的位置插入
void STPush(ST* pt, STDataType x)
{
//需要先判断空间够不够
asser(pt);
if (pt->top == pt->capacity)
{
//增容
int newCapacity = pt->capacity == 0 ? 4 : 2 * pt->capacity;//判断capacity是否wei0,如果为0直接就增容4,如果不为0,就2倍的增容。
STDataType* tmp = (STDataType*)realloc(pt->arr, newCapacity * sizeof(STDataType));
//这里新定义一个tmp是为了防止arr增容失败,arr数组中的数据都会发生改变
if (tmp == NULL)
{
perror("reail fail!\n");
exit(1);
//直接退出
}
pt->arr = tmp;
pt->capacity = newCapacity;
}
//空间足够
pt->arr[pt->top++] = x;
}
1.2.5出栈
void STPop(ST* pt)
{
assert(!STEmpty(pt));
pt->top=pt->arr[pt->top--];
}
1.2.6取栈顶处的元素
因为要的是元素,需要在数组中找到栈顶的元素,不能直接top-1,直接top-1后是top-1的值,不是top-1处数组中的值。
//取栈顶的元素,需要数组
void STFind(ST* ps)
{
assert(!STEmpty(ps));
//判断栈是否为空
return ps->arr[ps->top-1];
}
1.2.7获取栈中有效的个数
直接返回top的值就可以
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
2.队列
2.1概念与结构
概念:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特点
入队列:进行插入操作的一端称为队尾
出队列进行删除操作的一端称为对头
队列底层结构:
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
选择链表为底层结构,那么在定义结构体时,就需要再把链表也结构体化一下。
假如入队列的顺序是1,2,3,4;那么出队列的顺序也是1,2,3,4.就像我们日常生活中的排队一样。先进先出,后进后出
2.2队列的实现
上面的方法是可行的,但是时间复杂度还是很高。我们还可以其他的办法:
这种方法只需要将链表改造一下就可以了。
2.2.1结构体定义
由于队列是由链表构成的,所以在结构体定义时,需要将链表中的头结点和尾节点定义出来,队列是由数据域和指针域组成的,也是需要定义出来的。
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//定义队列结点结构体
typedef int STDataType;
typedef struct QueueNode
{
STDataType data;
struct QUeuNOde* next;
}QN;
struct Queue
{
QN* phead;
QN* ptail;
};
2.2.2入队列
void QueuPush(Queue* pq,STDataType x)
{
assert(pq);
//创建一个新节点
//在队尾插
QN* newnode = (QN*)malloc(sizeof(QN));
if (newnode == NULL)
{
perror("malloc fail!\n");
exit(1);
}
newnode->data = x;
newnode->next = NULL;
//队列为空
if (pq->phead == NULL)
{
pq->phead = pq->ptail = newnode;
}
else {
pq->ptail->next = newnode;
pq->ptail = pq->ptail->next;//让ptail往后走
}
}
2.2.3判断是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->phead == NULL;
}
2.2.4队列中的有效元素个数
int QueueSize(Queue* pq)
{
QN* pcur = pq->phead;
int size;
while (pcur)
{
size++;//++后向后遍历直到找到pcur为空的时候
pcur = pcur->next;
}
return size;
}
2.2.5删除结点
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
//只有一个结点
if (pq->phead == pq->ptail)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
//多个结点
else {
QN* next = pq->phead->next;
free(pq->phead);
next = next->next;
pq->phead = next;
pq->phead = pq->ptail = NULL;
}
pq->size--;
}
2.2.6取对头数据
返回的是头结点的数据
STDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
2.2.7取队尾的数据
//取队尾的数据
STDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
2.2.8 销毁队列
//销毁队列
void QueueDestroy(Queue* pq)
{
asser(pq);
QN* pcur = pq->phead;
while (pcur)
{
QN* next = pcur->next;
free(pcur);
pcur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
3.栈的源代码
Stack.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void STInit(ST* ps)//传一级指针
{
assert(ps);
ps->arr = NULL;
ps->top = ps->capacity = 0;
}
void STDestroy(ST* ps)
{
if (ps->arr != NULL)
free(ps->arr);
ps->arr = NULL;
ps->top = ps->capacity= 0;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
//如果他们两个相等返回的是真,下面的函数需要用!来接收
}
//入栈--栈顶的位置插入
void STPush(ST* ps, STDataType x)
{
//需要先判断空间够不够
assert(ps);
if (ps->top == ps->capacity)
{
//增容
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;//判断capacity是否wei0,如果为0直接就增容4,如果不为0,就2倍的增容。
STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
//这里新定义一个tmp是为了防止arr增容失败,arr数组中的数据都会发生改变
if (tmp == NULL)
{
perror("realloc fail!\n");
exit(1);
//直接退出
}
ps->arr = tmp;
ps->capacity = newCapacity;
}
//空间足够
ps->arr[ps->top++] = x;
}
void STPop(ST* ps)
{
assert(!STEmpty(ps));
--ps->top;
}
//取栈顶的元素,需要数组
STDataType STFind(ST* ps)
{
assert(!STEmpty(ps));
//判断栈是否为空
return ps->arr[ps->top-1];
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;
//定义栈的数据结构
typedef struct Stack
{
STDataType* arr;
int top;//指向栈顶的位置
int capacity;
}ST;
//初始化
void STInit(ST* ps);//传一级指针
//销毁
void STDestroy(ST* ps);
//判断栈是否为空
bool STEmpty(ST* ps);
//入栈
void STPush(ST* ps,STDataType x);
//出栈
void STPop(ST* ps);
//取栈顶的元素
STDataType STFind(ST* ps);
//获取栈中有效的个数
int STSize(ST* ps);
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void test()
{
ST st;
STInit(&st);
STPush(&st, 1);
STPush(&st, 2);
STPush(&st, 3);
STPush(&st, 4);
STPush(&st, 5);
STFind(&st);
while (!STEmpty(&st))
{
//取栈顶元素
STDataType top = STFind(&st);
printf("%d ", top);
STPop(&st);
}
STDestroy(&st);
}
int main()
{
test();
return 0;
}
4.队列的源代码
Queue.vc
#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
void QueuInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;//队列中只有头和尾
pq->size = 0;
}
//判断是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->phead == NULL;
}
//队列中的有效元素个数
int QueueSize(Queue* pq)
{
//QN* pcur = pq->phead;
//int size=0;
//while (pcur)
//{
// size++;//++后向后遍历直到找到pcur为空的时候
// pcur = pcur->next;
//}
/*return size;*/
return pq->size;//时间复杂度降低了
}
void QueuPush(Queue* pq,STDataType x)
{
assert(pq);
//创建一个新节点
//在队尾插
QN* newnode = (QN*)malloc(sizeof(QN));
if (newnode == NULL)
{
perror("malloc fail!\n");
exit(1);
}
newnode->data = x;
newnode->next = NULL;
//队列为空
if (pq->phead == NULL)
{
pq->phead = pq->ptail = newnode;
}
else {
pq->ptail->next = newnode;
pq->ptail = pq->ptail->next;//让ptail往后走
}
pq->size++;
}
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
//只有一个结点
if (pq->phead == pq->ptail)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
//多个结点
else {
QN* next = pq->phead->next;
free(pq->phead);
next = next->next;
pq->phead = next;
pq->phead = pq->ptail = NULL;
}
pq->size--;
}
//取对头数据
STDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
//取队尾的数据
STDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
//销毁队列
void QueueDestroy(Queue* pq)
{
asser(pq);
QN* pcur = pq->phead;
while (pcur)
{
QN* next = pcur->next;
free(pcur);
pcur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
Queue.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
//定义队列结点结构体
typedef int STDataType;
typedef struct QueueNode
{
STDataType data;
struct QUeuNOde* next;
}QN;
//定义队列的结构
typedef struct Queue //typedef是去掉关键词struct
{
QN* phead;
QN* ptail;
int size;
}Queue;
void QueuInit(Queue* pq);
//入队列
void QueuPush(Queue* pq ,STDataType x);
//出队
void QueuePop(Queue* pq);
//判断是否为空
bool QueueEmpty(Queue* pq);
//队列中的有效元素个数
int QueueSize(Queue* pq);
//取对头数据
STDataType QueueFront(Queue* pq);
//取队尾的数据
STDataType QueueBack(Queue* pq);
//销毁队列
void QueueDestroy(Queue* pq);
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
void test()
{
Queue q;
QueuInit(&q);
QueuPush(&q, 1);
QueuPush(&q, 2);
QueuPush(&q, 3);
QueuPush(&q, 4);
//while (QueueSize(&q))
//{
// printf("%d\n", QueueSize(&q));
// QueuePop(&q);
//}
//printf("%d\n", QueueSize(&q));
printf("phead:%d\n", QueueFront(&q));
printf("ptail:%d\n", QueueBack(&q));
QueueDestroy(&q);
}
int main()
{
test();
return 0;
}