数据结构:使用链栈实现回文判断

题目:
回文判断
试写一个算法,判断依次读入的一个以@为结束符的字母序列,是否为形如‘序列1&序列2’模式的字符序列。其中序列1和序列2中都不含字符‘&’,且序列2是序列1的逆序列。例如,‘a+b&b+a’是属该模式的字符序列,而‘1+3&3-1’则不是。

主要思路:
“回文”一字符串正着读和反着读是相同的字符序列,如“abcba”,“abba"为"回文”,“abab”则不是“回文”。
通过栈先进后出的特点。即可判断是否为回文。
这里的题目是耿国华的数据结构教材题目。
【只提供子函数,需放入链栈的基本操作里运行】

void huiwen()//回文
{
	linkstack top;
	type temp, ch;

	top = (linkstack)malloc(sizeof(stack));
	if (top == NULL) return;
	InitStack(top);
	printf("\n请输入字符:");
	while ((ch = getchar()) != '&')
	{
		if (push(top, ch))
			printf("%c入栈成功\n", ch);
		else
			printf("%c入栈失败\n", ch);
	}
	do
	{
		ch = getchar();
		pop(top, &temp);
		//		printf("ch=%c,temp=%c\n", ch,temp);
		if (ch != temp)
		{
			printf("no");
			break;
		}
	} while (ch != '@' && !IsEmpty(top));
	if ((ch = getchar()) == '@' && IsEmpty(top))
		printf("yes");
	else
		printf("no");

}
  • 2
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断回文可以使用链栈或队列来实现。具体实现方法如下: 1.使用链栈实现回文判断 将字符串的前一半依次,然后依次并与字符串的后一半进行比较,如果全部相等,则为回文。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 100 typedef struct Node { char data; struct Node* next; } Node; typedef struct Stack { Node* top; } Stack; Stack* initStack() { Stack* stack = (Stack*)malloc(sizeof(Stack)); stack->top = NULL; return stack; } void push(Stack* stack, char data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->next = stack->top; stack->top = node; } char pop(Stack* stack) { if (stack->top == NULL) { return '\0'; } char data = stack->top->data; Node* temp = stack->top; stack->top = stack->top->next; free(temp); return data; } int isPalindrome(char* str) { int len = strlen(str); Stack* stack = initStack(); for (int i = 0; i < len / 2; i++) { push(stack, str[i]); } for (int i = len / 2 + len % 2; i < len; i++) { if (pop(stack) != str[i]) { return 0; } } return 1; } int main() { char str[MAXSIZE]; printf("请输入字符串:"); scanf("%s", str); if (isPalindrome(str)) { printf("是回文\n"); } else { printf("不是回文\n"); } return 0; } ``` 2.使用队列实现回文判断 将字符串的前一半依次入队,然后依次出队并与字符串的后一半进行比较,如果全部相等,则为回文。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 100 typedef struct Queue { char* data; int front; int rear; } Queue; Queue* initQueue() { Queue* queue = (Queue*)malloc(sizeof(Queue)); queue->data = (char*)malloc(sizeof(char) * MAXSIZE); queue->front = queue->rear = 0; return queue; } void enqueue(Queue* queue, char data) { queue->data[queue->rear] = data; queue->rear = (queue->rear + 1) % MAXSIZE; } char dequeue(Queue* queue) { if (queue->front == queue->rear) { return '\0'; } char data = queue->data[queue->front]; queue->front = (queue->front + 1) % MAXSIZE; return data; } int isPalindrome(char* str) { int len = strlen(str); Queue* queue = initQueue(); for (int i = 0; i < len / 2; i++) { enqueue(queue, str[i]); } for (int i = len / 2 + len % 2; i < len; i++) { if (dequeue(queue) != str[i]) { return 0; } } return 1; } int main() { char str[MAXSIZE]; printf("请输入字符串:"); scanf("%s", str); if (isPalindrome(str)) { printf("是回文\n"); } else { printf("不是回文\n"); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值