1099 [填空题]链表的合并
下面程序创建两个链表,然后将第二个链表合并到第一个链表未尾,但合并部分的代码未完成,请你完成这部分代码。
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;//建立三个结构体指针变量指向空
int i;
for(i=1; i<=n; i++)
{
p1=(struct student *)malloc(LEN);//开辟结构体指针类型的空间
scanf("%ld",&p1->num);//为节点成员赋值
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;//如果只建立一个链表 头指针指向p1
else p2->next=p1;
p2=p1;
}
return(head);
}
struct student *merge(struct student *head, struct student *head2)
{
struct student *p1;
p1=head;
while (p1->next!=NULL)
p1=p1->next;
p1->next=head2;
return (head);
/* struct student *p1;//定义结构体指针变量p1
p1=head;//p1指向head
while(p1->next!=NULL)p1=p1->next;//使p1指向head链表的最后一个元素
p1->next=head2;//p1的next指向head2头指针
return(head);*/
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
main()
{
struct student *head, *head2;
int n;
long del_num;
scanf("%d",&n);
head=create(n);
print(head);
scanf("%d",&n);
head2=create(n);
print(head2);
head = merge(head, head2);
print(head);
}
//标程
if(head==NULL) return head2;
struct student *p=head;
while(p->next!=NULL) p=p->next;
p->next = head2;
return head;
1098 [填空]链表结点的插入
完成插入链表结点的函数(按学号顺序),并调试通过、提交。
//题目
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head, struct student *stud)
{
--------------------------------------
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)//如果head是空表 则令head指向p0 ,p0的next指向null
{
head=p0;
p0->next=NULL;
}
else //否则当需要插入的号码大于原有表中的号码且原有表p1的next不为空时,
//让p2指向p1,p1指向下一个
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
//说明p0在p1前面,如果前面没有其他元素了,head->p0->p1;
//如果前面还有其他元素head->...->p2->p0->p1
{
if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;
}
else//如果p0在p1后面 p1->p0->null
{
p1->next=p0;
p0->next=NULL;
}
}
return(head);
--------------------------------
}
main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
stu=(struct student *)malloc(LEN);
scanf("%ld",&stu->num);
scanf("%d",&stu->score);
stu->next = NULL;
head=insert(head,stu);
print(head);
}
//标程
struct student *p0,*p1,*p2;
p1=head; p0=stud;
if(head==NULL)
{head=p0;}
else
{ while( (p0->num > p1->num) && (p1->next!=NULL) )
{ p2=p1; p1=p1->next;}
if( p0->num <= p1->num )
{ if( head==p1 ) head=p0;
else p2->next=p0;
p0->next=p1; }
else { p1->next=p0;}
}
return(head);
1104 [填空题]链表的倒序
下面程序,先创建一个链表,然后调用reverse函数,将链表中各结点变为倒序排列。请完成reverse函数,
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *reverse(struct student *head)
{
--------------------------------------------------
struct student *p1 = head,*p2 = NULL,*p3 = NULL;//如果不初始化为NULL可能会不行,可能!
while (p1!= NULL) {//迭代算法。把链表中的每个数往后挪,最后一个数挪到第一个,遍历链表,
//最后达到倒序的效果。
p2 = p1->next;
p1->next = p3;
p3 = p1;
p1 = p2;
}
return p3;
--------------------------------------------------
}
main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
head=reverse(head);
print(head);
}
在链表逆序算法_石锅拌饭的博客-CSDN博客,找到了两种方法,迭代算法、递归算法
1101 [填空题]链表的排序
下面程序,先创建一个链表(链表中各结点未按学号由小到大排序),然后调用sort函数,将链表中各结点按学号由小到大排序。
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head, struct student *stud)
{ struct student *p0,*p1,*p2;
p1=head; p0=stud;
if(head==NULL)
{head=p0;}
else
{ while( (p0->num > p1->num) && (p1->next!=NULL) )
{ p2=p1; p1=p1->next;}
if( p0->num <= p1->num )
{ if( head==p1 ) head=p0;
else p2->next=p0;
p0->next=p1; }
else { p1->next=p0;}
}
return(head);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
p1=head;
while(p1!=NULL)
{
if(p1->num == num)
{
if(p1 == head) head=p1->next;
else p2->next=p1->next;
free(p1);
break;
}
p2=p1;
p1=p1->next;
}
return(head);
}
struct student *sort(struct student *head)
{
-------------------------------------------------------
// 我的错啦 struct student *p1=head,*p2=NULL,*p3=NULL;
// while(p1!=NULL)
// {
// if(p1->next<p1)
// {
// p2=p1;
// p1=p1->next;
// p1->next=p2;
// }
// }
// return(head);
//保持冒泡,将满足判断语句(后比前小)结构体里面的成员替换就好了。比如1-5-4-6,当p指向5时,
//发现下一个的num更小,那么我就把下一个结点的num和我当前的num换一下,结点还是原来的结点,
//无需变动,只需改变成员变量就行了。
struct student *p1,*p2;
for(p1=head;p1!=NULL;p1=p1->next)
{
for(p2=p1->next;p2!=NULL;p2=p2->next)
{
if(p2->num<p1->num)
{
int t;
t = p1->num;
p1->num=p2->num;
p2->num=t;
t=p1->score;
p1->score=p2->score;
p2->score=t;
}
}
}
return head;
//标程
struct student *head2=NULL;
if(head==NULL) return NULL;
struct student *p=head, *p2;
while(p!=NULL)
{ //应该是将链表中的结点依次,按顺序大小插入到链表中的位置
p2=p->next;
p->next=NULL;
head2=insert(head2,p);
p=p2;
}
return head2;
-------------------------------------------------
}
main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
head=sort(head);
print(head);
}