用C语言实现栈和队列

前言

栈和队列是两种结构较为相似的数据结构。栈为后进先出,队列为先进先出。

栈中只能在栈顶进行插入和删除数据。

队列则是在队头删数据,在队尾入数据。

栈的实现

我们首先要定义一个结构体用来存储数据,同时要有变量来记录容量大小,用于后续扩容。

要包含的头文件和大致要实现的功能函数

#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);
// 获取栈中有效元素个数 
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回true,如果不为空false 
bool StackEmpty(Stack* ps);
// 销毁栈 
void StackDestroy(Stack* ps);

初始化和入栈有两种写法,第一种在初始化时置空,在入栈时再动态开辟空间。也就是我将使用的一种。另一种则是在初始化时就预先开辟一块空间。入栈时只需要判断是否满了需要扩容即可。

第一种

// 初始化栈 
void StackInit(Stack* ps)
{
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
// 入栈 
void StackPush(Stack* ps, STDataType data)
{
	
	if (ps->top == ps->capacity)
	{
        int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		Stack* new = (Stack*)realloc(ps->a, sizeof(Stack) * newcapacity);
        ps->a = new;
        ps->capacity = newcapacity;
	}
	ps->a[ps->top] = data;
	ps->top++;
}

第二种

// 初始化栈 
void StackInit(Stack* ps)
{
	ps->a = (Stack*)malloc(sizeof(Stack) * 4);
	ps->capacity = 4;
	ps->top = 0;
}
// 入栈 
void StackPush(Stack* ps, STDataType data)
{
	if (ps->top == ps->capacity)
	{
        int newcapacity = 2 * ps->capacity;
		Stack* new = (Stack*)realloc(ps->a, sizeof(Stack) * newcapacity);
        ps->a = new;
        ps->capacity = newcapacity;
	}
	ps->a[ps->top] = data;
	ps->top++;
}

接下来实现剩余的函数。

// 出栈 
void StackPop(Stack* ps)
{
	assert(ps->top > 0);
	ps->top--;
}
// 获取栈顶元素 
STDataType StackTop(Stack* ps)
{
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}
// 获取栈中有效元素个数 
int StackSize(Stack* ps)
{
	return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(Stack* ps)
{
	return ps->top == 0;
}
// 销毁栈 
void StackDestroy(Stack* ps)
{
	assert(ps->a);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

值得注意的是有的函数要注意参数的范围,避免越界,最好加上断言,这样在出现问题时可以迅速找到原因。

完整代码

//main.c
#include "test.h"
void test()
{
	Stack st;
	// 初始化栈 
	StackInit(&st);
	// 入栈 
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);
	int i = StackSize(&st);
	while (i--)
	{
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}
	// 销毁栈 
	void StackDestroy(Stack * ps);
}
int main()
{
	test();
	return 0;
}
//test.h
#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);
// 获取栈中有效元素个数 
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(Stack* ps);
// 销毁栈 
void StackDestroy(Stack* ps);
​//test.c
// 初始化栈 
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 ? 4 : 2 * ps->capacity;
		STDataType* new = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
		ps->a = new;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = data;
	ps->top++;
}
// 出栈 
void StackPop(Stack* ps)
{
	assert(ps->top > 0);
	ps->top--;
}
// 获取栈顶元素 
STDataType StackTop(Stack* ps)
{
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}
// 获取栈中有效元素个数 
int StackSize(Stack* ps)
{
	return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(Stack* ps)
{
	return ps->top == 0;
}
// 销毁栈 
void StackDestroy(Stack* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

​

队列的实现

直接上代码……

完整代码

//test.h
#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* tail;
}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);
//test.c
#include "test.h"
// 初始化队列 
void QueueInit(Queue* q)
{
	q->front = NULL;
	q->tail = NULL;
}
// 队尾入队列 
void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	newnode->data = data;
	newnode->next = NULL;
	if (q->front == NULL)
	{
		q->front = q->tail = newnode;
	}
	else
	{
		q->tail->next = newnode;
		q->tail = newnode;
	}
}
// 队头出队列 
void QueuePop(Queue* q)
{
	assert(q);
	if (q->front == NULL)
	{
		return NULL;
	}
	if (q->front->next == NULL)
	{
		free(q->front);
		q->front = q->tail = NULL;
	}
	else
	{
		QNode* cur = q->front->next;
		free(q->front);
		q->front = cur;
	}
}
// 获取队列头部元素 
QDataType QueueFront(Queue* q)
{
	return q->front->data;
}
// 获取队列队尾元素 
QDataType QueueBack(Queue* q)
{
	return q->tail->data;
}
// 获取队列中有效元素个数 
int QueueSize(Queue* q)
{
	int count = 0;
	QNode* cur = q->front;
	while (cur)
	{
		count++;
		cur = cur->next;
	}
	return count;
}
// 检测队列是否为空
bool QueueEmpty(Queue* q)
{
	return q->front == NULL;
}
// 销毁队列 
void QueueDestroy(Queue* q)
{
	QNode* cur = q->front;
	while (cur)
	{
		QNode* m = cur->next;
		free(cur);
		cur = m;
	}
	q->front = q->tail = NULL;
}
//main.c
#include "test.h"
void test()
{
	Queue pq;
	// 初始化队列 
	QueueInit(&pq);
	// 队尾入队列 
	QueuePush(&pq, 1);
	QueuePush(&pq, 2);
	QueuePush(&pq, 3);
	QueuePush(&pq, 4);
	QueuePush(&pq, 5);
	QueuePush(&pq, 6);
	while (!QueueEmpty(&pq))
	{
		printf("%d ", QueueFront(&pq));
		QueuePop(&pq);
	}
	// 销毁队列 
	void QueueDestroy(Queue * q);
}
int main()
{
	test();
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值