栈和队列的实现

本文介绍了如何使用C语言实现栈和队列这两种基本数据结构。栈的实现包括初始化、入栈、出栈、获取栈顶元素、检查栈是否为空以及销毁栈的操作。队列的实现则涵盖了初始化、入队、出队、获取队列头部和尾部元素、检查队列是否为空以及销毁队列的方法。示例代码详细展示了这两个数据结构的完整功能。
摘要由CSDN通过智能技术生成

栈的实现

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#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 x);    //入栈
void StackPop(Stack* ps);                   //出栈
STDataType StackTop(Stack* ps);             //获取栈顶元素
int StackSize(Stack* ps);                   //获取栈中有效元素个数
bool StackEmpty(Stack* ps);                 //检测栈是否为空,如果不为空返回0,为空返回非0
void StackDestory(Stack* ps);               //销毁栈


void StackInit(Stack* ps)                            //初始化栈
{
	assert(ps);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
void StackPush(Stack* ps, STDataType x)                             //入栈
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc::Stack");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(Stack* ps)                     //出栈
{
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
STDataType StackTop(Stack* ps)                //获取栈顶元素
{
	assert(ps);
	assert(ps->top > 0);
	STDataType num = ps->a[ps->top - 1];
	return num;
}
int StackSize(Stack* ps)                        //获取栈中有效元素个数
{
	assert(ps);
	return ps->top;
}
bool StackEmpty(Stack* ps)             //检测栈是否为空,如果不为空返回0,为空返回非0
{
	assert(ps);
	return ps->top == 0;
}
void StackDestory(Stack* ps)               //销毁栈
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void Test1()
{
	Stack st;
	StackInit(&st);
	StackPush(&st, 10);
	StackPush(&st, 11);
	StackPush(&st, 12);
	StackPush(&st, 13);
	StackPop(&st);
	StackTop(&st);
	printf("%d\n", StackTop(&st));
	StackSize(&st);
	printf("%d\n", StackSize(&st));
	while (!StackEmpty(&st))
	{
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}
	StackDestory(&st);
}
int main()
{
	Test1();
	return 0;
}

队列的实现

#define _CRT_SECURE_NO_WARNINGS
#pragma once 
#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* head;
	QNode* tail;
}Queue;
void QueueInit(Queue* q);                //初始化队列
void QueuePush(Queue* q, QDataType x);   //队尾入队列
void QueuePop(Queue* q);                 //队头出队列
QDataType QueueFront(Queue* q);          //获取队列头部元素
QDataType QueueBack(Queue* q);           //获取队列队尾元素
int QueueSize(Queue* q);                 //获取队列中有效元素个数
void QueueDestory(Queue* q);        //销毁队列
bool QueueEmpty(Queue* q);          //检测队列是否为空,如果为空返回非0结果,如果非空返回0


void QueueInit(Queue* q)                   //初始化队列
{
	assert(q);
	q->head = q->tail = NULL;
}
void QueuePush(Queue* q, QDataType x)         //队尾入队列
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("QNode::malloc");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (q->tail == NULL)
	{
		q->head = q->tail = newnode;
	}
	else
	{
		q->tail->next = newnode;
		q->tail = newnode;
	}
}
void QueuePop(Queue* q)                   //队头出队列
{
	assert(q);
	assert(!QueueEmpty(&q));
	if (q->head->next == NULL)
	{
		free(q->head);
		q->head = q->tail = NULL;
	}
	else
	{
		QNode* next = q->head->next;
		free(q->head);
		q->head = next;
	}
}
QDataType QueueFront(Queue* q)          //获取队列头部元素
{
	assert(q);
	assert(q->head);
	return q->head->data;
}
QDataType QueueBack(Queue* q)                 //获取队列队尾元素
{
	assert(q);
	assert(q->tail);
	return q->tail->data;
}
int QueueSize(Queue* q)                 //获取队列中有效元素个数
{
	assert(q);
	int count = 0;
	QNode* cur = q->head;
	while(cur)
	{
		count++;
		cur = cur->next;
	}
	return count;
}
bool QueueEmpty(Queue* q)            //检测队列是否为空,如果为空返回非0结果,如果非空返回0
{
	assert(q);
	return q->head == NULL;
}
void QueueDestory(Queue* q)        //销毁队列
{
	assert(q);
	while (q->head)
	{
		QNode* next = q->head->next;
		free(q -> head);
		q->head = next;
	}
	q->head = q->tail = NULL;
}


void TestQueue()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	while (!QueueEmpty(&q))
	{
		QueueFront(&q);
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");
	QueuePush(&q, 1);
	QueuePush(&q, 2);
    QueueBack(&q);
	printf("%d\n", QueueBack(&q));
	QueueSize(&q);
	printf("%d\n", QueueSize(&q));
	QueueDestory(&q);
}
int main()
{
	TestQueue();
	return 0;
}

🙇‍感谢大家的阅读,如有错误请指出,我们下次再见。

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁华的梦境

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值