在上一篇文章中我提到了如何去写链栈与链队,
但是任何代码都是要有用处才可以吸引我们的兴趣,给我们继续前进的动力。
那么下面就来介绍如何使用链栈来解决回文数问题。
时空门--->>>链栈与链队(^_−)☆
****************************************************************************************************************************************
★直入主题,直接附上源代码:
(已省略注释)
#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;
}
☆仅仅记录日常编写代码 与 疑问(`・ω・´)
****************************************************************************************************************************************
最快的脚步不是跨越,而是继续,最慢的步伐不是小步,而是徘徊。
****************************************************************************************************************************************