栈和列队模拟实现
- 一.栈
- 1.1栈的概念及其结构
- 1.2栈的实现
- 1.2.1 stack.h
- 1.2.2 stack.c
- 二.列队
- 2.1队列的概念及结构
- 2.2队列的实现
- 2.2.1 Queue.h
- 2.2.2 Queue.cpp
一.栈
1.1栈的概念及其结构
栈
:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈
:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶
出栈
:栈的删除操作叫做出栈。出数据也在栈顶
栈是先进后出
1.2栈的实现
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
1.2.1 stack.h
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{STDataType* a;int top; // 栈顶int capacity; // 容量
}ST;// 初始化栈
void StackInit(ST* ps);// 入栈
void StackPush(ST* ps, STDataType x);// 出栈
void StackPop(ST* ps);// 获取栈顶元素
STDataType StackTop(ST* ps);// 获取栈中有效元素个数
int StackSize(ST* ps);// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps);// 销毁栈
void StackDestroy(ST* ps);
1.2.2 stack.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"a.h"
#include"stack.h"// 初始化栈
void StackInit(ST* ps)
{assert(ps);ps->capacity = 0;ps->a = NULL;//表示指向栈顶元素的下一个位置ps->top = 0;//表示指向栈顶元素//ps->top = -1;
}// 入栈
void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->capacity==ps->top){int newcapcity = ps->capacity == 0 ? 4 : 2 * ps->capacity;STDataType* tmp = (STDataType*)realloc(ps->a, 2 * sizeof(STDataType) * newcapcity);if (tmp==NULL){perror("realloc mail");return;}ps->capacity = newcapcity;ps->a = tmp;}ps->a[ps->top] = x;ps->top++;
}// 出栈
void StackPop(ST* ps)
{assert(ps);assert(ps->top > 0);ps->top--;
}// 获取栈顶元素
STDataType StackTop(ST* ps)
{assert(ps);assert(ps->top > 0);return ps->a[ps->top-1];
}// 获取栈中有效元素个数
int StackSize(ST* ps)
{return ps->top;
}// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps)
{assert(ps);return ps->top==0;
}// 销毁栈
void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->top = ps->capacity = 0;
}
二.列队
2.1队列的概念及结构
队列
:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头
2.2队列的实现
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
2.2.1 Queue.h
typedef int QDataType;
// 链式结构:表示队列
typedef struct QueueNode
{QDataType val;struct QueueNode* next;
}QNode;// 队列的结构
typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Queue;// 初始化队列
void QueueInit(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);
// 销毁队列
void QueueDestroy(Queue* q);
2.2.2 Queue.cpp
// 初始化队列
void QueueInit(Queue* q)
{assert(q);q->ptail = q->phead = NULL;q->size = 0;
}
// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{assert(q);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");return;}newnode->val = data;newnode->next = NULL;if (q->ptail == NULL){q->phead = q->ptail = newnode;}else{q->ptail->next = newnode;q->ptail = newnode;}q->size++;
}// 队头出队列
void QueuePop(Queue* q)
{assert(q);QNode* del = q->phead;q->phead = q->phead->next;free(del);if (q->phead == NULL){q->ptail = NULL;}q->size--;
}// 获取队列头部元素
QDataType QueueFront(Queue* q)
{assert(q);assert(q->phead);return q->phead->val;
}// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{assert(q);assert(q->ptail);return q->ptail->val;
}// 获取队列中有效元素个数
int QueueSize(Queue* q)
{assert(q);return q->size;
}// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q)
{assert(q);return q->phead == NULL;
}// 销毁队列
void QueueDestroy(Queue* q)
{QNode* cur = q->phead;while (cur != NULL){QNode* next = cur->next;free(cur);cur = next;}q->phead = q->ptail = NULL;q->size = 0;
}