一.基本框架
栈的特点就是先进后出,具体栈究竟是如何运行的可以看看这篇博客函数栈帧
二.初始化和销毁
三.插入
四.剩余部分
五.测试
六.源代码
main.c
#include"stack.h"
int main()
{
ST st;
STInit(&st);
STPush(&st, 1);
STPush(&st, 2);
STPush(&st, 3);
STPush(&st, 4);
STPush(&st, 5);
STPush(&st, 6);
STPop(&st);
STPop(&st);
//STPop(&st);
while (!STEmpty(&st))
{
printf("%d ", STTop(&st));
STPop(&st);
}
STDestroy(&st);
return 0;
}
stack.h
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
#define STDataType int
//一些常用的头文件和定义
typedef struct Stack {
int* a;//一个动态数组(注意栈是用数组构成的)
int top;//顶部元素是第几个元素
int capacity;//容量
}ST;//创建栈
//一些基本功能
void STInit(ST* ps);//初始化
void STDestroy(ST* ps);//销毁
void STPush(ST* ps, STDataType x);//栈顶插入(栈只能在栈顶插入)
void STPop(ST* ps);//栈顶弹出
int STSize(ST* ps);//当前栈里的元素个数
bool STEmpty(ST* ps);//判断是否为空
STDataType STTop(ST* ps);//查询栈顶元素
stack.c
#include"Stack.h"
void STInit(ST* ps)
{
assert(ps);
ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
if (ps->a == NULL)
{
perror("malloc fail");
return;
}
ps->capacity = 4;
ps->top = 0; // top是栈顶元素的下一个位置
//ps->top = -1; // top是栈顶元素位置
}
void STDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void STPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
STDataType* tmp = (STDataType*)realloc(ps->a,
sizeof(STDataType) * ps->capacity * 2);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity *= 2;
}
ps->a[ps->top] = x;
ps->top++;
}
void STPop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
ps->top--;
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
STDataType STTop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
return ps->a[ps->top - 1];
}
七.队
队的概念是先进先出。是用单链表实现的,一般情况下我们使用两个指针,头指针和尾指针(ps:这与单链表不同,单链表只有头指针是因为单链表尾删后,尾指针必须向前移一个但由于没有前一个节点的指针所以又不得不从头节点遍历,所以单链表是完全没必要使用尾指针的。但队列由于性质只能进行头删,所以我们可以使用尾指针方便找尾。)
main.c
#include"Queue.h"
int main()
{
Queue p;
QueueInit(&p);
QueuePush(&p, 1);
QueuePush(&p, 2);
QueuePush(&p, 3);
QueuePush(&p, 4);
QueuePush(&p, 5);
while (!QueueEmpty(&p))
{
printf("%d ", QueueFront(&p));
QueuePop(&p);
}
QueueDestroy(&p);
return 0;
}
queue.c
#include"Queue.h"
void QueueInit(Queue* pq)//初始化
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
pq->size = 0;
}
void QueueDestroy(Queue* pq)//销毁
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
pq->size = 0;
}
void QueuePush(Queue* pq, QDataType x) //插入
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror("malloc fail");
return;
}
newnode->data = x;
newnode->next = NULL;
if (pq->head == NULL)
{
assert(pq->tail == NULL);
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
pq->size++;
}
void QueuePop(Queue* pq)//删除
{
assert(pq);
assert(pq->tail);
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
pq->size--;
if (pq->tail == NULL)
pq->tail = NULL;
}
int QueueSize(Queue* pq)//大小
{
assert(pq);
return pq->size;
}
bool QueueEmpty(Queue* pq)//判断是否为空
{
assert(pq);
return pq->size == 0;
}
QDataType QueueFront(Queue* pq)//访问队头数据
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
QDataType QueueBack(Queue* pq)//访问队尾数据
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
queue.h
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
#define QDataType int
//一些常见的头文件和定义
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;//定义节点
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;//定义头节点和尾节点
void QueueInit(Queue* pq);//初始化
void QueueDestroy(Queue* pq);//销毁
void QueuePush(Queue* pq,QDataType x); //插入
void QueuePop(Queue* pq);//删除
int QueueSize(Queue* pq);//大小
bool QueueEmpty(Queue* pq);//判断是否为空
QDataType QueueFront(Queue* pq);//访问队头数据
QDataType QueueBack(Queue* pq);//访问队尾数据