文章目录
- 栈和队列
- 1.栈
- 1.1 概念与结构
- 1.2 栈的实现
- 2. 队列
- 2.1 概念与结构
- 2.2 队列的实现
- 总结
栈和队列
1.栈
1.1 概念与结构
栈:⼀种特殊的线性表,其只允许在固定的⼀端进行插⼊和删除元素操作。进⾏数据插⼊和删除操作的⼀端称为栈顶,另⼀端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插⼊操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
栈的实现⼀般可以使⽤数组或者链表实现,相对而言数组的结构实现更优⼀些。因为数组在尾上插入数据的代价比较小。
1.2 栈的实现
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 capacity; //栈的空间大小int top; //栈顶
}ST;//初始化
void STInit(ST* ps);
//销毁
void STDestroy(ST* ps);
//栈顶 --入数据,出数据
void StackPush(ST* ps,STDataType x);
void StackPop(ST* ps);
//判断栈是否为空
bool StackEmpty(ST* ps);
//取栈顶元素
STDataType Stacktop(ST* ps);
// 栈里的数据不能被遍历,也不能随机访问
//获取栈中有效元素个数
int STSize(ST* ps);
stack.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"stack.h"
//初始化
void STInit(ST* ps)
{assert(ps);ps->arr = NULL;ps->capacity = ps->top = 0;//栈顶等于栈底
}//销毁
void STDestroy(ST* ps) {assert(ps);if (ps->arr)free(ps->arr);ps->arr = NULL;ps->capacity = ps->top = 0;
}
//栈顶 --入数据,出数据
void StackPush(ST* ps, STDataType x)//入栈
{assert(ps);//判断空间是否足够if (ps->top == ps->capacity)//如果相等就说明栈满了 用栈顶和容量比大小{int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;STDataType* tmp = (STDataType*)realloc(ps->arr, newcapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail!");exit(1);}ps->arr = tmp;//将tmp增容到arrps->capacity= newcapacity;//增容完,容量增大;}//空间足够ps->arr[ps->top++]=x;//栈顶向上移动
}
//出栈
void StackPop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));//如果不为空,top----ps->top;}
//判断栈是否为空
bool StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}
//取栈顶元素
STDataType Stacktop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->arr[ps->top - 1];
}
//获取栈中有效元素个数
int STSize(ST* ps)
{assert(ps);return ps->top;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"stack.h"
void STTest()
{ST st;STInit(&st);///插入数据StackPush(&st, 1);StackPush(&st, 2);StackPush(&st, 3);StackPush(&st, 4);printf("size :%d\n", STSize(&st));StackPop(&st);//循环出栈,直到栈为空while (!StackEmpty(&st)){STDataType data = Stacktop(&st);printf("%d " ,data);//出栈StackPop(&st);}STDestroy(&st);
}
int main()
{STTest();return 0;
}
2. 队列
2.1 概念与结构
概念:只允许在⼀端进⾏插⼊数据操作,在另⼀端进⾏删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)。
⼊队列:进行插入操作的⼀端称为队尾。
出队列:进行删除操作的⼀端称为队头。
队列也可以数组和链表的结构实现,使⽤链表的结构实现更优⼀些,因为如果使⽤数组的结构,出队列在数组头上出数据,效率会⽐较低。
2.2 队列的实现
queue.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//定义队列结构
typedef int QDataType;
typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QueueNode;
typedef struct Queue
{QueueNode*phead;QueueNode* ptail;int size;//队列有效个数
}Queue;//初始化
void QueueInit(Queue* pq);
//入队列 ,队尾
void QueuePash(Queue*pq, QDataType x);
//队列判空
bool QueueEmpty(Queue* pq);
//出队列,对头
void QueuePop(Queue* pq);
//取队头数据
QDataType QueueFront(Queue* pq);
//取队尾数据
QDataType QueueBack(Queue* pq);
//队列有效元素个数
int QueueSize(Queue* pq);
//销毁队列
void QueueDestroy(Queue* pq);
queue.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
//初始化
void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}
//入队列 ,队尾
void QueuePash(Queue* pq, QDataType x)
{assert(pq);QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));if (newnode == NULL){ferror("malloc fail!");exit(1);}newnode->data = x;newnode->next = NULL;//ptail newnodeif (pq->phead == NULL) {//队列为空pq->phead = pq->ptail= newnode;}else{//队列不为空pq->ptail->next = newnode;pq->ptail = newnode;//pq->ptail->next}pq->size++;
}
//队列判空
bool QueueEmpty(Queue* pq) {assert(pq);return pq->phead == NULL && pq->ptail == NULL;
}//出队列,对头
void QueuePop(Queue* pq) {assert(pq);assert(!QueueEmpty(pq));//只有一个结点的情况,避免ptail变成野指针if (pq->ptail == pq->phead){free(pq->phead);pq->phead =pq->ptail= NULL;}else{//删除队头元素QueueNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}//取队头数据
QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));//队列不为空return pq->phead->data;
}
//取队尾数据
QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));//队列不为空return pq->ptail->data;
}
//队列有效元素个数
int QueueSize(Queue* pq)
{assert(pq);//int size = 0;//QueueNode* pcur = pq->phead;//while (pcur)//{// size++;// pcur = pcur->next;//}//return size;return pq->size;
}
//销毁队列
void QueueDestroy(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));QueueNode* pcur = pq->phead;while (pcur){QueueNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
void QueueTest01()
{Queue q;QueueInit(&q);QueuePash(&q, 1);QueuePash(&q, 2);QueuePash(&q, 3);QueuePash(&q, 4);/*QueuePop(&q);*/printf("head:%d\n",QueueFront(&q));printf("htail:%d\n",QueueBack(&q));printf("size:%d\n",QueueSize(&q));QueueDestroy(&q);
}
int main()
{QueueTest01();return 0;
}
总结
以上内容就是超详细的数据结构3(初阶C语言版)栈和队列的全部内容,喜欢博主内容的可以一键三连,支持博主!!