线性表中的
单链表
typedef struct LNode{
Elemtype data;
struct LNode *next;
}LNode,*Linklist;
双链表
typedef struct DLNode{
Elemtype data;
struct DLNode *next;
struct DLNode *prior;
}DLNode;
假设有n个元素已经存在数组a中,用尾插法建立链表C
尾插法
void merge(LNode *&C,int n,int a[])
{ LNode *p,*s;
C=(LNode*)malloc(sizeof(LNode));
C->next=NULL;
p=C;
for(int i=0;i<n;i++)
{ s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=r->next;}
r-next=NULL;
}
头插法
void merge(LNode *&C,int n,int a[])
{ LNode *s;
C=(LNode*)malloc(sizeof(LNode));
C->next=NULL;
for(int i=0;i<n;i++)
{ s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
s->next=C->next;
C->next=s;}
}
}
代码好难敲·········
A和B是两个单链表(带表头链表),其中元素递增有序,设计一个算法,将A和B归并成一个按元素值非递减有序的链表C,C由A和B的结点组成
手写去了