Stack.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDatatype;
typedef struct Stack
{
STDatatype* a;
int top;
int capacity;//动态增长类型
}ST;
//初始化
void StackInit(ST* ps);
//摧毁
void StackDestroy(ST* ps);
//压栈
void StackPush(ST* ps, STDatatype x);
//出栈
void StackPop(ST* ps);
//栈是否为空
bool StackEmpty(ST* ps);
//栈的元素个数
int StackSize(ST* ps);
//栈顶
STDatatype StackTop(ST* ps);
Stack.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;//也可以设为-1
ps->capacity = 0;
}
//销毁
void StackDestroy(ST* ps)
{
assert(ps);
if (ps->a)
{
free(ps->a);
}
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
//插入
void StackPush(ST* ps, STDatatype x)
{
//先判断是否增容
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDatatype* tmp = realloc(ps->a, sizeof(STDatatype) * newcapacity);
if (NULL == tmp)
{
printf("ralloc fail\n");
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity = newcapacity;
}
}
ps->a[ps->top] = x;
ps->top++;
}
//删除
void StackPop(ST* ps)
{
assert(ps);
//判断是否为空
assert(!StackEmpty(ps));
//顺序表实现的栈,直接让top--即可
ps->top--;
}
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
STDatatype StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
Quene.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
//用链表实现队列
typedef struct QueneNode
{
struct QueneNode* next;
QDataType data;
}QueneNode;
//队列只需要记录头和尾
typedef struct Quene
{
QueneNode* phead;
QueneNode* ptail;
}Quene;
//初始化
void QueneInit(Quene* pq);
//销毁
void QueneDestory(Quene* pq);
//入队
void QuenePush(Quene* pq, QDataType x);
//出队
void QuenePop(Quene* pq);
//队的大小
int QueneSize(Quene* pq);
//队头
QDataType QueneFront(Quene* pq);
//队尾
QDataType QueneBack(Quene* pq);
//队是否为空
bool QueneEmpty(Quene* pq);
Quene.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Quene.h"
void QueneInit(Quene* pq)
{
assert(pq);
pq->phead = NULL;
pq->ptail = NULL;
}
void QueneDestory(Quene* pq)
{
assert(pq);
//先摧毁链表
QueneNode* cur = pq->phead;
while (cur)
{
QueneNode* next = cur->next;
free(cur);
cur = next;
}
//摧毁队列
pq->phead = pq->ptail = NULL;
}
void QuenePush(Quene* pq, QDataType x)
{
assert(pq);
QueneNode* newnode = (QueneNode*)malloc(sizeof(QueneNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->ptail == NULL)
pq->phead = pq->ptail = newnode;
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
}
void QuenePop(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
if (pq->phead->next = NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QueneNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
}
int QueneSize(Quene* pq)
{
assert(pq);
int n = 0;
QueneNode* cur = pq->phead;
while (cur)
{
cur = cur->next;
n++;
}
return n;
}
QDataType QueneFront(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
return pq->phead->data;
}
QDataType QueneBack(Quene* pq)
{
assert(pq);
assert(!QueneEmpty(pq));
return pq->ptail->data;
}
bool QueneEmpty(Quene* pq)
{
assert(pq);
return pq->ptail == NULL && pq->phead == NULL;
}