diff --git a/c-cpp/06_linkedlist/list_isPalindrome/LinkList.cpp b/c-cpp/06_linkedlist/list_isPalindrome/LinkList.cpp new file mode 100644 index 0000000..876162c --- /dev/null +++ b/c-cpp/06_linkedlist/list_isPalindrome/LinkList.cpp @@ -0,0 +1,148 @@ +#include "LinkList.h" + +void CreateListHead(LinkList *&L,ElemType a[],int n) +{ + int i; + LinkList *s; + L = (LinkList *)malloc(sizeof(LinkList)); + L->next = NULL; + for(i = 0;i < n;i++) + { + s=(LinkList*)malloc(sizeof(LinkList)); + s->data = a[i]; + s->next = L->next; + L->next = s; + } +} +void CreateListTail(LinkList *&L,ElemType a[],int n) +{ + int i; + LinkList * s,* r; + L = (LinkList *)malloc(sizeof(LinkList)); + r = L; + for(i = 0;i < n;i++) + { + s = (LinkList *)malloc(sizeof(LinkList)); + s->data = a[i]; + r->next = s; + r = s; + } + r->next = NULL; +} +void InitList(LinkList *&L) +{ + L=(LinkList *)malloc(sizeof(LinkList)); + L->next = NULL; +} +void DestroyList(LinkList *&L) +{ + LinkList * pre = L,*p = L->next; + while(p!=NULL) + { + free(pre); + pre = p; + p = L->next; + } + free(pre); +} +bool ListEmpty(LinkList *L) +{ + return(L->next==NULL); +} +int ListLength(LinkList *L) +{ + int n = 0; + LinkList * p = L; + while(p->next!=NULL) + { + n++; + p=p->next; + } + return(n); +} +void ShowList(LinkList *L) +{ + LinkList * p = L->next;//指向开始节点 + while(p!=NULL) + { + printf(" %c ",p->data); + p = p->next; + } + printf("\n"); +} +bool GetListElem(LinkList *L,int i,ElemType &e) +{ + int j = 0; + LinkList *p = L; + while(jnext; + } + if(p==NULL) + return false; + else + { + e=p->data; + return true; + } +} +int LocateElem(LinkList*L,ElemType e) +{ + int i=1; + LinkList *p = L->next; + while(p!=NULL&&p->data!=e){ + p=p->next; + i++; + } + if(p==NULL) + { + return(0); + } + else + return(i); +} +bool ListInsert(LinkList *&L,int i,ElemType e) +{ + int j=0; + LinkList *p =L,*s; + while(jnext; + } + if(p==NULL) + { + return false; + } + else + { + s= (LinkList*)malloc(sizeof(LinkList)); + s->data = e; + s->next = p->next; + p->next = s; + return true; + } +} +bool ListDelete(LinkList *&L,int i,ElemType &e) +{ + int j=0; + LinkList * p =L,*q; + while(jnext; + } + if(p==NULL) + return false; + else + { + q=p->next; + if(q==NULL) + return false; + e=q->data; + p->next=q->next; + free(q); + return true; + } +} diff --git a/c-cpp/06_linkedlist/list_isPalindrome/LinkList.h b/c-cpp/06_linkedlist/list_isPalindrome/LinkList.h new file mode 100644 index 0000000..190b7a9 --- /dev/null +++ b/c-cpp/06_linkedlist/list_isPalindrome/LinkList.h @@ -0,0 +1,24 @@ +#ifndef LINKLIST_H +#define LINKLIST_H + +#include +#include +typedef char ElemType; +typedef struct LNode +{ + ElemType data; + struct LNode*next; +}LinkList; + +void CreateListHead(LinkList *&L,ElemType a[],int n); +void CreateListTail(LinkList *&L,ElemType a[],int n); +void InitList(LinkList *&L); +void DestroyList(LinkList *&L); +bool ListEmpty(LinkList *L); +int ListLength(LinkList *L); +void ShowList(LinkList *L); +bool GetListElem(LinkList *L,int i,ElemType &e); +int LocateElem(LinkList*L,ElemType e); +bool ListInsert(LinkList *&L,int i,ElemType e); +bool ListDelete(LinkList *&L,int i,ElemType &e); +#endif diff --git a/c-cpp/06_linkedlist/list_isPalindrome/main.cpp b/c-cpp/06_linkedlist/list_isPalindrome/main.cpp new file mode 100644 index 0000000..1c79274 --- /dev/null +++ b/c-cpp/06_linkedlist/list_isPalindrome/main.cpp @@ -0,0 +1,40 @@ +#include"LinkList.h" +char array1[]= {'a','b','c','b','a'}; +bool isPalindrome(LinkList * list,int length); +int main() +{ + LinkList * list; + + int length = sizeof(array1)/sizeof(array1[0]); + InitList(list); + CreateListTail(list,array1,length);//用尾插法创建一个单链表 + if(isPalindrome(list,length)) + printf("isPalindrome\n"); + else + printf("isNotPalindrome\n"); + return 0; +} +bool isPalindrome(LinkList * list,int length) +{ + int i; + char buff1[length],buff2[length]; + ElemType e; + for(i=1; i<=length; i++) + { + GetListElem(list,i,e);//遍历获取链表元素并放入数组中 + buff1[i-1]=e;//正向数组 + buff2[length-i]=e;//反向数组 + } + i=0; + while(i<=length) { + if(buff1[i]==buff2[i])//比较 + { + i++; + } + else + { + return false; + } + } + return true; +}