一.栈的实现:
1.头文件代码实现:
#include<assert.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
typedef int DataType;
typedef struct Stack
{
DataType* a;
int top;
int capacity;
}ST;
void STInit(ST* st);//初始化
void STDestroy(ST* st);//销毁
void STPush(ST* st, DataType x);//入栈
void STPop(ST* st);//出栈
DataType gettop(ST* st);//得到栈顶元素
int STSize(ST* st);//个数
bool STEmpty(ST* st);//判空
2.各接口函数函数的实现:
(1):初始化和销毁
void STInit(ST* st)//初始化
{
assert(st);
st->a = NULL;
st->top = st->capacity = 0;
}
void STDestroy(ST* st)//销毁
{
assert(st);
free(st->a);
st->a = NULL;
st->top = st->capacity = 0;
}
(2):入栈和出栈
void STPush(ST* st, DataType x)//入栈
{
assert(st);
if (st->top == st->capacity)
{
int newcapacity = st->capacity == 0 ? 4 : st->capacity * 2;
DataType* tmp = (DataType*)realloc(st->a, sizeof(DataType) * newcapacity);
if (tmp == NULL)
{
perror("");
return;
}
st->a = tmp;
st->capacity = newcapacity;
}
st->a[st->top] = x;
st->top++;
}
void STPop(ST* st)//出栈
{
assert(st);
assert(st->top > 0);
st->top--;
}
(3).得到栈顶元素:
DataType gettop(ST* st)//得到栈顶元素
{
assert(st && st->top > 0);
return st->a[st->top - 1];
}
(4).得到元素个数:
int STSize(ST* st)//个数
{
assert(st);
return st->top;
}
(5)判空
bool STEmpty(ST* st)//判空
{
assert(st);
return st->top == 0;
}
(6)遍历栈
void STPrint(ST* ps)//打印
{
while (STEmpty(ps) == 0)
{
printf("%d ", STTop(ps));
STPop(ps);
}
printf("\n");
}
二.队列的实现
1.头文件形式:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int DataType;
typedef struct QueueNode
{
DataType val;
struct QueueNode* next;
}QN;
typedef struct Queue
{
QN* phead;
QN* ptail;
int size;
}Queue;
void QNInit(Queue* pq);//初始化
void QNDestroy(Queue* pq);//销毁
void QNPush(Queue* pq, DataType x);//入队
void QNPop(Queue* pq);//出队
DataType QNFront(Queue* pq);//队首元素
DataType QNBack(Queue* pq);//队尾元素
int QNSize(Queue* pq);//个数
bool QNEmpty(Queue* pq);//判空
2.各接口函数的实现:
(1)初始化和销毁
void QNInit(Queue* pq)//初始化
{
assert(pq);
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
void QNDestroy(Queue* pq)//销毁
{
assert(pq);
QN* cur = pq->phead;
while (cur)
{
QN* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
pq->size = 0;
}
(2)入队和出队
void QNPush(Queue* pq, DataType x)//入队
{
assert(pq);
QN* node = (QN*)malloc(sizeof(QN));
if (node == NULL)
{
perror("");
return;
}
node->next = NULL;
node->val = x;
if (pq->ptail == NULL)
{
pq->phead = pq->ptail = node;
}
else
{
pq->ptail->next = node;
pq->ptail = node;
}
pq->size++;
}
void QNPop(Queue* pq)//出队
{
assert(pq);
assert(pq->phead);
if (pq->phead->next == NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else {
QN* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
(3)得到队头或队尾:
DataType QNFront(Queue* pq)//队首元素
{
assert(pq && pq->phead);
return pq->phead->val;
}
DataType QNBack(Queue* pq)//队尾元素
{
assert(pq && pq->ptail);
return pq->ptail->val;
}
(4)得到队员个数
int QNSize(Queue* pq)//个数
{
assert(pq);
return pq->size;
}
(5)判空
bool QNEmpty(Queue* pq)//判空
{
assert(pq);
return pq->size == 0;
}
(6)遍历队列
void print(Queue* pq)
{
assert(pq);
while (QNEmpty(pq) == 0)
{
printf("%d ", QNFront(pq));
QNPop(pq);
}
}