运用双链表实现字符串的拆分,去重,检测回文

  • Post author:
  • Post category:其他


输入一串字符,可以是字母,数字与符号,空格也包含在内,以问号结束(结束标志可以自行设定),实现将字符串中的字母,数字和其他

字符分开,并输出字母串的去重后的结果,最后一行为数字串的回文判断,是为1,不是回文为0.

以下是源代码,在VS2013中测试可用:

#include<iostream>

#include<string>

using namespace std;

class list

{

char Data;

public:

void Input_Data(char s)

{

Data = s;

}

char Output_Data()

{

return Data;

}

list *next;

list *prior;

};
void build(list * &List, list * &s)

{

list *p;

p = List;

if (List == NULL)

{

List = s;

return;

}

while (p)

{

if (p->next == NULL)

{

p->next = s;

s->prior = p;

return;

}

p = p->next;

}

cout << “Wrong !” << endl;

}
void check(list * &List)

{

list *p;

list *q;

p = List;

while (p)

{

q = p->next;

while (q)

{

if (q->Output_Data() == p->Output_Data())

{

list *k;

k = q;

q = q->prior;

if (k->next == NULL)

{

k->prior->next = NULL;

}

else

{

k->prior->next = k->next;

k->next->prior = k->prior;

}

delete k;

k = NULL;

}

q = q->next;

}

p = p->next;

}

}
void classify(list * &List_A,list * &List_B,list * &List_C)

{

list *p;

p = List_A;

while (p)

{

if ((p->Output_Data() >= 65 && p->Output_Data() <= 90) || (p->Output_Data() >= 97 && p->Output_Data() <= 122))

{

list *q = NULL;

q = p;

if (q == List_A)

{

List_A = q->next;

p = q->next;

q->next->prior = NULL;

q->next = NULL;

q->prior = NULL;

}

else if (q->next == NULL)

{

q->prior->next = NULL;

p = q->prior;

q->next = NULL;

q->prior = NULL;

}

else

{

q->prior->next = q->next;

q->next->prior = q->prior;

p = q->prior;

q->next = NULL;

q->prior = NULL;

}

build(List_B,q);

if (p == List_A)

continue;

}

else if (p->Output_Data() >= 48 && p->Output_Data() <= 57)

{

}

else

{

list *q = NULL;

q = p;

if (q == List_A)

{

List_A = q->next;

p = q->next;

q->next->prior = NULL;

q->next = NULL;

q->prior = NULL;

}

else if (q->next == NULL)

{

q->prior->next = NULL;

p = q->prior;

q->next = NULL;

q->prior = NULL;

}

else

{

q->prior->next = q->next;

q->next->prior = q->prior;

p = q->prior;

q->next = NULL;

q->prior = NULL;

}

build(List_C, q);

if (p == List_A)

continue;

}

p = p->next;

}

return;

}

bool Check_repeat(list * &List)

{

list *p;

p = List;

list *q;

while (p->next != NULL)

{

p = p->next;

}

q = p;

p = List;

while (p && q)

{

if ( p->Output_Data() != q->Output_Data() )

{

return 0;

}

p = p->next;

q = q->prior;

}

return 1;

}
int main()

{

list *List_A = NULL;

char input;

while (cin.get(input))

{

if (input == ‘?’)

{

break;

}

list *s;

s = new list();

s->next = NULL;

s->prior = NULL;

s->Input_Data(input);

build(List_A,s);

}

list *List_B = NULL;

list *List_C = NULL;

classify(List_A,List_B,List_C);

list *p;

p = List_A;

if (List_A == NULL)

{

cout << “-1”;

}

else

while (p)

{

cout << p->Output_Data();

p = p->next;

}

cout << endl;

p = List_B;

if (List_B == NULL)

{

cout << “-1”;

}

else

while (p)

{

cout << p->Output_Data();

p = p->next;

}

cout << endl;

p = List_C;

if (List_C == NULL)

{

cout << “-1”;

}

else

while (p)

{

cout << p->Output_Data();

p = p->next;

}

cout << endl;

check(List_B);

p = List_B;

if (List_B == NULL)

{

cout << “-1”;

}

else

while (p)

{

cout << p->Output_Data();

p = p->next;

}

cout << endl;

if (List_A != NULL)

{

cout << Check_repeat(List_A) << endl;

}

else cout << “-1” << endl;

}
输出第一行为数字串,第二行为字母串(去重前),第三行为其他字符,第四行为去重后的字母串,最后一行为数字串是否是回文的判断



版权声明:本文为u010093693原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。