《数据结构学习笔记---第六篇》---栈和队列的实现

本文详细介绍了顺序栈和链队的基本概念、结构以及在C语言中的具体实现,包括栈的压栈、出栈、栈顶元素获取、判空、栈大小计算和销毁,以及队列的入队、出队、队头队尾元素获取、判空、队列长度和销毁操作。
摘要由CSDN通过智能技术生成

目录

1.栈

1.1栈的概念及结构

 1.2栈的实现

 2.队列

2.1队列的概念及结构

​2.2队列的实现

 3.顺序栈的具体实现

3.1建头文Stack.h”

3.2创建具体接口实现文件Stack.c

3.2.1初始化

 3.2.2入栈出栈

3.2.4判空

3.2.5栈的大小

3.2.6销毁栈

3.3主函数的实现

 4.链队的具体实现

4.1建头文Queue.h”

4.2创建具体接口实现文件Queue.c

4.2.1打印

4.2.2初始化

 4.2.3出队入队

4.2.4返回队头队尾元素

4.2.5判空

 4.2.6返回队列长度

4.2.7销毁队列

4.3.主函数的实现


1.栈

1.1栈的概念及结构

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

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

出栈:栈的删除操作叫做出栈。出数据也在栈顶。

 1.2栈的实现

栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的 代价比较小。

 2.队列

2.1队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出 FIFO(First In First Out)

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

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

2.2队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数 组头上出数据,效率会比较低。

 3.顺序栈的具体实现

3.1建头文Stack.h”

  ​​​​​为什么要创立头文件

#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef int STDataType;
typedef struct Stack {
	STDataType*a ;
	int capacity;
	int top;
}ST;

void StackInit(ST*ps);

void StackDestroy(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);

STDataType StackTop(ST* ps);
//返回栈顶元素
bool StackEmpty(ST* ps);
//判空
int  StackSize(ST* ps);

3.2创建具体接口实现文件Stack.c

先引用#include "Stack.h

3.2.1初始化


void StackInit(ST* ps)
{
	assert(ps);
	//创建一个节点空间类似顺序表;
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		exit(-1);
		
	}
	ps->top = 0;
	ps->capacity = 4;
}

 3.2.2入栈出栈

void StackPush(ST* ps, STDataType x)

{
	assert(ps);
	if (ps->top == ps->capacity)//判断是否栈满  需要扩容
	{
		STDataType* tmp = (STDataType*)realloc(ps->a,sizeof(STDataType) * ps->capacity*2);
		if (tmp == NULL)
		{
			perror("melloc fail");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity *= 2;

	}
	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

3.2.3返回栈顶元素

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

3.2.4判空

bool StackEmpty(ST* ps)
{
	assert(ps);

	/*if (ps->top == 0)
	{
		return true;
	}
	else
	{
		return false;
	}*/
	return(ps->top == 0);
}

3.2.5栈的大小

int  StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

3.2.6销毁栈

void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

3.3主函数的实现

#include "Stack.h"

void Test1()
{
	ST st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);

	printf("size:%d\n", StackSize(&st)); // 不关心底层实现
	printf("size:%d\n", st.top + 1);  // 关心

	StackPop(&st);
	StackPop(&st);
	StackPop(&st);
	

	//StackPop(&st);
	printf("%d\n", StackTop(&st));

	StackDestroy(&st);

}


int main()
{
	Test1();
	return 0;
}

 4.链队的具体实现

4.1建头文Queue.h”

  ​​​​​为什么要创立头文件

#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef int QDataType;
typedef struct QueueNode {//创建队列结点
	QDataType data;
	struct QueueNode* next;//别学了单链表 就不会类比其他特殊链表结点的创建
	
}QNode;
typedef struct Queue {//创建队列 指针结构体
	QNode* head;
	QNode* tail;
	int size;
}Queue;

void QueueInit(Queue* ps);
void QueueDestroy(Queue* ps);
void QueuePush(Queue* ps, QDataType x);
void QueuePop(Queue* ps);
void QueuePrint(Queue* ps);
//返回队头队尾元素
QDataType Queuefront(Queue* ps);
QDataType Queueback(Queue* ps);

bool QueueEmpty(Queue* ps);
int QueueSize(Queue* ps);

4.2创建具体接口实现文件Queue.c

先引用#include "Queue.h"

4.2.1打印

void QueuePrint(Queue* ps)
{
	if (ps == NULL)

	{
		printf("NULL\n");
	}
	QNode* cur = ps->head;
	while (cur)
	{
		QNode* next = cur->next;
		printf("%d->", cur->data);
		cur = next;
	}
	printf("NULL\n");
}

4.2.2初始化

void QueueInit(Queue* ps)
{
	assert(ps);
	ps->head = NULL;
	ps->tail = NULL;
	ps->size = 0;
}

 4.2.3出队入队

void QueuePush(Queue* ps, QDataType x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (ps->tail == NULL)
	{
		ps->tail = ps->head = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = newnode;
	}
	ps->size++;
	
}

void QueuePop(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	if (ps->head->next == NULL)
	{
		free(ps->head);
		ps->head = NULL;
		ps->tail = NULL;
		ps->size = 0;
	}
	else {
		
		QNode* del = ps->head;
		
		ps->head = ps->head->next;
		free(del);
		ps->size--;
	}
}

3.2.5头插头删

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size; i >0; i--)
	{
		ps->a[i] = ps->a[i-1];
	}
	ps->a[0] = x;
	ps->size++;
}

void SLPopFront(SL* ps)
{
	assert(ps);
	for(int i = 0; i<ps->size; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

4.2.4返回队头队尾元素

QDataType Queuefront(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return (ps->head->data);
	
}
QDataType Queueback(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return (ps->tail->data);
}

4.2.5判空

bool QueueEmpty(Queue* ps)
{
	assert(ps);

	return ps->head == NULL && ps->tail == NULL;
}
}

 4.2.6返回队列长度

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

4.2.7销毁队列

void QueueDestroy(Queue* ps)
{
	assert(ps);
	QNode* cur = ps->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	ps->head = NULL;
	ps->tail = NULL;
	ps->size = 0;

}

4.3.主函数的实现

#include "Queue.h"

void Test1()

{
	Queue q;
	//初始化测试
	QueueInit(&q);
	QueuePrint(&q);
	//入队测试
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePrint(&q);
	//出队测试
	QueuePop(&q);
	QueuePop(&q);
	QueuePop(&q);
	QueuePrint(&q);
	//返回队头队尾元素
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	printf("%d----%d\n", Queueback(&q), Queuefront(&q));
	QueuePrint(&q);
	printf("%d", QueueSize(&q));
}
int main()
{

	Test1();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值