Merge pull request #4 from MrzhangF1ghter/MrzhangF1ghter

回文算法 C++实现
This commit is contained in:
wangzheng0822 2018-10-06 21:43:38 +08:00 committed by GitHub
commit 576940cf11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 212 additions and 0 deletions

View File

@ -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(j<i&&p!=NULL)
{
j++;
p=p->next;
}
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(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
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(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
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;
}
}

View File

@ -0,0 +1,24 @@
#ifndef LINKLIST_H
#define LINKLIST_H
#include <stdlib.h>
#include <stdio.h>
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

View File

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