C语言——两个栈实现一个队与两个队实现一个栈

C语言——两个栈实现一个队与两个队实现一个栈

两个栈实现一个队

定义栈的头文件

#ifndef _STACK_H
#define _STACK_H

enum res
{
	FULL=-2,
	EMPTY,
	StackNULL,
	OK
};
//定义数据元素
typedef int data_type;
//定义栈
typedef struct stack
{
	data_type * p_data;//数据存储首地址
	int size;//最大个数
	int top;//栈顶下标
}Stack;
//创建栈,输入size,返回结构体首地址
Stack * createStack(int size);


//入栈
int pushStack(Stack * pStack,data_type item);

//展示
int showStack(Stack * pStack);

//出栈
int popStack(Stack * pStack,data_type * pItem);

//销毁
int destroyStack(Stack ** ppStack);

//菜单
void menu();


int outStackQueue(Stack * pStack1,Stack * pStack2);


#endif

定义栈函数

栈函数中实现了栈的添加、删除、销毁、查看

#include <stdio.h>
#include "../include/stack.h"
#include <stdlib.h>
#include <string.h>


//创建栈
Stack * createStack(int size)
{
		//动态分配栈大小
		//结构体分配空间
		Stack * pStack=NULL;
		pStack=(Stack *)malloc(sizeof(Stack));
		//判断空间是否分配成功
		if(pStack == NULL)
		{
			perror("malloc error");
			return NULL;
		}
		//重置空间,首地址,值,大小
		memset(pStack,0,sizeof(Stack));
		// 设置栈顶
		pStack->top=-1;
		//分配数据空间
		//开辟数据的空间,结构体中的p_data为其首地址
		pStack->p_data=(data_type *)malloc(size*sizeof(data_type));
		if(pStack->p_data == NULL)
		{
			perror("malloc error");
			return NULL;
		}
		pStack->size=size;
		return pStack;
}
//插入
int pushStack(Stack * pStack,data_type item)
{
	if(pStack == NULL)
	{
		return StackNULL;
	}
	
	if(pStack->top==pStack->size-1)
	{
		return FULL;
	}

	//栈顶下标
	pStack->top++;
	//栈顶添加元素
	pStack->p_data[pStack->top]=item;
	return OK;
}
//展示
int showStack(Stack * pStack)
{
	if(pStack == NULL)
	{
		return StackNULL;
	}
	int i=0;
	for(i=0;i<=pStack->top;i++)
	{
		printf("%5d",pStack->p_data[i]);
	}
	printf("\n");
}
//出栈
//Pitem:删除后保存的数据
int popStack(Stack * pStack,data_type * Pitem)
{
	if(pStack == NULL)
	{
		return StackNULL;
	}
	if(pStack->top==-1)
	{
		return EMPTY;
	}
	*Pitem=pStack->p_data[pStack->top];
	pStack->top--;
	return OK;
}
//销毁空间
int destroyStack(Stack ** ppStack)
{
	if(*ppStack == NULL)
	{
		return StackNULL;
	}
	free((*ppStack)->p_data);
	free(*ppStack);
	*ppStack=NULL;
	return OK;
}

实现两个栈模拟一个队

#include <stdio.h>
#include "../include/stack.h"



int main()
{	
	Stack * pStack1=NULL;
	Stack * pStack2=NULL;
	int size=0;
	int op=0;
	data_type item;
	printf("请输入您要创建的队的大小\n");
	scanf("%d",&size);
	//创建栈
	pStack1=createStack(size);
	pStack2=createStack(size);
	while(1)
	{
		printf("请输入选项:------1:入队/2:出队\n");
		scanf("%d",&op);
		if(op == -1)break;
		switch(op)
		{
			case 1:
				//入队
				printf("请输入你要插入的元素\n");
				scanf("%d",&item);
				pushStack(pStack1,item);
				break;
			
			case 2:
				//出队
				outStackQueue(pStack1,pStack2);
				
				break;
		}
		showStack(pStack1);
		showStack(pStack2);
	}
	return 0;

}
//出队

int outStackQueue(Stack * pStack1,Stack * pStack2)
{	
	
	data_type q_item,p_item;
	if((pStack1->top)==0) 
	{
		popStack(pStack1,&q_item);
		return OK;
	}
	while(1)
	{
		//出1
		popStack(pStack1,&q_item);
		//入2
		pushStack(pStack2,q_item);
		if((pStack1->top)==-1) break;
	}
	//出2
	popStack(pStack2,&p_item);
	printf("出队======%d\n",p_item);
	//入1
	
	while(1)
	{
		if((pStack2->top)==-1) break;
		//出2
		popStack(pStack2,&p_item);
		//入1
		pushStack(pStack1,p_item);
		
	}
	
	return OK;
}

两个队实现一个栈

定义队的头文件

#ifndef _QUEUE_H
#define _QUEUE_H

enum res
{
	EMPTY=-4,
	FULL,
	POSERR,
	QUEUENULL,
	OK
};
//定义数据元素
typedef int data_type;
//定义顺序队列
typedef struct queue
{
	data_type * p_data;//数据存储首地址
	int size;//最大个数
	int front;//队头
	int rear;//队尾始终指向下一个入队元素
}Queue;
//创建顺序队列,输入size,返回结构体首地址
Queue * createQueue(int size);


//插入
int inQueue(Queue * pQueue,data_type item);

//展示
int showQueue(Queue * pQueue);

//删除
int outQueue(Queue * pQueue,data_type * Pitem);

//销毁
int destroyQueue(Queue ** ppQueue);



//菜单
void menu();

//模拟出栈
int outQueueStack(Queue * pQueue1,Queue * pQueue2);


#endif

定义队函数

#include <stdio.h>
#include "../include/queue.h"
#include <stdlib.h>
#include <string.h>


//创建顺序表
Queue * createQueue(int size)
{
		//动态分配顺序表大小
		//结构体分配空间
		Queue * pQueue=NULL;
		pQueue=(Queue *)malloc(sizeof(Queue));
		//判断空间是否分配成功
		if(pQueue == NULL)
		{
			perror("malloc error");
			return NULL;
		}
		//重置空间,首地址,值,大小
		memset(pQueue,0,sizeof(Queue));
		//对头与对尾
		//分配数据空间
		//开辟数据的空间,结构体中的p_data为其首地址
		pQueue->p_data=(data_type *)malloc(size*sizeof(data_type));
		if(pQueue->p_data == NULL)
		{
			perror("malloc error");
			return NULL;
		}
		pQueue->size=size;
		pQueue->front=pQueue->size-1;
		pQueue->rear=pQueue->size-1;
		return pQueue;
}
//插入,在队尾插入
int inQueue(Queue * pQueue,data_type item)
{
	if(pQueue == NULL)
	{
		return POSERR;
	}
	if((pQueue->rear+1)%pQueue->size==pQueue->front)
	{
		printf("队满\n");
		return FULL;
	}
	//插入
	//队尾指向下一个入队元素
	pQueue->rear=(pQueue->rear+1)%(pQueue->size);
	pQueue->p_data[pQueue->rear]=item;
	return OK;
}
//展示
int showQueue(Queue * pQueue)
{
	if(pQueue == NULL)
	{
		return POSERR;
	}
	if((pQueue->front)==(pQueue->rear))
	{
		printf("队空\n");
	 	return QUEUENULL;
	}
	int i=0;
	for(i=0;i<(pQueue->rear+1)%(pQueue->size);i++)
	{
		printf("%5d",pQueue->p_data[(pQueue->front+1)%(pQueue->size)+i]);
	}
	printf("\n");
}
//出队:队头删除,每删除一个元素,队头加1;
//Pitem:删除后保存的数据
int outQueue(Queue * pQueue,data_type * Pitem)
{
	if(pQueue == NULL)
	{
		return POSERR;
	}
	if((pQueue->front)==(pQueue->rear))
	{
		printf("队空\n");
	 	return QUEUENULL;
	}
	pQueue->front=(pQueue->front+1)%pQueue->size;
	*Pitem=pQueue->p_data[pQueue->front];

	return OK;
}
//销毁空间
int destroyQueue(Queue ** ppQueue)
{
	if(*ppQueue == NULL)
	{
		return POSERR;
	}
	free((*ppQueue)->p_data);
	free(*ppQueue);
	*ppQueue=NULL;
	return OK;
}

实现两个队模拟一个栈

#include <stdio.h>
#include "../include/queue.h"



int main()
{	
	Queue * pQueue1=NULL;
	Queue * pQueue2=NULL;
	int size=0;
	int op=0;
	data_type item;
	printf("请输入您要创建的栈的大小\n");
	scanf("%d",&size);
	//创建队
	pQueue1=createQueue(size);
	pQueue2=createQueue(size);
	while(1)
	{
		printf("请输入选项:------1:入栈/2:出栈\n");
		scanf("%d",&op);
		if(op == -1)break;
		switch(op)
		{
			case 1:
				//入栈
				printf("请输入你要插入的元素\n");
				scanf("%d",&item);
				inQueue(pQueue1,item);
				break;
			case 2:
				//出栈
				outQueueStack(pQueue1,pQueue2);
				
				break;
		}

	}
	return 0;

}
//出栈

int outQueueStack(Queue * pQueue1,Queue * pQueue2)
{	
	
	data_type q_item,p_item;
	while(1)
	{
		//出1
		outQueue(pQueue1,&q_item);
		//入2
		inQueue(pQueue2,q_item);
		//1空结束
		if(((pQueue1->front)==(pQueue1->rear))) break;
	}
	//出2
	outQueue(pQueue2,&p_item);
	printf("出栈======%d\n",p_item);
	
	while(1)
	{
		//2空结束
		if((pQueue2->front)==(pQueue2->rear)) break;
		//出2
		outQueue(pQueue2,&p_item);
		//入1
		inQueue(pQueue1,p_item);
		
	}
	
	return OK;
}

!!!为了防止队列的指针溢出,本篇采用循环队列解决队列指针溢出,
因为删除队列是更改队头指针,队列中的数据并未删除,所以显示队列会显示队列中的所有元素,而不会只显示有效元素

!!!!求个只显示有效元素的方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值