创建循环双链表,并进行两个链表合并操作
L1={1,2,3,4,5,6},L2={11,22,33,44,55,66}当i=0时,将L2链接到L1前,i大于0小于6时,插入第i个位置,i大于6是,链接到L1后面
实现代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct DNode{
int data;
struct DNode *prior;
struct DNode *next;
}DLinkNode;
void CreateF(DLinkNode *&L,int a[],int n){
DLinkNode *s,*L1;
L=(DLinkNode*)malloc(sizeof(DLinkNode));
L->prior=L->next=L;
L1=L;
for(int i=0;i<n;i++){
s=(DLinkNode*)malloc(sizeof(DLinkNode));
s->data=a[i];
L1->next=s;
s->prior=L1;
s->next=L;
L->prior=s;
L1=s;
}
}
void Insert(DLinkNode *&ha,DLinkNode *&hb,int i){
DLinkNode *p=ha->next,*post;
int lena=1,j;
while(p->next!=ha){
lena++;
p=p->next;
}
if(i==0){
p=hb->prior;//由于ha头结点要摘下来,所以必须这么做
p->next=ha->next;//要画图才能容易地看出关系
ha->next->prior=p;
ha->next=hb->next;
hb->next->prior=ha;
}else if(i<lena){
j=1;