代码仅供参考,建议读者先自行思考
如下:
#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;
}