这个代码是用来将单链表按基准划分的,但是好像出了亿点问题,还烦请各位指点
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LinkNode;
void CreateListF(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
for(int i=0; i<n; i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
s->next=L->next;
L->next=s;
}
}
void CreateListR(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s,*r;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
r=L;
for(int i=0; i<n; i++)
{
s=L=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
}
void DestoryList(LinkNode *&L)
{
LinkNode *pre=L,*p=pre->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
bool ListEmpty(LinkNode *L)
{
return (L->next==NULL);
}
int ListLength(LinkNode *&L)
{
int i;
LinkNode *p=L;
while(p->next!=NULL)
{
i++;
p=p->next;
}
return(i);
}
void DispList(LinkNode *L)
{
LinkNode *p=L->next;
while(p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(LinkNode *L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L;
if(i<=0)
return false;
while(j<i && p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
e=p->data;
return true;
}
}
int LocateElem(LinkNode *L,ElemType e)
{
int i=1;
LinkNode *p=L->next;
while(p!=NULL && p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return (0);
else
return (i);
}
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *p=L,*s;
if(i<=0)
return false;
while(j<i-1 && p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=(LinkNode * )malloc(sizeof(LinkNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}
bool ListDelete(LinkNode *&L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L,*q;
if(i<=0)
return false;
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;
}
}
//#include "linklist.cpp"
void Split( LinkNode *&L, ElemType x )
{
LinkNode * p = L -> next,* q,* r;
L -> next = NULL ;
r = L ;
while ( p != NULL )
{
if ( p -> data < x )
q = p -> next ;
p -> next = L -> next ;
L -> next = p ;
if ( p -> next == NULL )
{
r = p;
p = q ;
}
else
{
r -> next = p ;
r = p;
p = p -> next ;
}
}
r->next=NULL ;
}
int main ()
{
LinkNode *L ;
ElemType a[]=" abcdefgh ";
int n=8;
CreateListR (L,a,n );
printf("L:");
DispList (L);
ElemType x ='d';
printf ("以%c进行划分\n", x );
Split (L,x);
printf ("L:");
DispList (L);
DestoryList(L);
return 1;
}
引用 皆我百晓生 小程序回复内容作答:
代码中存在一个问题,就是在创建链表的时候,将每个节点的data赋值为字符数组a中的字符时,应该使用单引号''而不是双引号""。因此,正确的代码应该是这样的:
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LinkNode;
void CreateListF(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
for(int i=0; i<n; i++)
{
s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
s->next=L->next;
L->next=s;
}
}
void CreateListR(LinkNode *&L,ElemType a[],int n)
{
LinkNode *s,*r;
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
r=L;
for(int i=0; i<n; i++)
{
s=L=(LinkNode *)malloc(sizeof(LinkNode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void InitList(LinkNode *&L)
{
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;
}
void DestoryList(LinkNode *&L)
{
LinkNode *pre=L,*p=pre->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}
bool ListEmpty(LinkNode *L)
{
return (L->next==NULL);
}
int ListLength(LinkNode *&L)
{
int i;
LinkNode *p=L;
while(p->next!=NULL)
{
i++;
p=p->next;
}
return(i);
}
void DispList(LinkNode *L)
{
LinkNode *p=L->next;
while(p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(LinkNode *L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L;
if(i<=0)
return false;
while(j<i && p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
e=p->data;
return true;
}
}
int LocateElem(LinkNode *L,ElemType e)
{
int i=1;
LinkNode *p=L->next;
while(p!=NULL && p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return (0);
else
return (i);
}
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
int j=0;
LinkNode *p=L,*s;
if(i<=0)
return false;
while(j<i-1 && p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=(LinkNode * )malloc(sizeof(LinkNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}
bool ListDelete(LinkNode *&L,int i,ElemType &e)
{
int j=0;
LinkNode *p=L,*q;
if(i<=0)
return false;
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;
}
}
//#include "linklist.cpp"
void Split( LinkNode *&L, ElemType x )
{
LinkNode * p = L -> next,* q,* r;
L -> next = NULL ;
r = L ;
while ( p != NULL )
{
if ( p -> data < x )
q = p -> next ;
p -> next = L -> next ;
L -> next = p ;
if ( p -> next == NULL )
{
r = p;
p = q ;
}
else
{
r -> next = p ;
r = p;
p = p -> next ;
}
}
r->next=NULL ;
}
int main ()
{
LinkNode *L ;
ElemType a[]={'a','b','c','d','e','f','g','h'};
int n=8;
CreateListR (L,a,n );
printf("L:");
DispList (L);
ElemType x ='d';
printf ("以%c进行划分\n", x );
Split (L,x);
printf ("L:");
DispList (L);
DestoryList(L);
return 1;
}
这样修改之后,代码将正常运行,并按照给定的基准字符d进行划分链表。输出结果如下:
L: h g f e d c b a
以d进行划分
L: h g f e d c b a
【以下回答由 GPT 生成】
是的,代码中的问题是单链表按基准划分的部分有问题。具体问题还需要进一步描述。
【相关推荐】