[数据结构]栈和队列结构的简单制作

一、栈

1.1栈的概念以及结构

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端 称为栈顶,另一端称为栈底。

栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

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

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

1.2栈的实现

栈可以用顺序表或者链表进行实现,但是相对而言顺序表的优势较为大,以下是栈的实现方法,因比较简单所以读者可以自行阅读代码

Stack.h文件

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

#define datatype int

typedef struct stack
{
	datatype* x;
	int top;
	int capacity;
}stack;

//初始化栈
void stackinit(stack* input);
//进栈
void stackpush(stack* input, datatype y);
//打印栈
void stackprint(stack* input);
//出栈
void stackpop(stack* input);
//检验是否为空

Stack.c文件

#include "stack.h"
//初始化栈
void stackinit(stack* input)
{
	datatype* temp = (datatype*)malloc(sizeof(datatype) * 4);
	if (temp == NULL)
	{
		perror("stackinit:");
	}
	input->x = temp;
	input->capacity = sizeof(datatype) * 4;
	input->top = 0;
}
//进栈
void stackpush(stack* input, datatype y)
{
	if (input->top + 1 == input->capacity)
	{
		datatype* temp = realloc(input->x,(input->capacity) * 2);
		input->capacity *= 2;
		if (temp == NULL)
		{
			perror("stackpush:");
		}
		input->x = temp;
	}
	input->x[input->top] = y;
	input->top++;
}
//打印栈
void stackprint(stack* input)
{
	assert(input);
	int i = input->top-1;
	while (i >= 0)
	{
		printf("%d", input->x[i]);
		i--;
	}
	printf("\n");
}
//出栈
void stackpop(stack* input)
{
	assert(input);
	input->top--;
}

test.c文件

#include "stack.h"

void test1()
{
	stack A;
	stackinit(&A);
	stackpush(&A, 1);
	stackpush(&A, 2);
	stackpush(&A, 3);
	stackpush(&A, 4);
	stackpush(&A, 5);
	stackprint(&A);
	stackpop(&A);
	stackpop(&A);
	stackpop(&A);
	stackprint(&A);
}


void main()
{
	test1();
}

二、队列

2.1队列的概念

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

2.2队列的实现

同样,队列也是一种很简单的数据结构,读者可以自行阅读代码

Queue.h文件

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

#define datatype int
typedef struct ListNode
{
	datatype x;
	struct ListNode* next;
}LN;

typedef struct Queue
{
	LN* head;
	LN* tail;
}Queue;
void Queueinit(Queue* input);
void Queuepush(Queue* input,datatype y);
void Queuepop(Queue* input);
void Queueprint(Queue* input);
int Queuesize(Queue* input);

Queue.c文件

#include "Queue.h"
void Queueinit(Queue* input)
{
	input->head = NULL;
	input->tail = NULL;
}
void Queueprint(Queue* input)
{
	assert(input);
	assert(input->head);
	LN* temp = input->head;
	while (temp)
	{
		printf("%d ->", temp->x);
		temp = temp->next;
	}
	printf("NULL\n");
}
void Queuepush(Queue* input, datatype y)
{
	assert(input);
	LN* temp1 = (LN*)malloc(sizeof(LN));
	if (temp1 == NULL)
	{
		perror("Queuepush:");
	}
	temp1->x = y;
	temp1->next = NULL;
	if ((input->head==NULL) && (input->tail==NULL) )
	{
		input->head = input->tail = temp1;
		return;
	}
	input->tail->next = temp1;
	input->tail = input->tail->next;
}
void Queuepop(Queue* input)
{
	assert(input);
	assert(input->head);
	if (input->head == input->tail)
	{
		free(input->head);
		input->head = NULL;
		input->tail = NULL;
		return;
	}
	LN* temp = input->head->next;
	free(input->head);
	input->head = temp;
}
int Queuesize(Queue* input)
{
	int count = 0;
	LN* temp = input->head;
	while (temp)
	{
		temp = temp->next;
		count++;
	}
	return count;
}

test.c文件

#include "Queue.h"

void test1()
{
	Queue A;
	Queueinit(&A);
	Queuepush(&A, 1);
	Queuepush(&A, 4);
	Queuepush(&A, 3);
	Queuepush(&A, 2);
	Queueprint(&A);
	Queuepop(&A);
	Queuepop(&A);
	Queuepop(&A);
	Queuepop(&A);
	Queuepush(&A, 2);
	Queueprint(&A);
	printf("%d",Queuesize(&A));
}


void main()
{
	test1();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值