求教数据结构实验题目题目一:已知线性表LA和LB中的数据元素按值非递减有序排列,现要求将LA和LB归并为一个新的线性表L

1个回答

  • 如满意,望及时采纳,如有问题请追问

    #include

    #include

    #define M 5

    #define N 6

    typedef struct node

    {

    int data;

    struct node *next;

    }LinkNode, *LinkList;

    LinkList Creat(int *arr, int n)

    {

    int i;

    LinkList p = NULL;

    LinkList q = NULL;

    LinkList head = NULL;

    head = (LinkList) malloc (sizeof(LinkNode));

    head->next = NULL;

    q = head;

    for (i=0; inext = NULL;

    p->data = arr[i];

    q->next = p;

    q = p;

    }

    return head;

    }

    void Merger(LinkList LA, LinkList LB)

    {

    LinkList p1 = LA->next;

    LinkList p2 = LB->next;

    LinkList p3 = NULL;

    LinkList tmp = NULL;

    p3 = LA;

    free(LB);

    while ((NULL != p1) && (NULL != p2))

    {

    if (p1->data data)

    {

    p3 = p1;

    p1 = p1->next;

    }

    else

    {

    tmp = p2->next;

    p2->next = p1;

    p3->next = p2;

    p3 = p2;

    p2 = tmp;

    }

    }

    if (NULL == p1)

    {

    p3->next = p2;

    }

    else

    {

    p3->next = p1;

    }

    }

    void Output(LinkList head)

    {

    LinkList p = head->next;

    while (NULL != p)

    {

    printf("%d ", p->data);

    p = p->next;

    }

    printf("n");

    }

    int main(void)

    {

    int arrOne[M] = {3, 5, 8, 11, 24};

    int arrTwo[N] = {2, 6, 8, 9, 11, 20};

    LinkList LA = NULL;

    LinkList LB = NULL;

    LA = Creat(arrOne, M);

    LB = Creat(arrTwo, N);

    Merger(LA, LB);

    Output(LA);

    return 0;

    }