单链表里有三种类型元素,将它们按类型分为三个循环链表 这个算法哪里出错

C语言 码拜 7年前 (2017-04-22) 934次浏览
#include<iostream>
#include<stdlib.h>
#include<malloc.h>
typedef char ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode , *LinkList , *CiList ;
void CreateList( LinkList &L ,int n )
{
LinkList p , q ;
L = ( LinkList ) malloc ( sizeof ( LNode) );
L ->next = NULL ;
q = L ;
for ( int i=0 ; i < n ; i++)
{
p = ( LinkList ) malloc ( sizeof ( LNode) );
scanf( ” %c”, &p->data );
p->next = q->next ;
q->next = p ;
q = p ;
}
}
void DipList (  LinkList L )
{
LinkList p = L ;
while ( p->next != NULL )
{
p = p->next ;
printf ( “%c”, p->data);
printf(” “);
}
}
void Separate_List ( LinkList L , CiList &La ,CiList &Lb ,CiList &Lc )
{
LNode *p,*q,*r,*s ;
La = ( CiList ) malloc ( sizeof( LNode )); p = La ;
Lb = ( CiList ) malloc ( sizeof( LNode )); q = Lb ;
Lc = ( CiList ) malloc ( sizeof( LNode )); r = Lc ;
s = L->next ;
while ( s!= NULL )
{
if ( s->data >= “0” && s->data <= “9”)
{
p->next = s ;
p = s ;
s = s->next ;
}
if( s->data >= “a” && s->data <= “z”)
{
q->next = s ;
q = s ;
s = s->next ;
}
else
{
r->next = s ;
r = s ;
s = s->next ;
}
}
p->next = La ;
q->next = Lb ;
r->next = Lc ;
}
void main()
{
LinkList L;
CiList La , Lb ,Lc ;
int n ;
printf( “请输入单链表长度:\n”);
scanf(“%d”,&n);
printf(“请输入单链表元素:\n”);
CreateList( L , n );
printf(“输入的单链表为:\n”);
DipList ( L ) ;
Separate_List (  L , La ,Lb ,Lc );
printf(“分离后的循环链表La为:\n”);
DipList ( La ) ;
printf(“分离后的循环链表Lb为:\n”);
DipList ( Lb ) ;
printf(“分离后的循环链表Lc为:\n”);
DipList ( Lc ) ;
}
到分离这个函数的时候就会出错 但是本人本人检查本人的算法始终不知道错在哪 求指点 十分感谢!
解决方案

80

if( s->data >= “a” && s->data <= “z”)
这句改成;
else if( s->data >= “a” && s->data <= “z”)

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明单链表里有三种类型元素,将它们按类型分为三个循环链表 这个算法哪里出错
喜欢 (0)
[1034331897@qq.com]
分享 (0)