1. 程式人生 > 其它 >SCAU程式設計線上實訓平臺_實驗_高階語言程式設計_實驗11_連結串列操作_堂前習題

SCAU程式設計線上實訓平臺_實驗_高階語言程式設計_實驗11_連結串列操作_堂前習題

技術標籤:程式設計學習SCAU

1099 [填空題]連結串列的合併

Description

下面程式建立兩個連結串列,然後將第二個連結串列合併到第一個連結串列未尾,但合併部分的程式碼未完成,請你完成這部分程式碼。

#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); } struct student *merge(struct student *head, struct student *head2) { _______________________ } 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); }

輸入樣例

2 (the 1st linked list, 2 students)
1 (code of no.1 studentof the 1st linked list)
98 (score of no.1 student of the 1st linked list)
7 (code of no.2 student of the 1st linked list)
99 (score of no.2 student of the 1st linked list)
1 (the 2nd linked list, 1 student)
5 (code of no.1 student of the 2nd linked list)
87 (score of no.1 student of the 2nd linked list)

輸出樣例

   1      98
   7      99
   5      87
   1      98
   7      99
   5      87

程式碼實現

struct student *merge(struct student *head, struct student *head2)
{
    struct student *p1=head;
    while(p1->next!=NULL)p1=p1->next;
    p1->next=head2;
    return head;
}