# SCAU--Data Structure Exercise--8580--Merge Chain Lists

Keywords: less

## Title Description

Description
The basic operations of a linear chain table are as follows:
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define ElemType int

typedef int Status;
typedef struct LNode
{
int data;
struct LNode *next;

Status ListInsert_L (LinkList &L, int i, ElemType e) {//algorithm 2.9
//Insert element e before the first element of a single-chain linear table L with a leading node
p = L;
int j = 0;
While (p && J < i-1) {//Find node I-1
p = p->next;
++j;
}
If (!P || J > i-1) return ERROR; // I is less than 1 or greater than table length
s = (LinkList)malloc(sizeof(LNode)); //Generate a new node
S->data = e; s->next = p->next; //Insert in L
p->next = s;
return OK;
} // LinstInsert_L

Status ListDelete_L (LinkList &L, int i, ElemType &e) {//algorithm 2.10
//In single-chain linear table L with leading nodes, delete the first element and return its value from e
p = L;
int j = 0;
While (p->next && J < i-1) {// find the ith node and point P to its direction
p = p->next;
++j;
}
If (! (p->next) || J > i-1) return ERROR; //delete location is not reasonable
q = p->next;
P->next = q->next;//Delete and release node
e = q->data;
free(q);
return OK;
} // ListDelete_L

Design an algorithm to merge two non-decreasing ordered chain tables A and B into a new non-decreasing ordered chain table C.

Input Format
First row: Number of elements in single-chain list A
Line 2: Elements of single-chain list A (non-decreasing), separated by spaces
Line 3: Number of elements in single-chain table B
Line 4: Elements of single-chain table B (non-decreasing), separated by spaces

Output Format
Line 1: List of elements in single-chain list A
Line 2: Element list of single-chain table B
Line 3: Merged list of elements in single-chain table C

sample input
6
12 24 45 62 84 96
4
15 31 75 86

sample output
List A:12 24 45 62 84 96
List B:15 31 75 86
List C:12 15 24 31 45 62 75 84 86 96

## To put it simply

```        if(pa->data>pb->data)
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
else
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
```

That is, pc=pb and pc=pa, both of which are equivalent to pc=pc->next;

The code is as follows:

```#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define ElemType int
typedef struct LNode
{
int data;
struct LNode *next;
// Create a single-chain table with n elements
int i;
ElemType e;
L->next = NULL;              // Start by building a single-chain list with leading nodes
q = L;
for (i=0; i<n; i++) {
scanf("%d", &e);
p = (LinkList)malloc(sizeof(LNode));  // Generate new nodes
p->data=e;
p->next=NULL;
q->next=p;
q=q->next;
}
return OK;
}
// Single Chain Table Traversal
if(p==NULL)printf("The List is empty!"); // Please fill in the blanks
else
{
while(p!=NULL)    // Please fill in the blanks
{
printf("%d ",p->data);
p=p->next;    // Please fill in the blanks
}
}
printf("\n");
return OK;
}

// Algorithms 2.9
// Insert element e before position i I in single-chain linear table L with leading nodes
int j=0;
while(p&&j<i-1)
{
p=p->next;
j++;
}
if(!p||j>i-1) return ERROR;
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}

// Algorithms 2.10
// In the single-chain linear table L with the leading node, delete the ith element and return its value with e
int j=0;
while(p->next&&j<i-1)
{
p=p->next;
++j;
}
if(!(p->next)||j>i-1) return ERROR;
p->next=q->next;
e=q->data;
free(q);
return OK;
}
{
pa=La->next;
pb=Lb->next;
pc=Lc=La;
while(pa&&pb)
{
if(pa->data>pb->data)
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
else
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
}
pc->next=pa?pa:pb;
free(Lb);
}
int main()
{
int n1,n2;
scanf("%d",&n1);
printf("List A:");