1、类型定义
typedef struct node
{
int ele;
struct node* next;
}S_NODE;
typedef struct
{
S_NODE* head;
S_NODE* tail;
int length;
}S_LIST;
2、初始化
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9796903c1b9e16905ba0e29286a8351a.jpeg)
void InitList(S_LIST* list)
{
list->head = NULL;
list->tail = list->head;
list->length = 0;
}
3、在尾部添加节点
(1)头节点为空时
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/31b31e833a034612cfda1be2f7eefb97.jpeg)
if(list->head == NULL)
{
list->head = list->tail = p;
p->next = p;
}
(2)头节点不为空时
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9de56ef2ae3ef62efcdf0bcbef360690.jpeg)
list->tail->next = p;
p->next = list->head;
list->tail = p;
(3)添加节点函数
int AddNode(S_LIST* list, int ele)
{
S_NODE* p = NULL;
if(list == NULL)
return -1;
p = (S_NODE*)malloc(sizeof(S_NODE));
if(p == NULL)
return -2;
p->ele = ele;
p->next = NULL;
if(list->head == NULL)
{
list->head = list->tail = p;
p->next = p;
}
else
{
list->tail->next = p;
p->next = list->head;
list->tail = p;
}
(list->length)++;
return 0;
}
4、插入节点
(1)在头节点插入
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6eef1aecfcfb03da248636ef5729f519.jpeg)
q = list->head;
p->next = q;
list->head = p;
list->tail->next = list->head;
(2)在索引处插入
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5b0ee24f090d3e4c95b2ce8ad460fed4.jpeg)
while(num < index - 1)
{
q = q->next;
num++;
}
p->next = q->next;
q->next = p;
(3)插入函数
int InsertNode(S_LIST* list, int index, int ele)
{
S_NODE* p = NULL, *q = NULL;
int num = 1;
if(list == NULL)
return -1;
if(index < 1 || index >= list->length)
return -2;
p = (S_NODE*)malloc(sizeof(S_NODE));
if(p == NULL)
return -2;
p->ele = ele;
p->next = NULL;
q = list->head;
if(index == 1)
{
p->next = q;
list->head = p;
list->tail->next = list->head;
}
else
{
while(num < index - 1)
{
q = q->next;
num++;
}
p->next = q->next;
q->next = p;
}
(list->length)++;
return 0;
}
5、删除节点
(1)删除头节点
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/594ccc6526def36ef290264ec4b3038f.jpeg)
p = list->head;
list->head = p->next;
list->tail->next = list->head;
(2)删除索引节点
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f2bd9b4116651847482070d4983568fb.jpeg)
p = q->next;
q->next = p->next;
(3)在尾节点删除
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/501a0a5535eeec8cb617fbc44afb68c1.jpeg)
p = q->next;
q->next = p->next;
list->tail = q;
6、合并链表
(1)方法一,合并为第3个链表
void CombineList(S_LIST la, S_LIST lb, S_LIST* lc)
{
S_NODE* p = NULL;
if(lc == NULL)
return;
p = la.head;
while(p->next != la.head)
{
AddNode(lc, p->ele);
p = p->next;
}
AddNode(lc, p->ele);
p = lb.head;
while(p->next != lb.head)
{
AddNode(lc, p->ele);
p = p->next;
}
AddNode(lc, p->ele);
}
(2)方法二,在原链表上合并
void CombineList2(S_LIST* la, S_LIST* lb, S_LIST* lc)
{
lc->head = la->head;
la->tail->next = lb->head;
lb->tail->next = lc->head;
lc->tail = lb->tail;
lc->length = la->length + lb->length;
}
7、打印
void PrintList(S_LIST list)
{
S_NODE* p = NULL;
if(list.length == 0 || list.head == NULL)
return;
p = list.head;
printf("List contains %d: ", list.length);
while(p->next != list.head)
{
printf("%d ", p->ele);
p = p->next;
}
printf("%d\n", p->ele);
}
8、测试
int main()
{
S_LIST list, lb, lc;
int i = 0;
InitList(&list);
InitList(&lb);
InitList(&lc);
for(i = 0; i < 10; i++)
{
AddNode(&list, i);
AddNode(&lb, i * 2);
}
PrintList(list);
InsertNode(&list, 1, 100);
PrintList(list);
InsertNode(&list, 2, 200);
PrintList(list);
InsertNode(&list, list.length, 500);
PrintList(list);
AddNode(&list, 600);
PrintList(list);
DeleteNode(&list, 1, NULL);
PrintList(list);
DeleteNode(&list, 2, NULL);
PrintList(list);
DeleteNode(&list, list.length, NULL);
PrintList(list);
printf("combine list\n");
PrintList(list);
PrintList(lb);
CombineList2(&list, &lb, &lc);
PrintList(lc);
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6e036714ac06e475aefa82c0b0b9a751.jpeg)