一、栈实现
栈是先进后出,使用顺序表实现实现栈唯一要注意的就是初始化栈的top值,
若top = -1;有元素时top指向最后一个元素.
若top = 0; 有元素时top指向最后一个元素的下一个位置.
1.1 代码实现
stack.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
bool IsEmptyStack(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
teststack.c
#include"Stack.h"
void pfint(Stack* ps)
{
int i = 0;
for (i = 0; i < ps->_top; i++)
{
printf("%d ", (ps->_a)[i]);
}
}
void test1()
{
Stack ps;
StackInit(&ps);
StackPush(&ps, 4);
StackPush(&ps, 3);
StackPush(&ps, 2);
StackPush(&ps, 1);
pfint(&ps);
printf("\n栈顶元素:%d", StackTop(&ps));
printf("\n栈中元素个数:%d", StackSize(&ps));
StackPop(&ps); printf("\n");
pfint(&ps);
printf("\n栈顶元素:%d", StackTop(&ps));
printf("\n栈中元素个数:%d", StackSize(&ps));
StackPop(&ps); printf("\n");
pfint(&ps);
printf("\n栈顶元素:%d", StackTop(&ps));
printf("\n栈中元素个数:%d", StackSize(&ps));
StackPop(&ps); printf("\n");
pfint(&ps);
printf("\n栈顶元素:%d", StackTop(&ps));
printf("\n栈中元素个数:%d", StackSize(&ps));
StackPop(&ps); printf("\n");
pfint(&ps);
/*StackPop(&ps); printf("\n");
pfint(&ps);*/
}
int main()
{
test1();
return 0;
}
stack.c
#include"Stack.h"
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->_a = NULL;
ps->_capacity = 0;
ps->_top = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
int newcapacity = ps->_capacity == 0 ? 2 : ps->_capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->_a, newcapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->_capacity = newcapacity;
ps->_a = tmp;
}
ps->_a[ps->_top] = data;
ps->_top++;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool IsEmptyStack(Stack* ps)
{
assert(ps);
return ps->_top == 0;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(!IsEmptyStack(ps));
ps->_top--;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!IsEmptyStack(ps));
return ps->_a[ps->_top - 1];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->_top;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);
ps->_capacity = 0;
ps->_top = 0;
}
二、队列
队列是先进先出,使用链表实现,链表的头作为队列的头,链表的尾作为队列的尾,出队列头删,进队列尾插.
2.1 代码实现
Queue.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
// 链式结构:表示队列
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* _next;
QDataType _data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* _front;
QNode* _rear;
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
bool QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);
#pragma once
queue.c
#include"Queue.h"
// 初始化队列
void QueueInit(Queue* q)
{
assert(q);
q->_front = NULL;
q->_rear = NULL;
q->size = 0;
}
// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
assert(q);
QNode* tmp = (QNode*)malloc(sizeof(QNode));
if (NULL == tmp)
{
perror("QueueInit malloc fail!");
return;
}
tmp->_data = data;
tmp->_next = NULL;
if (NULL == q->_rear && NULL == q->_front)
{
q->_rear = tmp;
q->_front = tmp;
}
else
{
//链接
q->_rear->_next = tmp;
q->_rear = tmp;
}
q->size++;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* q)
{
assert(q);
return q->size == 0;
}
// 队头出队列
void QueuePop(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
QNode* del = q->_front;
//一个结点
if (q->_front == q->_rear)
{
q->_rear = NULL;
q->_front = NULL;
}
//多个结点
else
{
q->_front = q->_front->_next;
}
free(del);
q->size--;
}
// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->_front->_data;
}
// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->_rear->_data;
}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
assert(q);
return q->size;
}
// 销毁队列
void QueueDestroy(Queue* q)
{
assert(q);
while (!QueueEmpty(q))
{
QueuePop(q);
}
}
testqueue.c
#include"Queue.h"
void print(Queue* q)
{
assert(q);
if (!q->size)
{
printf("队列为空!\n");
return;
}
QNode* cur = q->_front;
int num = q->size;
while (num--)
{
printf("%d ",cur->_data);
cur = cur->_next;
}
printf("\n");
}
void test()
{
Queue q;
QueueInit(&q);
QueuePush(&q, 1);
print(&q);
QueuePop(&q);
print(&q);
QueuePush(&q, 2);
print(&q);
QueuePush(&q, 3);
print(&q);
QueuePush(&q, 4);
print(&q);
QueuePop(&q);
print(&q);
QueuePop(&q);
print(&q);
QueuePop(&q);
print(&q);
QueuePop(&q);
print(&q);
QueuePop(&q);
print(&q);
}
int main()
{
test();
return 0;
}