| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 210 人关注过本帖
标题:[求助]我有一个链表,数据是从小到大的整型数。然后我插入另外一个整数,使 这个链
收藏  订阅  推荐  打印 
pinglideyu
Rank: 4
来自:武汉工程大学
等级:高级会员
威望:1
帖子:679
积分:6916
注册:2007-1-7
[求助]我有一个链表,数据是从小到大的整型数。然后我插入另外一个整数,使 这个链

# include<stdio.h>
# include<malloc.h>
struct list
{
int data;
struct list *next;
};
struct list *creat();
void print(struct list *head);
void insert(struct list *head,struct list *pnew);
void main()
{
struct list *head,*p1;
int m;

head=creat();
printf("\n");
printf("Enter the number of you want:\n");
scanf("%d",&m);
p1=(struct list*)malloc(sizeof(struct list));
if (p1==NULL)
{
printf("NO MEMORY!\n");
return ;
}
p1->data=m;
p1->next=NULL;
insert(head,p1);
printf("\n");
return;
}
struct list *creat()
{
struct list *head,*p,*rear;
int x;
head=(struct list*)malloc(sizeof(struct list));
rear=head;
scanf("%d",&x);
puts("input the list end with '0':\n");
while(x)
{
p=(struct list*)malloc(sizeof(struct list));
p->data=x;
rear->next=p;
rear=p;
scanf("%d",&x);
}
rear->next=NULL;
puts("the list you input is: ");
print(head->next);
return head->next;
}

void print(struct list *head)
{
struct list *p;
p=head;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
void insert(struct list *head,struct list *pnew)
{
struct list *p;
p=head;
for (p=head->next;p->next!=NULL;p=p->next)
if ((pnew->data)>(p->data))
{
pnew->next=p->next;
p->next=pnew;
}
print(head);
printf("\n");
}

假设我输入1 2 3 4 5 6
首先是打印这些数1 2 3 4 5 6
我要输入一个8
然后打印插入这个数之后的数1 2 3 4 5 6 8
可我的程序好像执行不出来。请问是那里的问题。

搜索更多相关主题的帖子: 链表  整型  整数  从小到大  数据  
2007-3-29 22:06
pinglideyu
Rank: 4
来自:武汉工程大学
等级:高级会员
威望:1
帖子:679
积分:6916
注册:2007-1-7

没人会吗?


~~我的明天我知道~~
2007-3-29 22:26
nuciewth
Rank: 12Rank: 12Rank: 12
来自:我爱龙龙
等级:版主
威望:93
帖子:9521
积分:95068
注册:2006-5-23

return head-&gt;next;//估计是这里的原因,你建立的是带头结点的,但返回的是它下一个结点,这样如果要修改head-&gt;next; 那你很可能就会丢失这个链表.

倚天照海花无数,流水高山心自知。
2007-3-29 23:10
nuciewth
Rank: 12Rank: 12Rank: 12
来自:我爱龙龙
等级:版主
威望:93
帖子:9521
积分:95068
注册:2006-5-23

void Insert_Node(node *head,int x)//插入一结点到升序链表中.
{
node *pre,*s,*p=head->next;
while(p&&p->info<x)
{
pre=p;
p=p->next;
}
s=(node *)malloc(sizeof(node));
s->info=x;
s->next=NULL;
pre->next=s;
s->next=p;
}

倚天照海花无数,流水高山心自知。
2007-3-29 23:19
nuciewth
Rank: 12Rank: 12Rank: 12
来自:我爱龙龙
等级:版主
威望:93
帖子:9521
积分:95068
注册:2006-5-23

/*****************测试程序***********************/
#include"Head_Node.h"
void Insert_Node(node *head,int x)
{
node *pre,*s,*p=head->next;
while(p&&p->info<x)
{
pre=p;
p=p->next;
}
s=(node *)malloc(sizeof(node));
s->info=x;
s->next=NULL;
pre->next=s;
s->next=p;
}

int main()
{
node *head;
int x;
head=Creat_Node();
Print_Node(head);
printf("输入x的值:");
scanf("%d",&x);
Insert_Node(head,x);
Print_Node(head);
return 0;
}
/*************************Head_Node.h************************/
#include<stdio.h>
#include<malloc.h>

typedef struct List_Node{
int info;
struct List_Node *next;
}node;//结点结构体
/******************************/
/* 尾插法建立带头结点的单链表 */
/******************************/
node* Creat_Node()
{
node *head,*pre,*p;
int x;
head=(node*)malloc(sizeof(node));;
head->next=NULL;
pre=head;
printf("输入各结点的值,以0结束:");
while(EOF!=(scanf("%d",&x))&&x!=0)
{
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=pre->next;
pre->next=p;
pre=pre->next;
}
return head;
}

/******************************/
/* 头插法建立带头结点的单链表 */
/******************************/
node* Build_Node()
{
node *head,*p;
int x;
head=(node*)malloc(sizeof(node));;
head->next=NULL;
printf("输入各结点的值,以0结束:");
while(EOF!=(scanf("%d",&x))&&x!=0)
{
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=head->next;
head->next=p;
}
return head;
}


/******************************/
/* 打印单链表 */
/******************************/

void Print_Node(node *head)
{
node *p=head->next;
printf("输出该链表:");
while(p)
{
printf("%-5d--->",p->info);
p=p->next;
}
if(p==NULL)
{
printf("^\n\n\n");
}
}


倚天照海花无数,流水高山心自知。
2007-3-29 23:21
mp3aaa
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:16
帖子:1966
积分:20194
注册:2006-2-15

# include<stdio.h>
# include<malloc.h>
struct list
{
int data;
struct list *next;
};
struct list *creat();
void print(struct list *head);
void insert(struct list *head,struct list *pnew);
void main()
{
struct list *head,*p1;
int m;

head=creat();
printf("\n");
printf("Enter the number of you want:\n");
scanf("%d",&m);
p1=(struct list*)malloc(sizeof(struct list));
if (p1==NULL)
{
printf("NO MEMORY!\n");
return ;
}
p1->data=m;
//p1->next=NULL;
insert(head,p1);
printf("\n");
return;
}
struct list *creat()
{
struct list *head,*p,*rear;
int x;
head=(struct list*)malloc(sizeof(struct list));
rear=head;
scanf("%d",&x);
puts("input the list end with '0':\n");
while(x)
{
p=(struct list*)malloc(sizeof(struct list));
p->data=x;
rear->next=p;
rear=p;
scanf("%d",&x);
}
rear->next=NULL;
puts("the list you input is: ");
print(head->next);
return head->next;
}

void print(struct list *head)
{
struct list *p;
p=head;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
void insert(struct list *head,struct list *pnew)
{
struct list *p;
p=head;
for (p=head;p!=NULL;p=p->next)
if (p->next==NULL||p->next->data>pnew->data)
{
pnew->next=p->next;
p->next=pnew;
break;
}
print(head);
printf("\n");
}

以后你要注意了 头结点最好不要放数据


羊肉串 葡萄干 哈密瓜!!
2007-3-29 23:31
mp3aaa
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:16
帖子:1966
积分:20194
注册:2006-2-15

nuciewth斑竹速度快啊
pinglideyU:因为你的头结点放了数据所以在输入2 3 4 5 0 插入1的时候 会出现2 1 3 4 5 0因为头结点是2 所以不好插入。
其他的都正常了 。

[此贴子已经被作者于2007-3-29 23:36:06编辑过]


羊肉串 葡萄干 哈密瓜!!
2007-3-29 23:35
pinglideyu
Rank: 4
来自:武汉工程大学
等级:高级会员
威望:1
帖子:679
积分:6916
注册:2007-1-7

哦 谢谢各位了

~~我的明天我知道~~
2007-3-30 07:46
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.052479 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved