Problem A: 实现链表(线性表)

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; 
} 

  

 
原文地址:https://www.cnblogs.com/mjn1/p/8893284.html