快速学习“栈“与“队列“(C语言)

前言:

        接触过链表和顺序表之后,接下来继续深入学习另外两种特殊的线性表:

栈:


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


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


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

注:数据结构中的栈不同于内存中的栈区

数据结构中的栈是存储数据的一种方式。

内存中的栈是一种物理结构。

栈的模拟实现:

模拟实现栈有两种方式:

1、链式栈

2、数组栈

也就是可以用链表实现栈。也可以用顺序表谁先栈

那么究竟用谁来实现栈呢?

首先我们来看看栈的使用情景:

当然也就是说我要实现一个栈,必须要满足:

如果是头插就必须是头删。

如果是尾插就必须是尾删。

满足后进先出!!

因此两种栈都可以选用!

链式栈:

由于链表头插头删比较方便,因此采用头插头删:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#define STDataType int 
typedef struct StackNode
{
	struct StackNode* next;
	STDataType value;
}STNode;
//入栈
void StackPush(STNode** ps, STDataType data);
// 出栈
void StackPop(STNode** ps);
// 获取栈顶元素
STDataType StackTop(STNode* ps);
// 获取栈中有效元素个数
int StackSize(STNode* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(STNode* ps);
// 销毁栈
void StackDestroy(STNode** ps);
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void StackPush(STNode** ps, STDataType data)
{
	assert(ps);
	//扩容
	STNode* tmp = (STNode*)malloc(sizeof(STNode));
	if (tmp == NULL)
	{
		perror("malloc::");
		exit(-1);
	}
	tmp->value = data;
	tmp->next = NULL;
	//头插
	if (*ps == NULL)
	{
		*ps = tmp;
	}
	else
	{
		tmp->next = *ps;
		*ps = tmp;
	}
}
void StackPop(STNode** ps)
{
	assert(ps);
	assert(*ps);
	STNode* tmp = (*ps)->next;
	free(*ps);
	*ps = tmp;
}
STDataType StackTop(STNode* ps)
{
	assert(ps);
	return ps->value;
}
// 获取栈中有效元素个数
int StackSize(STNode* ps)
{
	int tmp = 0;
	while (ps)
	{
		tmp++;
		ps = ps->next;
	}
	return tmp;
}
int StackEmpty(STNode* ps)
{
	return (ps == NULL);
}
// 销毁栈
void StackDestroy(STNode** ps)
{
	assert(ps);

	while (*ps)
	{
		STNode* tmp = (*ps)->next;
		free(*ps);
		*ps = tmp;
	}
}#include"Stack.h"
void test1()
{
	STNode* head = NULL;
	StackPush(&head, 1);
	StackPush(&head, 2);
	StackPush(&head, 3);
	StackPush(&head, 4);
	while (!StackEmpty(head))
	{
		printf("%d ", StackTop(head));
		StackPop(&head);
	}
	StackDestroy(&head);
}

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

数组栈:

定义结构体:
 

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#define STDataType int 
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}Stack;

初始化栈:

void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	//top指向栈顶的下一个位置
	ps->top = 0;
	//top指向栈顶的当前位置
	//ps->top = -1;
}

在初始化栈的时候有几个问题:

1、哪边是栈顶哪边是栈底。

2、这个指向栈顶的top初始化的时候应该初始化为多少。

由于数组的尾插尾删比较方便,所以优先采用尾删尾插的形式。

因此确定了哪边是栈顶,哪边是栈底。

由于需要区分栈里面是否有元素,也就是当top = 0时栈里面究竟有没有元素有点争议。

此时有两种方式:

1、当栈里面没有元素是让top = -1

2、直接让top指向栈顶的下一个位置。

入栈:

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		int newcapacity = (ps->capacity == 0 ? 4 : ps->capacity * 2);
		STDataType* tmp = (STDataType*)realloc(ps->a,newcapacity*sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("malloc::");
			exit(-1);
		}
		ps->a = tmp;
        ps->capacity = newcapacity;
	}
	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];
}

注:由于top指向的是栈顶的下一个位置的元素,所以返回的时候注意下标要减1。

 获取栈中有效元素个数:

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

检测栈是否为空:

如果为空返回非零结果,如果不为空返回0

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

销毁栈: 

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

完整代码:

void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	//top指向栈顶的下一个位置
	ps->top = 0;
	//top指向栈顶的当前位置
	//ps->top = -1;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		int newcapacity = (ps->capacity == 0 ? 4 : ps->capacity * 2);
		STDataType* tmp = (STDataType*)realloc(ps->a,newcapacity*sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("malloc::");
			exit(-1);
		}
		ps->a = tmp;
        ps->capacity = newcapacity;
	}
	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;
}

队列:

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

队列的模拟实现:

由于队列要满足先进先出,也就是头插尾删或者是头删尾插。

要用数组实现队列不太方便,

这里直接采用用链表实现队列。

链式队列:


#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#define	QDataType int
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType value;
}QN;
typedef struct Queue
{
	QN* front;
	QN* rear;
}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);

#include"Queue.h"
void QueueInit(Queue* q)
{
	assert(q);
	q->front = q->rear = NULL;
}
// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QN* tmp = (QN*)malloc(sizeof(QN));
	if (tmp == NULL)
	{
		perror("malloc:");
		exit(-1);
	}
	tmp->next = NULL;
	tmp->value = data;
	if (q->front == NULL)
	{
		q->front = q->rear = tmp;
	}
	else
	{
		q->rear->next = tmp;
		q->rear = tmp;
	}
}
void QueuePop(Queue* q)
{
	assert(q);
	assert(q->rear);
	QN* tmp = q->front->next;
	free(q->front);
	q->front = tmp;
}
QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(q->front);
	return q->front->value;
}
// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(q->rear);
	return q->rear->value;
}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
	assert(q);
	int tmp = 0;
	QN* ptr = q->front;
	while (ptr)
	{
		tmp++;
		ptr = ptr->next;
	}
	return tmp;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0 
int QueueEmpty(Queue* q)
{
	assert(q);
	return (q->front == NULL);
}
// 销毁队列
void QueueDestroy(Queue* q)
{
	assert(q);
	while (q->front)
	{
		QN* tmp = q->front->next;
		free(q->front);
		q->front = tmp;
	}
	q->rear = NULL;
}

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值