读书人

跪求大神指点下小弟我错哪了!

发布时间: 2013-04-21 21:18:07 作者: rapoo

跪求大神指点下我哪里错了!!!
输入一个字符串,将其中地字母 字符 链入一个线 性列表,将数字 字符链入 另一个线性链表,要求程 序能够 1 按字典 顺序 输出 字母线性 列表中的元素,2 按照 由小 到大 的顺 序 输出 数字 线性 列表 中的 元素

#include<iostream>
using namespace std;

typedef struct LNode//定义结点
{
char data;
struct LNode *next;
}


LNode,*Linklist;
LNode *r;
LNode *p,*q;


void insertNode(Linklist &L,char e )//插入结点
{
q->data=e;
q->next=L;
p->next=L;
while(p->next->next->data<'e' && p->next->next!=NULL)
q->next=p->next->next;
p->next=q;

}
void outNode(Linklist &L)//输出结点
{

r->next=L;
while(r!=NULL)
{
cout<<r->data;
r->next=r->next->next;
}
}



int main()
{


cout<<"请输入字符串,必须为数字或者字母:";

Linklist list1,list2;//定义两个带头结点的链表
list1=(Linklist) malloc (sizeof(LNode));
list2=(Linklist) malloc (sizeof(LNode));

list1->next=NULL;
list2->next=NULL;

while(cin)
{
char ch;
cin>>ch;
if('0'<=ch&&ch<='9')
insertNode(list1,ch);
else if('a'<=ch&&ch<='z'||'A'<=ch&&ch<='Z')
insertNode(list2,ch);
else
{
cout<<"you put wrong";
return 0;
}

}
outNode(list1);
cout<<'\n';
outNode(list2);
return 1;

}

[解决办法]

#include<iostream>
using namespace std;

typedef struct LNode//定义结点
{
char data;
struct LNode *next;


}*Linklist;
LNode *r;
LNode *p,*q;

void insertNode(Linklist &L,char e )//插入结点
{
q = (LNode*)malloc(sizeof(LNode)); //要为新节点申请空间
q->data = e;
q->next = NULL;
for(p=L; p->next && p->next->data < e; p=p->next);//一直找到一个比它大的或者找不到
if(p->next) q->next = p->next;//插在中间
p->next = q;
}
void outNode(Linklist &L)//输出结点
{
for(r=L->next; r; r=r->next) cout<<r->data;
}

int main()
{
cout<<"请输入字符串,必须为数字或者字母:";
Linklist list1,list2;//定义两个带头结点的链表
list1=(Linklist) malloc (sizeof(LNode));
list2=(Linklist) malloc (sizeof(LNode));

list1->next=NULL;
list2->next=NULL;

char ch;
while(cin>>ch)
{
if('0'<=ch&&ch<='9')
insertNode(list1,ch);
else if('a'<=ch&&ch<='z'
[解决办法]
'A'<=ch&&ch<='Z')
insertNode(list2,ch);
else
{
cout<<"you put wrong";
return 0;
}
}
outNode(list1);
cout<<endl;
outNode(list2);
cout<<endl;
return 0;
}


[解决办法]
#include<iostream>
using namespace std;

typedef struct LNode//定义结点
{
char data;
struct LNode *next;
}*Linklist;
LNode *r;
LNode *p,*q;

void insertNode(Linklist &L,char e )//插入结点
{
q = (LNode*)malloc(sizeof(LNode)); //要为新节点申请空间
q->data = e;
q->next = NULL;
for(p=L; p->next && p->next->data < e; p=p->next);//一直找到一个比它大的或者找不到
if(p->next) q->next = p->next;//插在中间
p->next = q;
}
void outNode(Linklist &L)//输出结点
{
for(r=L->next; r; r=r->next) cout<<r->data;
}

int main()
{
cout<<"请输入字符串,必须为数字或者字母:";
Linklist list1,list2;//定义两个带头结点的链表
list1=(Linklist) malloc (sizeof(LNode));
list2=(Linklist) malloc (sizeof(LNode));



list1->next=NULL;
list2->next=NULL;

char ch;
while ((ch=getchar())!='\n')
{

if('0'<=ch&&ch<='9')
insertNode(list1,ch);
else if('a'<=ch&&ch<='z'
[解决办法]
'A'<=ch&&ch<='Z')
insertNode(list2,ch);
else
{
cout<<"you put wrong";
return 0;
}
}
outNode(list1);
cout<<endl;
outNode(list2);
cout<<endl;
free(list1);
free(list2);
free(q);
list1 = NULL;
list2 = NULL;
q = NULL;
return 0;
}


基于楼上的修改了该一下

读书人网 >C++

热点推荐