做360手机网站优化,桂林网上商城,wordpress更改域名,做网站应该学什么语言A和B是两个单链表#xff08;带表头结点#xff09;#xff0c;其中元素递增有序。设计一个算法#xff0c;将A和B归 并成一个按元素值非递减有序的链表 C#xff0c;C由A 和B 中的结点组成。
#includeiostream
#includestdlib.husing namespace std;typ…A和B是两个单链表带表头结点其中元素递增有序。设计一个算法将A和B归 并成一个按元素值非递减有序的链表 CC由A 和B 中的结点组成。
#includeiostream
#includestdlib.husing namespace std;typedef struct LNode
{int data;struct LNode * next;
}LNode;/**
尾插法建立单链表
*/
void createlistR(LNode *C,int a[],int n)
{LNode *s,*r;Cnew LNode;C-nextNULL;rC;for(int i0;in;i){snew LNode;s-dataa[i];r-nexts;rr-next;}r-nextNULL;
}/****************
头插法建立单链表
*****************/
createlistF(LNode *C,int a[],int n)
{LNode* s;Cnew LNode;C-nextNULL;for(int i0;in;i){snew LNode;s-dataa[i];s-nextC-next;C-nexts;}
}void merge1(LNode* A,LNode* B,LNode *C)
{LNode* pA-next;LNode* qB-next;LNode* r;//始终指向C的终端节点CA;C-nextNULL;rC;free(B);while(p!NULLq!NULL){if(p-dataq-data){r-nextp;pp-next;rr-next;}else{r-nextq;qq-next;rr-next;}}
// r-nextNULL;if(p!NULL)r-nextp;if(q!NULL)r-nextp;
}int main()
{int a[]{1,4,7,8,10,15,77,90};int b[]{1,2,3,4,5,6,9};LNode* head1new LNode;createlistR(head1,a,8);LNode* head2new LNode;createlistR(head2,b,7);LNode* phead1-next;while(p){coutp-data ;pp-next;}coutendl;phead2-next;while(p){coutp-data ;pp-next;}coutendl*************合并后***********endl;LNode* result;merge1(head1,head2,result);presult-next;while(p){coutp-data ;pp-next;}return 0;
}