| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 508 人关注过本帖
标题:关于字符串排序
只看楼主 加入收藏
lukezax
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-12-25
收藏
 问题点数:0 回复次数:3 
关于字符串排序

就是说两个单链表,合并之后排序,其中单链表的长度不一,内为单词,合并之后怎样实现按首字母排序

有如下程序,本鸟只做到这.不知道怎么改了,望高手指教.谢谢.
只实现了数字排序.....

#include <stdio.h>
#include <malloc.h>

struct LNode
{ int data;
struct LNode *next;
};

struct LNode *insert(struct LNode *head,int x,int i);
void display(struct LNode *head);
struct LNode *combine(struct LNode *head1,struct LNode *head2);

main()
{
struct LNode *head1,*head2,*head3;

head1 = NULL;
head1 = insert(head1,2,1);
head1 = insert(head1,12,2);
head1 = insert(head1,67,3);
printf("123123123123\n");
display(head1);
printf("\n");
head2 = NULL;
head2 = insert(head2,8,1);
head2 = insert(head2,20,2);
head2 = insert(head2,33,3);
head2 = insert(head2,35,4);
printf("123123123123\n");
display(head2);
printf("\n");

head3 = combine(head1,head2);
printf("123123123123\n");
display(head3);
getchar();
}

struct LNode *insert(struct LNode *head,int x,int i)
{
int j=1;
struct LNode *s,*q;
q=head;
s=(struct LNode *) malloc ( sizeof(struct LNode) );
s->data=x;

if(i==1)
{
s->next=q;
head=s;
}
else
{
while(q->next != NULL)
{
q=q->next;
j++;
}
if(j==i-1)
{
s->next=q->next;
q->next=s;
}
else
printf("error! there is no position\n");
}
return(head);
}

void display(struct LNode *head)
{
struct LNode *q;
q=head;
if(q==NULL)
printf("this is a NULL\n");
else
if(q->next==NULL)
printf("%d",q->data);
else
{
while(q->next!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
printf("%d",q->data);
}
}
struct LNode *combine(struct LNode *head1,struct LNode *head2)
{
int n;
struct LNode *p1,*p2,*p3,*head3;
head3=NULL;
p1=head1;
p2=head2;
p3=head3;

if(p1 == NULL && p2 == NULL)
printf("null!!!\n");
else
if(p1 != NULL && p2 == NULL)
return(head1);
else
if(p2 != NULL && p1 == NULL)
return(head2);
else
{ n=1;
while(p1 != NULL && p2 != NULL)
{
if(p1->data < p2->data)
{
head3=insert(head3,p1->data,n++);
p1=p1->next;
}
else
if(p1->data > p2->data)
{
head3=insert(head3,p2->data,n++);
p2=p2->next;
}
else
{
head3=insert(head3,p1->data,n++);
p1=p1->next;
p2=p2->next;
}
}
while(p1 != NULL && p2 == NULL)
{
head3=insert(head3,p1->data,n++);
p1=p1->next;
}
while(p2 != NULL && p1 == NULL)
{
head3=insert(head3,p2->data,n++);
p2=p1->next;
}
}
return(head3);
}

搜索更多相关主题的帖子: 字符 
2006-06-06 22:53
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
收藏
得分:0 
其实用字母一样的,只要你的链表元素是指向字符串的指针,使用strcmp来做比较就可以拉
例如p1->data < p2->data 就可以改成 if(strcmp( p1->data, p2->data))

c++/C + 汇编 = 天下无敌
2006-06-07 13:12
lukezax
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-12-25
收藏
得分:0 

那是不是要定义char型变量,还是什么?主要是我要输出单词(就是很多字母的~~),在

struct LNode
{ int data;
struct LNode *next;
};

struct LNode *insert(struct LNode *head,int x,int i);
void display(struct LNode *head);
struct LNode *combine(struct LNode *head1,struct LNode *head2);

中如何定义呢?

2006-06-07 14:59
SunShining
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:31
帖 子:2215
专家分:0
注 册:2006-2-17
收藏
得分:0 
char *data;

然后用 strcmp()比较就可以了!




[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
2006-06-08 08:48
快速回复:关于字符串排序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011791 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved