栈和队列的基本操作

栈的头文件Stack.h

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

void Stackinit(Stack* ps);//栈的初始化
void Stackpush(Stack* ps, Stackdatatype x);//进栈
void Stackpop(Stack* ps);//出栈
Stackdatatype Stacktop(Stack* ps);//返回栈头的位置
int Stacksize(Stack* ps);//求栈的长度
bool Stackempty(Stack* ps);//判断栈是否为空
void Stackprint(Stack* ps);
void Stackdestroy(Stack* ps);

队列的头文件Queue.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int Queuedadatype;
typedef struct Queue
{
	struct Queue* head;
	struct Queue* tail;
	struct Queue* next;
	Queuedadatype val;
}Queue;

void Queueinit(Queue* pq);//队列的初始化
void Queuedestory(Queue* pq);
void Queuepush(Queue* pq, Queuedadatype x);//入队列
void Queuepop(Queue* pq);//出队列
Queuedadatype Front(Queue* pq);//返回队列的头
Queuedadatype Back(Queue* pq);//返回队列的尾
int Size(Queue* pq);//求队列的长度
bool Queueempty(Queue* pq);//判断队列是否为空
void Queueprint(Queue* pq);

栈的源文件Stack.c

#include"Stack.h"
void Stackinit(Stack* ps)
{
	assert(ps);
	ps->a = (Stackdatatype*)malloc(sizeof(Stackdatatype)*4);
	if (ps->a== NULL)
		{
			printf("raelloc fail\n");
			exit(-1);
		}
	ps->top = 0;
	ps->capacity = 4;
}

void Stackpush(Stack* ps, Stackdatatype x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity * 2;
		Stackdatatype* tmp = realloc(ps->a, sizeof(Stack)*newcapacity);
		if (tmp == NULL)
		{
			printf("raelloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void Stackprint(Stack* ps)
{
	assert(ps);
	int size = Stacksize(ps);
	for (int i = 0; i < size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

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

void Stackpop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}

Stackdatatype Stacktop(Stack* ps)
{
	assert(ps);
	assert(!Stackempty(ps));
	return ps->a[ps->top - 1];
}

bool Stackempty(Stack* ps)
{
	assert(ps);
	if (ps->top == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

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

队列的源文件Queue.c

#include"Queue.h"
void Queueinit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->next = NULL;
	pq->val = 0;
}

void Queuedestory(Queue* pq)
{
	assert(pq);
	Queue* cur = pq->head;
	while (cur)
	{
		Queue* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}

void Queuepush(Queue* pq,Queuedadatype x)
{
	assert(pq);
	Queue* newnode = (Queue*)malloc(sizeof(Queue));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->val = x;
	newnode->next = NULL;
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}

void Queuepop(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		Queue* newhead = pq->head->next;
		free(pq->head);
		pq->head = newhead;
	}
}

Queuedadatype Front(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->val;
}

Queuedadatype Back(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->tail->val;
}

int Size(Queue* pq)
{
	assert(pq);
	Queue* cur = pq->head;
	int size = 0;
	while (cur)
	{
		cur = cur->next;
		++size;
	}
	return size;
}

bool Queueempty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}

void Queueprint(Queue* pq)
{
	assert(pq);
	Queue* cur = pq->head;
	while (cur)
	{
		printf("%d ", cur->val);
		cur = cur->next;
	}
	printf("\n");
}

test.c

#include"Stack.h"
#include"Queue.h"
void test1()
{
	Stack ps;
	Stackinit(&ps);
	Stackpush(&ps, 1);
	Stackpush(&ps, 2);
	Stackpush(&ps, 3);
	Stackpush(&ps, 4);
	Stackprint(&ps);
	Stackpop(&ps);
	Stackpop(&ps);
	Stackprint(&ps);
}

void test2()
{
	Stack ps;
	Stackinit(&ps);
	Stackpush(&ps, 1);
	Stackpush(&ps, 2);
	Stackpush(&ps, 3);
	Stackpush(&ps, 4);
	printf("%d ", Stacktop(&ps));
	Stackpop(&ps);
	printf("%d ", Stacktop(&ps));
	Stackpop(&ps);
	Stackpush(&ps, 5);
	Stackpush(&ps, 6);
	while (!Stackempty(&ps))
	{
		printf("%d ", Stacktop(&ps));
		Stackpop(&ps);
	}
	printf("\n");
}

void test3()
{
	Queue pq;
	Queueinit(&pq);
	Queuepush(&pq, 1);
	Queuepush(&pq, 2);
	Queuepush(&pq, 3);
	Queuepush(&pq, 4);
	Queueprint(&pq);
	Queuepop(&pq);
	Queuepop(&pq);
	Queueprint(&pq);
}

void test4()
{
	Queue pq;
	Queueinit(&pq);
	Queuepush(&pq, 1);
	Queuepush(&pq, 2);
	Queuepush(&pq, 3);
	Queuepush(&pq, 4);
	printf("%d ", Front(&pq));
	Queuepop(&pq);
	printf("%d ", Front(&pq));
	Queuepop(&pq);
	Queuepush(&pq, 10);
	Queuepush(&pq, 20);
	printf("\n%d\n", Size(&pq));
	while (!Queueempty(&pq))
	{
		printf("%d ", Front(&pq));
		Queuepop(&pq);
	}
	printf("\n");
	printf("%d\n", Size(&pq));
	Queuedestory(&pq);
}

int main()
{
	//test1();
	//test2();
	//test3();
	test4();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值