栈和队列的实现

目录

概念:

数组实现:

结构:

初始化:

入栈:

出栈:

获取栈顶元素:

获取栈有效的元素个数:

栈是否为空,为空返回真:

销毁:

全部代码:

stack.h:

stack.c:

测试:

队列:

概念:

链表实现:

结构:

初始化:

队尾入列:

队头出列:

获取队头元素:

获取队尾元素:

获取队列中的有效元素个数:

队列是否为空,为空返回真:

销毁:

全部代码:

QNode.h

QNode.c

测试:


概念:

栈是一种特殊的线性表,其结构的特性和内存中的栈是一样的,只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。

5先放入,再依次当如1和8,出栈时只能从8开始出栈,后进入的先出。8  1  5

数组实现:

栈是可以使用链表或数组实现,根据栈的性质:后入先出,那就是尾插和尾删,数组的时间复杂度是O(1),链表的时间复杂度是O(N),数组是更有性价比的。

结构:

typedef char STDataType;
typedef struct Stack
{
	STDataType* _a;
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;

_top:记录当前栈的元素个数

_capacity:记录开辟了多大的空间

_a:存储数据

初始化:

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 newcap = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
		STDataType* new_a = (STDataType*)realloc(ps->_a, sizeof(STDataType) * newcap);
		if (new_a == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->_a = new_a;
		ps->_capacity = newcap;
	}
	ps->_a[ps->_top] = data;
	ps->_top++;
}

开辟内存和顺序表的思路一样,_capacity为0开辟4个字节,不为0开辟原内存大小的二倍。_top是元素的个数,也是栈顶元素下一个元素的下标,所以直接在下标_top位置插入。

出栈:

void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	ps->_top--;
}

出栈时不需要修改值,在次入栈到出过栈的那块空间时,会重新放入元素。

获取栈顶元素:

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	return ps->_a[ps->_top-1];
}

前面说过_top是栈顶元素下一个元素的下标,_top-1也就是栈顶元素的下标。返回前要判断栈是否为空。

获取栈有效的元素个数:

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->_top;
}

栈是否为空,为空返回真:

int StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->_top == 0;
}

销毁:

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

全部代码:

stack.h:
#pragma once
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
// 支持动态增长的栈
typedef char 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 
int StackEmpty(Stack* ps);
// 销毁栈 
void StackDestroy(Stack* ps);
stack.c:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#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 newcap = ps->_capacity == 0 ? 4 : ps->_capacity * 2;
		STDataType* new_a = (STDataType*)realloc(ps->_a, sizeof(STDataType) * newcap);
		if (new_a == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->_a = new_a;
		ps->_capacity = newcap;
	}
	ps->_a[ps->_top] = data;
	ps->_top++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	ps->_top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->_top > 0);
	return ps->_a[ps->_top-1];
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->_top;
}

int StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->_top == 0;
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}
测试:
void test()
{
	Stack pc;
	StackInit(&pc);
	StackPush(&pc, 1);
	StackPush(&pc, 2);
	printf("%d ", StackTop(&pc));
	StackPop(&pc);
	StackPush(&pc, 3);
	StackPush(&pc, 4);
	printf("%d ", StackTop(&pc));
	StackPop(&pc);
	StackPush(&pc, 5);
	while (!StackEmpty(&pc))
	{
		printf("%d ", StackTop(&pc));
		StackPop(&pc);
	}
	StackDestroy(&pc);
}

在入栈是时,可以在中途出栈,这样原本顺序为5 4 3 2 1,改为2 4 5 3 1。在遍历时以栈是否为空来作为循环的结束条件。

队列:

概念:

队列也是一种线性表,它和栈的性质相反,队列为先进先出。只允许在一端进行插入数据操作,在另一端进行删除数据操作。

入队列:进行插入操作的一端称为队尾

出队列:进行删除操作的一端称为队头

链表实现:

队列也可以使用数组和链表进行实现,不过数组无论头部作为出还是入都是不方便的。使用链表让头节点当队头,尾节点当队尾,创建时用一个指针来指向队尾就方便入队了。不需要尾删链表还是很方便的。

结构:

// 链式结构:表示队列 
typedef int QDataType;
typedef struct QListNode
{
	struct QListNode* _next;
	QDataType _data;
}QNode;

// 队列的结构 
typedef struct Queue
{
	QNode* _front;
	QNode* _rear;
	int size;
}Queue;

_front:指向头节点

_rear:指向尾节点

size:存储有效的元素个数

初始化:

void QueueInit(Queue* q)
{
	assert(q);
	q->_front = q->_rear = NULL;
	q->size = 0;
}

队尾入列:

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* top = (QNode*)malloc(sizeof(QNode));
	if (top == NULL)
	{
		perror("malloc fail");
		return;
	}
	top->_data = data;
	top->_next = NULL;
	q->size++;
	if (q->_front == NULL)
	{
		q->_front = q->_rear = top;
		return;
	}
	q->_rear->_next = top;
	q->_rear = top;
}

创建一个节点top,top的next要指向NULL,它插入后要作为尾节点。使_rear的next指向top,top这时就为尾节点,_rear再指向top,保证_rear永远为尾节点。插入top前要判断队列是否为空,为空_front和_rear直接指向top。

队头出列:

void QueuePop(Queue* q)
{
	assert(q);
	assert(q->_front);
	QNode* top = q->_front->_next;
	free(q->_front);
	q->_front = top;
	q->size--;
	if (q->_front == NULL)
		q->_rear = NULL;
}

队列不为空再进行出列删除,存储_front的下一个节点,它将作为头节点,然后释放掉_front,_front再指向存储的节点,如果此时_front为空,那队列此时就是空队列,_rear也要指向空。

获取队头元素:

QDataType QueueFront(Queue* q)
{
	assert(q && q->_front);
	return q->_front->_data;
}

获取队尾元素:

QDataType QueueBack(Queue* q)
{
	assert(q && q->_rear);
	return q->_rear->_data;
}

获取队列中的有效元素个数:

int QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}

队列是否为空,为空返回真:

int QueueEmpty(Queue* q)
{
	assert(q);
	return q->_front == NULL;
}

销毁:

void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* cur = q->_front;
	while (cur)
	{
		q->_front = q->_front->_next;
		free(cur);
		cur = q->_front;
	}
	q->_rear = q->_front = NULL;
	q->size = 0;
}

和链表一样遍历一遍释放所有节点,队列内的变量在置为0。

全部代码:

QNode.h
#pragma once
#include<assert.h>
#include<stdlib.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 
int QueueEmpty(Queue* q);
// 销毁队列 
void QueueDestroy(Queue* q);
QNode.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"Queue.h"
void QueueInit(Queue* q)
{
	assert(q);
	q->_front = q->_rear = NULL;
	q->size = 0;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* top = (QNode*)malloc(sizeof(QNode));
	if (top == NULL)
	{
		perror("malloc fail");
		return;
	}
	top->_data = data;
	top->_next = NULL;
	q->size++;
	if (q->_front == NULL)
	{
		q->_front = q->_rear = top;
		return;
	}
	q->_rear->_next = top;
	q->_rear = top;
}

void QueuePop(Queue* q)
{
	assert(q);
	assert(q->_front);
	QNode* top = q->_front->_next;
	free(q->_front);
	q->_front = top;
	q->size--;
	if (q->_front == NULL)
		q->_rear = NULL;
}

QDataType QueueFront(Queue* q)
{
	assert(q && q->_front);
	return q->_front->_data;
}

QDataType QueueBack(Queue* q)
{
	assert(q && q->_rear);
	return q->_rear->_data;
}

int QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}

int QueueEmpty(Queue* q)
{
	assert(q);
	return q->_front == NULL;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* cur = q->_front;
	while (cur)
	{
		q->_front = q->_front->_next;
		free(cur);
		cur = q->_front;
	}
	q->_rear = q->_front = NULL;
	q->size = 0;
}
测试:
void test()
{
	Queue s;
	QueueInit(&s);
	QueuePush(&s, 1);
	QueuePush(&s, 2);
	printf("%d ", QueueFront(&s));
	QueuePop(&s);
	QueuePush(&s, 3);
	QueuePush(&s, 4);
	printf("%d ", QueueFront(&s));
	QueuePop(&s);
	QueuePush(&s, 5);
	while (!QueueEmpty(&s))
	{
		printf("%d ", QueueFront(&s));
		QueuePop(&s);
	}
	QueueDestroy(&s);
}

队列中入队数据的顺序和出队数据的顺序是一样的不可能改变。

  • 23
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值