谁来帮我看看这道有关数据结构的题啊

2025-03-06 20:23:17
推荐回答(1个)
回答1:

void main()
{ int a[10]={12,14,15,11,19,30,20,9,4,44};/*生成数组*/
int i,e;
struct node *l,*s,*p;
l=(struct node*)malloc(sizeof(struct node));
if(!l) printf("Error!\n");/*初始化空链表*/

e=a[0];
s=(struct node*)malloc(sizeof(struct node));
s->data=e;
s->next=NULL;
l->next=s;
for(i=1;i<10;i++)
{
e=a[i];
p=l;
while(p->next&&e>p->next->data)p=p->next;
s=(struct node*)malloc(sizeof(struct node));
s->data=e;
s->next=p->next;
p->next=s;
}/*依次将数组读入,生成一个升序的单链表*/

printf("please insert a int number(-32768~32767):");
scanf("%d",&e);

p=l;
while(p->next&&e>p->next->data)p=p->next;
s=(struct node*)malloc(sizeof(struct node));
s->data=e;
s->next=p->next;
p->next=s;

/*从键盘输入一个数据*/

p=l->next;
printf("The linklist is(from small to big):\n");
while(p)
{
printf("%4d",p->data);
p=p->next;
}
printf("\n");
/*打印单链表*/

}
累死了,写这么个程序容易嘛 呵呵 呦 一不小心还写多 没调用你提供的函数就解决问题了 哈哈 有空再给你写调用函数的吧 呵呵

我晕这也太简单了 没用5分钟就写好了 中间还吃了个苹果 呵呵
没有挑战性 还是自己写的有意思

void main()
{ int a[10]={12,14,15,11,19,30,20,9,4,44};/*生成数组*/
int i,e;
struct node *l,*s,*p;
l=(struct node*)malloc(sizeof(struct node));
if(!l) printf("Error!\n");
l->next=NULL;/*初始化空链表*/

while(i<10)
{
e=a[i];
insert(l,e);
}/*依次将数组读入,生成一个升序的单链表*/

printf("please insert a int number(-32768~32767):");
scanf("%d",&e); /*从键盘输入一个数据*/

insert(l,e);/*插入到链表的适当位置,并保持链表的有序性*/
output(l);/*打印单链表*/
}