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;
}LNode,*LinkList;
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
LinkList p,s;
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
LinkList p,q;
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
I first had a misleading point about this topic merging chains, but after trying it out, I found that there is an expression here that is equivalent.
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; }LNode,*LinkList; int CreateLink_L(LinkList &L,int n){ // Create a single-chain table with n elements LinkList p,q; int i; ElemType e; L = (LinkList)malloc(sizeof(LNode)); L->next = NULL; // Start by building a single-chain list with leading nodes q = (LinkList)malloc(sizeof(LNode)); 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; } int LoadLink_L(LinkList &L){ // Single Chain Table Traversal LinkList p = L->next; 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; } int LinkInsert_L(LinkList &L,int i,ElemType e){ // Algorithms 2.9 // Insert element e before position i I in single-chain linear table L with leading nodes // Please complete the code LinkList p=L; int j=0; while(p&&j<i-1) { p=p->next; j++; } if(!p||j>i-1) return ERROR; LinkList s; s=(LinkList)malloc(sizeof(LNode)); s->data=e; s->next=p->next; p->next=s; return OK; } int LinkDelete_L(LinkList &L,int i, ElemType &e){ // Algorithms 2.10 // In the single-chain linear table L with the leading node, delete the ith element and return its value with e // Please complete the code LinkList p=L; int j=0; while(p->next&&j<i-1) { p=p->next; ++j; } if(!(p->next)||j>i-1) return ERROR; LinkList q=p->next; p->next=q->next; e=q->data; free(q); return OK; } void MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc) { LinkList pa,pb,pc; 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() { LinkList T1,T2,T; int n1,n2; scanf("%d",&n1); CreateLink_L(T1,n1); printf("List A:"); LoadLink_L(T1); scanf("%d",&n2); CreateLink_L(T2,n2); printf("List B:"); LoadLink_L(T2); MergeList_L(T1,T2,T); printf("List C:"); LoadLink_L(T); return 0; }