当前位置: 代码迷 >> C语言 >> [求助]我有一个链表,数据是从小到大的整型数。然后我插入另外一个整数,使 ...
  详细解决方案

[求助]我有一个链表,数据是从小到大的整型数。然后我插入另外一个整数,使 ...

热度:243   发布时间:2007-03-29 22:06:38.0
[求助]我有一个链表,数据是从小到大的整型数。然后我插入另外一个整数,使 这个链

# 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
可我的程序好像执行不出来。请问是那里的问题。

搜索更多相关的解决方案: 链表  整型  整数  从小到大  数据  

----------------解决方案--------------------------------------------------------

没人会吗?


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

/*****************测试程序***********************/
#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");
}
}


----------------解决方案--------------------------------------------------------

# 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");
}

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


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

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


----------------解决方案--------------------------------------------------------
哦 谢谢各位了
----------------解决方案--------------------------------------------------------