#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct Lnode{
ElemType data;
Lnode *next;
}Lnode,*LinkNode;
int InitLinkNode(LinkNode &L){
L = (LinkNode)malloc(sizeof(Lnode));
L -> next = L;
printf("Lnode init success \n");
return 0;
}
int GetAllElem(LinkNode L){
LinkNode p;
p = L;
while(p->next!=L){
p = p ->next;
printf("%d\n",p->data);
}
return 0;
}
int LinkNodeCreate(LinkNode &L, int i ){
LinkNode q,r;
r = L;
for (int j=0;j<i;j++){
q = (LinkNode)malloc(sizeof(Lnode));
q ->data = j;
r -> next = q;
r = q;
}
r -> next = L;
return 0;
}
LinkNode ConnectLinkNode(LinkNode A,LinkNode B){
LinkNode p,q;
p = A;
while(p->next!=A){
p = p->next;
}
q = B;
while(q->next!=B){
q = q->next;
}
p->next = B->next; //原本p->next = A 现在让 p的next域等于B的首元结点 A和B的元素就连接起来了
q->next = A; //B的尾结点指向B现在让他指向A完成合并
return A;
}
int main(){
LinkNode L,Q,K;
InitLinkNode(L);
InitLinkNode(Q);
LinkNodeCreate(L,10);
LinkNodeCreate(Q,6);
L = ConnectLinkNode(L,Q);
GetAllElem(L);
// GetAllElem(Q);
return 0;
}