1. 程式人生 > >Problem A: 實現鏈表(線性表)

Problem A: 實現鏈表(線性表)

試用 stdio.h nbsp HR input type 結點 AR return

Problem A: 實現鏈表(線性表)

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 443 Solved: 293
[Submit][Status][Web Board]

Description

(線性表)順序結構線性表LA與LB的結點關鍵字為整數。LA與LB的元素按非遞減有序,線性表空間足夠大。試用給出一種高效算法,將LB中元素合到LA中,使新的LA的元素仍保持非遞減有序。高效指最大限度的避免移動元素。

Input

輸入LA長度m:7

輸入數據:3 7 11 15 57 68 99

輸入LB長度n:7

輸入數據:6 7 8 9 10 23 67

Output

3 6 7 8 9 10 11 15 23 57 67 68 99

Sample Input

7
4 6 7 9 10 16 23
8
1 2 4 7 8 13 15 44

Sample Output

1 2 4 6 7 8 9 10 13 15 16 23 44 
#include<stdio.h> 
typedef struct student 
{ 
    int data; 
    struct NODE *next; 
}Node; 
Node *insert_node(Node *head,int b) 
{ 
    Node *pre1=head,*pre2,*p; 
    p=(Node *)malloc(sizeof(Node)); 
    p->data=b; 
    if(head==NULL) 
    { 
        head=p; 
        p->next=NULL; 
    } 
    else if(p->data<head->data) 
    { 
        head=p; 
        p->next=pre1; 
    } 
    else
    { 
        while((pre1!=NULL&&p->data>=pre1->data)) 
        { 
            pre2=pre1; 
            pre1=pre1->next; 
        } 
        p->next=pre2->next; 
        pre2->next=p; 
    } 
    return head; 
} 
void print(Node *head) 
{ 
    Node *p,*q; 
    p=head; 
    q=p->next; 
    while(q!=NULL) 
    { 
        if(p->data!=q->data) 
            printf("%d ",p->data); 
        p=p->next; 
        q=p->next; 
    } 
    printf("%d ",p->data); 
} 
int main() 
{ 
    int a[81],b[81]; 
    int m,n,i,j; 
    Node *head=NULL; 
    scanf("%d",&m); 
    for(i=0;i<m;i++) 
        scanf("%d",&a[i]); 
    scanf("%d",&n); 
    for(j=0;j<n;j++) 
        scanf("%d",&b[j]); 
    for(i=0;i<m;i++) 
        head=insert_node(head,a[i]); 
    for(j=0;j<n;j++) 
        head=insert_node(head,b[j]); 
    print(head); 
    return 0; 
} 

  

 

Problem A: 實現鏈表(線性表)