☆ C/C++回文字符串的判定(使用链栈与队列)

本文介绍了一种使用链栈解决回文数问题的方法,通过编写C++代码实现链栈,并利用其特性判断输入数据是否为回文。文章提供了完整的源代码,包括链栈的初始化、入栈、出栈操作及回文判断过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上一篇文章中我提到了如何去写链栈与链队,

但是任何代码都是要有用处才可以吸引我们的兴趣,给我们继续前进的动力。

那么下面就来介绍如何使用链栈来解决回文数问题。

 

时空门--->>>链栈与链队(^_−)☆

 

****************************************************************************************************************************************

 

★直入主题,直接附上源代码:

     (已省略注释)

#include <iostream>
#include <windows.h>
#include <string.h>
#include <malloc.h>
using namespace std;

typedef struct node
{
	char data;
	struct node * link;
}LinkStack;

LinkStack * InitLinkStack()
{
	LinkStack * h;
	if (!(h = (LinkStack*)malloc(sizeof(LinkStack))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	h->data = '\0';
	h->link = NULL;
	return h;
}

void PushStack(LinkStack *h, char number)    //入栈
{
	LinkStack *p, *s;
	p = h;
	if (!(s = (LinkStack*)malloc(sizeof(LinkStack))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	s->data = number;
	s->link = NULL;
	p->link = s;
}

char PopStack(LinkStack *h)
{
	char temp_number = '\0';
	if (h->link == NULL)
	{
		cout << "LinkStack is Blank!" << endl;
		system("pause");
		cout << "Error:1" << endl;
	}
	LinkStack *p, *temp;
	p = h;
	if (!(temp = (LinkStack*)malloc(sizeof(LinkStack))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	while (p->link->link)
	{
		p = p->link;
	}
	temp = p->link;
	temp_number = p->link->data;
	p->link = p->link->link;
	free(temp);
//	cout << endl << "Pop " << temp_number << " Successful!";
	return temp_number;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int flag = 0;
	LinkStack *h, *p;
	h = InitLinkStack();
	p = h;
	char number;
	char temp;    //存储弹出的数据
	cout << "☆Push Stack: " << endl;
	int n = 0;
	cout << "How many people? " << endl;
	cin >> n;
	cout << "Please enter the data: " << endl;
	for (int i = 0; i<n; i++)
	{
		cin >> number;
		PushStack(p, number);
		p = p->link;
	}
	cout << endl;
	cout << "☆HuiWen_Judge: " << endl;
	p = h;
	for (int i = 0; i < n/2; i++)
	{
		temp = PopStack(h);
		if (temp != p->link->data)
		{
			flag = 1;
			cout << "Result:  Not OK!" << endl;
			return 1;
		}
		p = p->link;
	}
	if (flag == 0)
	{
		cout << "Result:  OK" << endl;
	}
	return 0;
}

(∑(っ°Д°;)っ啊嘞,被你发现了 ,好吧好吧,我招了~这就是我直接基于上篇文章中的链栈直接改的~)

 

 

 

 

****************************************************************************************************************************************

2018.09.29晚更新内容:

下面混合使用栈与队列来处理回文数问题:

思路:

由于栈与队列分别是从其所在的链上的两端输出,

所以根据这个性质可以创建一个栈和一个队列,使得二者使用的是同一个“链表”;

然后在主函数中对返回的值进行循环比较即可。

附上代码:

#include <iostream>
#include <string.h>
#include <windows.h>
using namespace std;

typedef struct node
{
	char data;
	struct node * link;
}LinkStack, LinkQueue;

LinkStack * InitLinkStack()
{
	LinkStack * h;
	if (!(h = (LinkStack*)malloc(sizeof(LinkStack))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	h->data = '\0';
	h->link = NULL;
	return h;
}

void PushStack(LinkStack *h, char number)    //入栈
{
	LinkStack *p, *s;
	p = h;
	if (!(s = (LinkStack*)malloc(sizeof(LinkStack))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	s->data = number;
	s->link = NULL;
	p->link = s;
}


char PopQueue(LinkQueue *h)                  //出队,使用返回值带出弹出的值
{
	char temp_number = '0';
	if (h->link == NULL)
	{
		cout << "LinkQueue is Blank!" << endl;
		system("pause");
		cout << "Error:1" << endl;
	}
	LinkQueue *p, *temp;
	p = h;
	if (!(temp = (LinkQueue*)malloc(sizeof(LinkQueue))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	temp = p->link;
	temp_number = p->link->data;
	p->link = p->link->link;
	free(temp);
	return temp_number;
}

char PopStack(LinkStack *h)                  //出栈,使用返回值带出弹出的值
{
	char temp_number = '\0';
	if (h->link == NULL)
	{
		cout << "LinkStack is Blank!" << endl;
		system("pause");
		cout << "Error:2" << endl;
	}
	LinkStack *p, *temp;
	p = h;
	if (!(temp = (LinkStack*)malloc(sizeof(LinkStack))))
	{
		cout << "Memory alllocate error!" << endl;
		exit(0);
	}
	while (p->link->link)
	{
		p = p->link;
	}
	temp = p->link;
	temp_number = p->link->data;
	p->link = p->link->link;
	free(temp);
	return temp_number;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int flag = 0;
	LinkStack *h, *p;
	h = InitLinkStack();
	p = h;
	char number;
	char temp_1;							//存储栈弹出的数据->尾元素
	char temp_2;							//存储队列弹出的数据->头元素
	cout << "☆Push Stack: " << endl;
	int n = 0;
	cout << "How many people? " << endl;
	cin >> n;
	cout << "Please enter the data: " << endl;
	for (int i = 0; i<n; i++)
	{
		cin >> number;
		PushStack(p, number);
		p = p->link;
	}
	cout << endl;
	cout << "☆HuiWen_Judge: " << endl;
	p = h;
	for (int i = 0; i < n / 2; i++)
	{
		temp_1 = PopStack(h);
		temp_2 = PopQueue(h);
//		cout << temp_1 << endl << temp_2 << endl;    //测试输出
		if (temp_1!= temp_2)
		{
			flag = 1;
			cout << "Result:  Not OK!" << endl;
			return 1;
		}
		p = p->link;
	}
	if (flag == 0)
	{
		cout << "Result:  OK" << endl;
	}
	return 0;
}

 

 

 

 

 

 

☆仅仅记录日常编写代码 与 疑问(`・ω・´)

 

****************************************************************************************************************************************

 

             最快的脚步不是跨越,而是继续,最慢的步伐不是小步,而是徘徊。

 

****************************************************************************************************************************************

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值