题目识别读入的一个以@为结束符的字符序列是否型如“序列1&序列2”模式的字符序列。序列2是序列1的逆序列。如“123&321”。
分析算法中采用栈的形式存储字符。
#include<stdio.h>
#include <malloc.h>
typedef struct node
{
int data;
struct node *next;
}node;
struct node *top=NULL;
bool push(int val);
int pop();
bool IsImpty();
bool Scan();
int search();
int main()
{
search() ? printf("true") : printf("false");
return 0;
}
int search()
{
int e;
int c, i;
while ((c = getchar()) != '&')
{
push(c);
}
while (c=getchar ())
{
if (IsImpty() && c=='@')
{
return true ;
}
else if (IsImpty() && c != '@' || c == '@' && (IsImpty() != 1))
{
return false;
}
else
{
e = pop();
if (c != e)
return false;
}
}
}
bool push(int val)
{
node *line;
line = (node *)malloc (sizeof(node ));
line->data = val;
line->next = top;
top = line;
return true;
}
int pop()
{
node *line;
int e;
line = top;
e=line->data;
top = line->next;
return e;
}
bool IsImpty()
{
return top == NULL ? true : false;
}
bool Scan()
{
node *line;
line = top;
while (line != NULL)
{
printf ("%c", line->data);
line = line->next;
}
return true;
}
输入形式为“123&321@”
如果满足类型则输出“True”否则输出“False”,