#include <iostream>
using namespace std;
struct LinkNode {
int data;
LinkNode* next;
};
void InitLinkList_L(LinkNode*& L) {
L = new LinkNode;
L->next = L;
}
int IsEmpty(LinkNode* L) {
if (L->next == L)
return 1;
else
return 0;
}
void Print(LinkNode* L) {
LinkNode* p;
p = L->next;
while (p != L) {
cout << p->data;
p = p->next;
if (p != L)
cout << ' ';
}
cout << endl;
}
int Push_Head(LinkNode*& L, int x) {
LinkNode* p = new LinkNode;
if (p == 0) return 0;
p->data = x;
p->next = L->next;
L->next = p;
return 1;
}
int Push_Back(LinkNode*& L, int x) {
LinkNode* p = new LinkNode, * q;
if (p == 0) return 0;
p->data = x;
p->next = L;
q = L->next;
while (q->next != L) {
q = q->next;
}
q->next = p;
return 1;
}
int Length(LinkNode* L) {
LinkNode* p;
int count = 0;
p = L->next;
while (p != L) {
p = p->next;
count++;
}
return count;
}
LinkNode* Find(LinkNode* L, int i) {
if (i<0 || i>Length(L)) return 0;
LinkNode* p = L;
while (i--) {
p = p->next;
}
return p;
}
void Insert(LinkNode*& L, int i, int x) {
if (i<0 || i>Length(L) + 1) return;
if (i == Length(L) + 1) {
Push_Back(L, x);
}
else {
LinkNode* p = Find(L, i - 1); //用找到的位置代替頭指針的位置,所以要找到i前面那個元素的位置
Push_Head(p, x);
}
}
void Pop_LinkHead(LinkNode*& L) { //刪除頭結(jié)點(diǎn)
LinkNode* p;
p = L->next;
L = p;
delete p;
}
void Pop_Head(LinkNode*& L) {
LinkNode* p = L->next;
L->next = p->next;
delete p;
}
void Pop_Back(LinkNode*& L) {
LinkNode* p = L->next, * q = p->next;
while (q->next != L) {
p = q;
q = q->next;
}
p->next = L;
delete q;
}
void Delete(LinkNode* L,int i) {
if (i<0 || i>Length(L)) return;
LinkNode* p = Find(L, i - 1);
Pop_Head(p);
}
void Union_Link(LinkNode*& LA, LinkNode*& LB) { //合并LA,LB存于LA中
LinkNode* TA = LA;
while (TA->next!=LA) {
TA = TA->next;
}
LinkNode* TB = LB;
while (TB->next!=LB) {
TB = TB->next;
}
TA->next = LB->next;
TB->next = LA;
delete LB;
}
int main() {
LinkNode* LA, * LB;
InitLinkList_L(LA);
InitLinkList_L(LB);
Push_Back(LA, 1);
Push_Back(LA, 3);
Push_Back(LA, 5);
Push_Back(LA, 7);
Push_Back(LA, 9);
Push_Back(LB, 2);
Push_Back(LB, 4);
Push_Back(LB, 6);
Push_Back(LB, 8);
Push_Back(LB, 10);
Print(LA);
Print(LB);
Union_Link(LA, LB);
Print(LA);
}
/*
int main() {
LinkNode* L;
InitLinkList_L(L);
Push_Head(L, 1);
Push_Head(L, 2);
Push_Head(L, 3);
Push_Head(L, 4);
Push_Head(L, 5);
Push_Back(L, 11);
Push_Back(L, 12);
Push_Back(L, 13);
Push_Back(L, 14);
Push_Back(L, 15);
Insert(L, 11, 100);
Print(L);
Delete(L, 2);
Print(L);
Delete(L, 10);
Print(L);
Pop_Back(L);
Print(L);
Pop_Head(L);
Print(L);
Insert(L, 5, 14);
Print(L);
Insert(L, 3, 12);
Print(L);
cout << Length(L) << endl;
cout << Find(L, 8) << endl << Find(L, 8)->data;
return 0;
}
*/