顺序栈
定义
顺序栈(Sequential Stack),也称为数组栈,是一种使用数组实现的栈数据结构。栈是一种遵循后进先出(Last-In-First-Out,LIFO)原则的数据结构,即最后入栈的元素最先出栈。
特点
使用数组来存储栈元素,通过数组的下标来实现栈的操作。
栈有一个指针(通常称为栈顶指针),指向当前栈顶元素在数组中的位置。
栈顶指针初始化为-1表示栈为空,当有元素入栈时,栈顶指针增加;当元素出栈时,栈顶指针减少。
代码实现
定义
typedef struct MyStack {
int* element_top;//栈顶指针
int* element_base; //栈底指针
int capacity; ///栈容量
}MyStack;
初始化
void InitStack(MyStack* ps) {
ps->element_top = NULL;
ps->element_base = (int*)malloc(sizeof(int) * INITCAPACITY);
if (ps->element_base == NULL) {
printf("内存分配失败\n");
exit(OUTMEMORY);
}
ps->capacity = INITCAPACITY;
}
判空
bool IsEmpty(MyStack* ps) {
return ps->element_top == ps->element_base;
}
判满
bool IsFull(MyStack* ps) {
return ps->element_base + ps->capacity == ps->element_top;
}
入栈
int Push(MyStack* ps, int val) {
if (IsFull(ps) && Grow(ps)) {
return OUTMEMORY;
}
else {
*ps->element_top++ = val;
return OK;
}
}
栈满扩容
bool Grow(MyStack* ps) {
int newlen = ps->capacity * INC_NUM;
int* p = (int*)realloc(ps->element_base, newlen);
if (p != NULL) {
return false;
}
else {
ps->element_base = p;
ps->element_top = ps->element_base + ps->capacity;
ps->capacity = newlen;
}
}
出栈
int Pop(MyStack* ps) {
if (IsEmpty(ps)) {
return ERROR;
}
else {
ps->element_top--;
return OK;
}
}
获取栈顶元素
int GetTop(MyStack* ps, int* val) {
if (IsEmpty(ps)) {
*val = 0;
return ERROR;
}
else
{
*val = *(ps->element_top-1);
return OK;
}
}
获取栈中有效元素个数
int GetSize(MyStack* ps) {
return ps->element_top - ps->element_base;
}
清空栈
void ClearStack(MyStack* ps) {
memset(ps->element_base, 0,sizeof(int) * ps->capacity);
ps->element_top = ps->element_base;
}
销毁栈
void DestoryStack(MyStack* ps) {
free(ps->element_base);
ps->element_base = NULL;
ps->element_top = NULL;
}
完整代码
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
//顺序栈 顺序表模拟栈 --> 尾插尾删O(1)
#define OK 1
#define ERROR 2
#define OUTMEMORY 3 //内存不足
#define INITCAPACITY 10 //栈初始容量
#define INC_NUM 1.5 //扩容大小
typedef struct MyStack {
int* element_top;//栈顶指针
int* element_base; //栈底指针
int capacity; ///栈容量
}MyStack;
//初始化
void InitStack(MyStack* ps) {
ps->element_base = ps->element_top=(int*)malloc(sizeof(int) * INITCAPACITY);
assert(ps->element_top != NULL);
ps->capacity = INITCAPACITY;
}
bool IsEmpty(MyStack* ps) {
return ps->element_top == ps->element_base;
}
bool IsFull(MyStack* ps) {
return ps->element_base + ps->capacity == ps->element_top;
}
//入栈操作
bool Grow(MyStack* ps) {
int newlen = ps->capacity * INC_NUM;
int* p = (int*)realloc(ps->element_base, newlen);
if (p != NULL) {
return false;
}
else {
ps->element_base = p;
ps->element_top = ps->element_base + ps->capacity;
ps->capacity = newlen;
}
}
int Push(MyStack* ps, int val) {
if (IsFull(ps) && Grow(ps)) {
return OUTMEMORY;
}
else {
*ps->element_top++ = val;
return OK;
}
}
//出栈操作
int Pop(MyStack* ps) {
if (IsEmpty(ps)) {
return ERROR;
}
else {
ps->element_top--;
return OK;
}
}
//获取栈顶元素
int GetTop(MyStack* ps, int* val) {
if (IsEmpty(ps)) {
*val = 0;
return ERROR;
}
else
{
*val = *(ps->element_top-1);
return OK;
}
}
//获取栈有效数据个数
int GetSize(MyStack* ps) {
return ps->element_top - ps->element_base;
}
//清空栈
void ClearStack(MyStack* ps) {
memset(ps->element_base, 0,sizeof(int) * ps->capacity);
ps->element_top = ps->element_base;
}
//销毁栈
void DestoryStack(MyStack* ps) {
free(ps->element_base);
ps->element_base = NULL;
ps->element_top = NULL;
}
顺序队列
定义
顺序栈(Sequential Stack)是一种使用数组实现的栈数据结构。栈是一种后进先出(Last-In-First-Out,LIFO)的数据结构,意味着最后入栈的元素将首先被弹出
特点
-
基于数组实现:顺序栈的底层使用数组来存储栈中的元素,这使得对栈的访问速度很快,因为数组的元素是连续存储的。
-
固定大小:顺序栈的大小在创建时就被确定了,它的容量是固定的。当栈满时,无法再添加新的元素,除非进行扩容操作。
-
高效的入栈和出栈操作:由于栈是一种LIFO数据结构,顺序栈的入栈(push)和出栈(pop)操作都非常高效,时间复杂度为O(1)。
代码实现
定义
typedef struct MyQueue {
int* queue_head;
int* queue_tail;
int capacity;
}MyQueue;
初始化
void InitQueue(MyQueue* pq) {
pq->queue_head = pq->queue_tail = (int*)malloc(sizeof(int) * INITCAPACITY);
pq->capacity = INITCAPACITY;
}
判空
bool IsEmpty(MyQueue* pq) {
return pq->queue_tail == pq->queue_head;
}
判满
bool IsFull(MyQueue* pq) {
return pq->queue_head + pq->capacity == pq->queue_tail;
}
队列满扩容
bool Grow(MyQueue* pq) {
int newlen = pq->capacity * INC_NUM;
int* p = (int*)realloc(pq->queue_head, sizeof(int) * newlen);
if (p!=NULL)
{
pq->queue_head = p;
pq->queue_tail = pq->queue_head +pq->capacity;
pq->capacity = newlen;
}
}
入队
int PushQueue(MyQueue* pq, int val) {
if (IsFull(pq) && Grow(pq)) {
return ERROR;
}
else {
*pq->queue_tail++ = val;
return OK;
}
}
出队
int PopQueue(MyQueue* pq, int* val) {
if (IsEmpty(pq)) {
*val = 0;
return ERROR;
}
else {
*val = *pq->queue_tail;
int* p = pq->queue_head;
while (p != pq->queue_tail) {
*p = *(p + 1);
}
pq->queue_tail--;
return OK;
}
}
获取队头元素
int GetTop(MyQueue* pq, int* pval) {
if (IsEmpty(pq)) {
*pval = 0;
return OK;
}
else {
*pval = *(pq->queue_head-1);
return OK;
}
}
获取队列有效元素个数
int GetSize(MyQueue* pq) {
return pq->queue_tail - pq->queue_head;
}
清空队列
void ClearQueue(MyQueue* pq) {
memset(pq->queue_head, 0, sizeof(int)*pq->capacity);
pq->queue_tail = pq->queue_head;
}
销毁队列
void DestoryQueue(MyQueue* pq) {
free(pq);
pq->queue_head = NULL;
pq->queue_tail = NULL;
}
完整代码
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
//顺序栈 顺序表模拟栈 --> 尾插尾删O(1)
#define OK 1
#define ERROR 2
#define OUTMEMORY 3 //内存不足
#define INITCAPACITY 10 //栈初始容量
#define INC_NUM 1.5 //扩容大小
typedef struct MyQueue {
int* queue_head;
int* queue_tail;
int capacity;
}MyQueue;
void InitQueue(MyQueue* pq) {
pq->queue_head = pq->queue_tail = (int*)malloc(sizeof(int) * INITCAPACITY);
pq->capacity = INITCAPACITY;
}
bool IsEmpty(MyQueue* pq) {
return pq->queue_tail == pq->queue_head;
}
bool IsFull(MyQueue* pq) {
return pq->queue_head + pq->capacity == pq->queue_tail;
}
bool Grow(MyQueue* pq) {
int newlen = pq->capacity * INC_NUM;
int* p = (int*)realloc(pq->queue_head, sizeof(int) * newlen);
if (p!=NULL)
{
pq->queue_head = p;
pq->queue_tail = pq->queue_head +pq->capacity;
pq->capacity = newlen;
}
}
int PushQueue(MyQueue* pq, int val) {
if (IsFull(pq) && Grow(pq)) {
return ERROR;
}
else {
*pq->queue_tail++ = val;
return OK;
}
}
int PopQueue(MyQueue* pq, int* val) {
if (IsEmpty(pq)) {
*val = 0;
return ERROR;
}
else {
*val = *pq->queue_tail;
int* p = pq->queue_head;
while (p != pq->queue_tail) {
*p = *(p + 1);
}
pq->queue_tail--;
return OK;
}
}
int GetTop(MyQueue* pq, int* pval) {
if (IsEmpty(pq)) {
*pval = 0;
return OK;
}
else {
*pval = *(pq->queue_head-1);
return OK;
}
}
int GetSize(MyQueue* pq) {
return pq->queue_tail - pq->queue_head;
}
void ClearQueue(MyQueue* pq) {
memset(pq->queue_head, 0, sizeof(int)*pq->capacity);
pq->queue_tail = pq->queue_head;
}
void DestoryQueue(MyQueue* pq) {
free(pq);
pq->queue_head = NULL;
pq->queue_tail = NULL;
}