BUPT数据结构第二次大作业第二题

代码仅供参考,建议读者先自行思考

如下:

#include<stdio.h>
#include<stdlib.h>
typedef struct node {
	struct node* bottom;
	char data;
	int size;
}stack;
typedef struct q_node {
	char data;
	struct q_node* nextPtr;
}list;
typedef struct l_node {
	list* front;
	list* rear;
}queue;
stack* createstack()
{
	stack* a = (stack*)malloc(sizeof(stack));
	a->bottom = NULL;
	a->size = 0;
	return a;
}
stack* create_new_ele(char str)
{
	stack* new_ele = (stack*)malloc(sizeof(stack));
	new_ele->bottom = NULL;
	new_ele->data = str;
	return new_ele;
}
void push(stack* a, stack* b)
{
	b->bottom = a->bottom;
	a->bottom = b;
	a->size++;
}//栈的建立
queue* createqueue()
{
	list* new_ptr = (list*)malloc(sizeof(list));
	new_ptr->nextPtr = NULL;
	queue* new_q = (queue*)malloc(sizeof(queue));
	new_q->front = new_q->rear = new_ptr;
	return new_q;
}
list* create_new_data(char str)
{
	list* new_node = (list*)malloc(sizeof(list));
	new_node->data = str;
	return new_node;
}//队列的建立
int main()
{
	stack* my_stack = createstack();
	queue* my_queue = createqueue();
	char t;
	while ((t = getchar()) != '#')
	{
		push(my_stack, create_new_ele(t));
		my_queue->rear->nextPtr = create_new_data(t);
		my_queue->rear = my_queue->rear->nextPtr;
	}
	int flag = 1;
	while (my_stack->bottom != NULL)
	{
		if (my_stack->bottom->data != my_queue->front->nextPtr->data)
		{
			flag = 0;
			break;
		}
		else
		{
			my_stack = my_stack->bottom;
			my_queue->front = my_queue->front->nextPtr;
		}
	}
	if (flag == 1)	printf("Yes\n");
	else printf("No\n");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值