数据结构—栈与队列

介绍

栈(stack)

栈就是有特殊规则限定的线性表,栈的插入和删除只能在表尾进行,栈的表尾叫做栈顶,表头叫栈底,不含元素的空表叫空栈,所以栈的原则是后进先出,(类似于子弹上膛,子弹为数据,弹匣为栈,取子弹先取后面上的)

队列(queue)

队列是一种先进先出的线性表,他只允许在表的一端插入,在另一端删除元素,允许插入的一端叫队尾,允许删除的一端叫队头

顺序结构的栈

复制代码
#include"stdio.h"
#include"stdlib.h"

typedef int  ElemType ;
#define MAXSIZE 100


//mode==0,表示在栈内存创建
//mode==1,表示在堆内存中创建
#define mode 0;


#if mode==1
	typedef struct stack
	{
		//创建数组存放数据
		ElemType date[MAXSIZE];
		//栈顶指针,这里的top代表数组下标
		int top;
	}Stack;

	void Stack_Init(Stack * S)
	{
		//此时栈没有数据,数组下标0位置没有数据
		S->top = -1;
	}
#else mode ==0

//在堆内存中创建顺序结构的栈
typedef struct stack
{
	//创建数组存放数据
	ElemType *date;
	//栈顶指针,这里的top代表数组下标
	int top;
}Stack;

 Stack* Stack_Init()
{
	Stack* S = (Stack*)malloc(sizeof(Stack));
	S->date = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
	S->top = -1;
	return S;
}

#endif


//进栈
int  Push(Stack* S, ElemType e)
{
	if (S->top == MAXSIZE-1)
	{
		printf("栈满了\n");
		return 0;
	}
	//栈顶指针+1,在数组下标为0地方写数据
	S->top++;
	S->date[S->top] = e;
	return 1;
}

//出栈
int Pop(Stack* S, ElemType *e)
{
	if (S->top == -1)
	{
		printf("空栈\n");
		return 0;
	}
	//纪录一下出栈的数据
	*e = S->date[S->top];
	//栈顶指针-1
	S->top--;
	return 1;
}

//获取栈顶元素
int  GetTop(Stack* S, ElemType* e)
{
	if (S->top == -1)
	{
		printf("空栈\n");
		return 0;
	}
	*e = S->date[S->top];
	return 1; 
}

链式结构的栈

复制代码
#include"stdio.h"
#include"stdlib.h"

typedef int ElemType;

typedef struct stack
{
	ElemType data;
	struct stack* next;
}Stack;

Stack* Stack_Init(void)
{
	Stack* S = (Stack*)malloc(sizeof(Stack));
	S->next = NULL;
	S->data = 0;
	return S;
}
//判断栈是否为空
int IsEmpty(Stack* S)
{
	if (S->next == NULL)
	{
		printf("栈为空\N");
		return 0;
	}
	return 1;
}

//进栈
void Push_Stack(Stack* S, ElemType e)
{
	Stack* s = (Stack*)malloc(sizeof(Stack));
	s->data = e;
	s->next = S->next;
	S->next = s;
}

//出栈
int Pop_Stack(Stack* S,ElemType	*e)
{
	//判断是不是空栈
	if (IsEmpty(S) == 0)
	{
		return 0;
	}

	Stack* L = S->next->next;
	//出栈的数据
	*e = S->next->data;
	free(S->next);
	S->next = L;
	return 1;
}
//获取栈顶元素
int Getpot(Stack* S,ElemType *e)
{
	//判断是不是空栈
	if (IsEmpty(S) == 0)
	{
		return 0;
	}
	
	*e = S->next->data;
	return 1;
}

队列

顺序结构的队列

复制代码
#include"stdio.h"
#include "stdlib.h"


#define mode 0
#if mode

typedef int ElemType;
#define MAXSIZE	 100

typedef struct queue
{
	ElemType data[MAXSIZE];

	//队头
	int front ;
	//队尾
	int rear ;

}Queue;

//队列初始化
void Queue_Init(Queue* Q)
{
	Q->front = 0;
	Q->rear = 0;
}

#else mode==1

typedef int ElemType;
#define MAXSIZE	 100

typedef struct queue
{
	ElemType* data;
	int front;
	int rear;
}Queue;

//队列初始化
Queue* Queue_Init(void)
{
	Queue* Q = (Queue*)malloc(sizeof(Queue));
	Q->data = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
	Q->front = 0;
	Q->rear = 0;
	return Q;
}
#endif // 0




//调整队列
int Queue_Full(Queue* Q)
{
	
	if (Q->front > 0)
	{
		int a = 0;
		for (int i = Q->front;i<Q->rear;i++)
		{
			Q->data[a] = Q->data[i];
			a++;
		}
		Q->front = 0;
		Q->rear = a+1;
		return 1;
	}
	else
	{
		printf("队列满了\n");
		return 0;
	}
}

//入队
int eQueue(Queue* Q, ElemType e)
{
	if (Q->rear>=MAXSIZE)
	{
		if (Queue_Full(Q) == 0)
		{
			return 0;
		}
	}
	else
	{
		Q->data[Q->rear] = e;
		Q->rear++;
		return 1;
	}
}
//出队
ElemType deQueue(Queue* Q)
{
	if (Q->front == Q->rear)
	{
		printf("队列无元素\n");
	}
	else
	{
		
		Q->front++;
		return 	Q->data[Q->front-1];
	}
}

//获取队头元素
int GetHead(Queue* Q, ElemType *e)
{
	if (Q->front == Q->rear)
	{
		return 0;
	}
	*e = Q->data[Q->front];
	return 1;
}

void main(void)
{
	Queue* q = Queue_Init();
	eQueue(q, 10);
	eQueue(q, 20);
	eQueue(q, 30);
	eQueue(q, 40);
	eQueue(q, 50);
	eQueue(q, 60);
	printf("%d\n",deQueue(q));
	printf("%d\n", deQueue(q));
	ElemType e;
	GetHead(q, &e);
	printf("%d\n", e);
}

链式结构的队列

复制代码
#include"stdio.h"
#include "stdlib.h"

typedef int ElemType;

typedef struct queuenode
{
	ElemType data;
	struct queuenode* next;

}QueueNode;

typedef struct queue
{
	QueueNode* front;
	QueueNode* rear;

}Queue;

//初始化
Queue* Queue_Init(void)
{
	QueueNode* QN = (QueueNode*)malloc(sizeof(QueueNode));
	Queue* Q = (Queue*)malloc(sizeof(Queue));
	Q->front = QN;
	Q->rear =QN;
	QN->next = NULL;
	QN->data = 0;
	return Q;
}

//入队
int Queue_Create_Node(Queue* L,ElemType e)
{
	QueueNode* q = (QueueNode*)malloc(sizeof(QueueNode));
	q->data = e;
	q->next = NULL;
	L->rear->next = q;
	L->rear = q;
	return q->data;
}

//出队
void Queue_Dele(Queue* Q,ElemType *e)
{
	if (Q->front == Q->rear)
	{
		printf("队列无数据\n");
	}
	else if (Q->front->next == Q->rear)
	{
		*e = Q->front->next->data;
		Q->rear = Q->front;
		free(Q->front->next);
	}
	else
	{
		*e = Q->front->next->data;
		QueueNode* S = Q->front->next->next;
		free(Q->front->next);
		Q->front->next = S;
	}
}

//获取队头
int  GetFront(Queue* Q, ElemType *e)
{
	if (Q->front == Q->rear)
	{
		printf("队列无数据\n");
		return 0;
	}
		*e = Q->front->next->data;
		return 1;
}
void main(void)
{
	Queue* Q = Queue_Init();
	Queue_Create_Node(Q, 10);
	Queue_Create_Node(Q, 20);
	Queue_Create_Node(Q, 30);
	Queue_Create_Node(Q, 40);
	int a=0, c=0, b=0,f=0;
	Queue_Dele(Q, &a);
	Queue_Dele(Q, &b);
	Queue_Dele(Q, &c);
	printf("%d %d %d\n", a, b, c);

	GetFront(Q,&f);
	printf("%d\n", f);

}
相关推荐
垫脚摸太阳2 小时前
二分查找经典算法题--数的范围
数据结构·算法
噜啦噜啦嘞好2 小时前
算法篇:二分查找
数据结构·c++·算法·leetcode
季明洵2 小时前
回溯介绍及实战
java·数据结构·算法·leetcode·回溯
minji...2 小时前
Linux 进程间通信(一)进程间通信与匿名管道
linux·运维·服务器·数据结构·数据库·c++
会编程的土豆2 小时前
【数据结构与算法】LCS刷题
数据结构·算法·动态规划
Jasmine_llq2 小时前
《B4258 [GESP202503 一级] 四舍五入》
数据结构·算法·整数运算实现四舍五入整十数算法·批量输入遍历算法·逐行输出算法·整数算术运算组合算法·顺序输入处理算法
j_xxx404_2 小时前
力扣--分治(归并排序)算法题I:排序数组,交易逆序对的总数
数据结构·c++·算法·leetcode·排序算法
sprite_雪碧3 小时前
排版类问题(机试高频)
c语言·数据结构·算法
北顾笙9803 小时前
测开准备-day02数据结构力扣
数据结构