设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称...
生活随笔
收集整理的這篇文章主要介紹了
设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称...
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
版權(quán)聲明:本文為博主原創(chuàng)文章。未經(jīng)博主同意不得轉(zhuǎn)載。
代碼例如以下: #include<cstdio> #include<cstdlib> #include<cstring> #define LEN sizeof(struct node) #define MAX 147 struct node {char cc;struct node *next; }; int judge(struct node *head,int len) {struct node *top,*p1,*p2;top = NULL;p1 = head->next;for(int i = 0 ; i < len/2 ; i++){p2 = (struct node *)malloc(LEN);p2->cc = p1->cc;p2->next = top;top = p2;p1 = p1->next;}if(len%2 == 1)p1 = p1->next;p2 = top;for(i = 0 ; i < len/2 ; i++){if(p2->cc != p1->cc)break;top = p2->next;p1 = p1->next;p2 = top;}if(!top) return 1;else return 0; }int main() {int n=0;char str[MAX];struct node *head,*p;head = p = (struct node *)malloc(LEN);head->next = p->next = NULL;printf("親、請(qǐng)輸入一個(gè)字符串:\n");gets(str);int len = strlen(str);while(n < len && str[0] != '\n'){p = (struct node *)malloc(LEN);p->cc = str[n];p->next = head->next;head->next = p;n++;}int flag = judge(head,len);if(flag)printf("%s是一個(gè)回文!\n",str);elseprintf("%s不是一個(gè)回文!\n",str);return 0; }
vasttian https://blog.csdn.net/u012860063/article/details/28281631
轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/u012860063
問題:設(shè)單鏈表中存放n個(gè)字符,試設(shè)計(jì)一個(gè)算法,使用棧推斷該字符串是否中心對(duì)稱,如xyzzyx即為中心對(duì)稱字符串。
代碼例如以下: #include<cstdio> #include<cstdlib> #include<cstring> #define LEN sizeof(struct node) #define MAX 147 struct node {char cc;struct node *next; }; int judge(struct node *head,int len) {struct node *top,*p1,*p2;top = NULL;p1 = head->next;for(int i = 0 ; i < len/2 ; i++){p2 = (struct node *)malloc(LEN);p2->cc = p1->cc;p2->next = top;top = p2;p1 = p1->next;}if(len%2 == 1)p1 = p1->next;p2 = top;for(i = 0 ; i < len/2 ; i++){if(p2->cc != p1->cc)break;top = p2->next;p1 = p1->next;p2 = top;}if(!top) return 1;else return 0; }int main() {int n=0;char str[MAX];struct node *head,*p;head = p = (struct node *)malloc(LEN);head->next = p->next = NULL;printf("親、請(qǐng)輸入一個(gè)字符串:\n");gets(str);int len = strlen(str);while(n < len && str[0] != '\n'){p = (struct node *)malloc(LEN);p->cc = str[n];p->next = head->next;head->next = p;n++;}int flag = judge(head,len);if(flag)printf("%s是一個(gè)回文!\n",str);elseprintf("%s不是一個(gè)回文!\n",str);return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/xfgnongmin/p/10742333.html
總結(jié)
以上是生活随笔為你收集整理的设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 发现了《ACE程序员教程》中一个例程的问
- 下一篇: centos安装多个tomcat