1. 程式人生 > >☆ C/C++迴文字串的判定(使用鏈棧與佇列)

☆ C/C++迴文字串的判定(使用鏈棧與佇列)

在上一篇文章中我提到了如何去寫鏈棧與鏈隊,

但是任何程式碼都是要有用處才可以吸引我們的興趣,給我們繼續前進的動力。

那麼下面就來介紹如何使用鏈棧來解決迴文數問題。

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

★直入主題,直接附上原始碼:

     (已省略註釋)

#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;
}

☆僅僅記錄日常編寫程式碼 與 疑問(`・ω・´)

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

             最快的腳步不是跨越,而是繼續,最慢的步伐不是小步,而是徘徊。

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