概念
循环链表是一种头尾相连的链表,最后一个结点的指针域指向头结点,从而使整个链表形成环 从表中的任一个结点出发均可以找到表中其他结点,最后一个链表结点的指针域不为空,指向头结点
注意点
循环链表中没有NULL指针,所以在遍历的时候,其终止条件不再像非循环链表那样子去判断p或者p->next是否为空,而是判断其是否等于头指针
头插法创建循环链表
static bool initcircularListHead(circularList* circularListHead, int N)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularListHead->next = circularListHead;
for (i = 0; i < N; i++)
{
circularList* newNode = (circularList*)malloc(sizeof(circularList));
newNode->value = i;
newNode->next = circularListHead->next;
circularListHead->next = newNode;
}
return true;
}
尾插法创建循环链表
static bool initcircularListTail(circularList* circularListHead, int N)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularListHead->next = circularListHead;
circularList* circularListCurrent = circularListHead;
for (i = 0; i < N; i++)
{
circularList* newNode = (circularList*)malloc(sizeof(circularList));
newNode->value = i;
circularListCurrent->next = newNode;
circularListCurrent = newNode;
}
circularListCurrent->next = circularListHead;
return true;
}
遍历循环单链表
static bool ergodicCircularListTail(circularList* circularListHead)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
if (circularListHead->next == circularListHead)
{
printf("circularList->next is NULL,please init it\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while (circularListCurrent != circularListHead)
{
printf("circularListCurrent->value=%d\n", circularListCurrent->value);
circularListCurrent = circularListCurrent->next;
}
return true;
}
链表长度
static int getCircularListLength(circularList* circularListHead)
{
int length = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while (circularListCurrent != circularListHead)
{
circularListCurrent = circularListCurrent->next;
length++;
}
return length;
}
指定位置插入元素
static bool insertCircularList(circularList* circularListHead, int pos, int data)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while ((circularListCurrent != circularListHead) && (i < pos - 1))
{
circularListCurrent = circularListCurrent->next;
i++;
}
if (circularListCurrent == circularListHead)
{
printf("circularListCurrent is equal circularListHead\n");
return false;
}
circularList* newNode = (circularList*)malloc(sizeof(circularList));
newNode->value = data;
newNode->next = circularListCurrent->next;
circularListCurrent->next = newNode;
return true;
}
根据下标删除元素
static bool delCircularList(circularList* circularListHead, int pos)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while ((circularListCurrent != circularListHead) && (i < pos - 1))
{
circularListCurrent = circularListCurrent->next;
i++;
}
if (circularListCurrent == circularListHead)
{
printf("circularListCurrent is equal circularListHead\n");
return false;
}
circularList* toDelNode = circularListCurrent->next;
circularListCurrent->next = toDelNode->next;
free(toDelNode);
toDelNode = NULL;
return true;
}
根据元素值删除元素
static bool delCircularListAsElem(circularList* circularListHead, int value)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
circularList* toDelNodePre = circularListHead;
while ((circularListCurrent != circularListHead))
{
if (circularListCurrent->value != value)
{
toDelNodePre = circularListCurrent;
circularListCurrent = circularListCurrent->next;
}
else
{
circularList* toDelNode = circularListCurrent;
toDelNodePre->next = toDelNode->next;
free(toDelNode);
toDelNode = NULL;
break;
}
}
return true;
}
根据元素获取下标
static int getCircularListIndexByVal(circularList* circularListHead, int val)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while ((circularListCurrent != circularListHead))
{
i++;
if (circularListCurrent->value == val)
{
return i;
}
circularListCurrent = circularListCurrent->next;
}
return -1;
}
根据下标来获取元素值
static int getCircularListValByIndex(circularList* circularListHead, int pos, int* val)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
if (pos < 0 || pos > getCircularListLength(circularListHead) - 1)
{
printf("pos is invaild\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while ((circularListCurrent != circularListHead) && (i < pos - 1))
{
circularListCurrent = circularListCurrent->next;
i++;
}
if (circularListCurrent->next == circularListHead)
{
printf("circularListCurrent is equal circularListHead\n");
return false;
}
*val = circularListCurrent->next->value;
return true;
}
根据下标来修改元素的值
static bool setCircularListValByIndex(circularList* circularListHead, int pos, int val)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
if (pos > getCircularListLength(circularListHead) - 1)
{
printf("pos is invaild\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
for (i = 0; i < getCircularListLength(circularListHead); i++)
{
if (circularListCurrent == circularListHead)
{
break;
}
if (i == pos)
{
circularListCurrent->value = val;
return true;
}
circularListCurrent = circularListCurrent->next;
}
return false;
}
删除链表
static bool clearCircularList(circularList* circularListHead)
{
if (circularListHead == NULL)
{
printf("CircularList is NULL\n");
return false;
}
circularList* toDelNode = circularListHead->next;
circularList* toDelNodeNext;
while (toDelNode != circularListHead)
{
toDelNodeNext = toDelNode->next;
free(toDelNode);
toDelNode = toDelNodeNext;
}
circularListHead->next = circularListHead;
return true;
}
链表合并
static bool mergeCircularList(circularList* aHead, circularList* bHead)
{
circularList* aCurrent = aHead;
circularList* bCurrent = bHead;
while (aCurrent->next != aHead )
{
aCurrent = aCurrent->next;
}
while (bCurrent->next != bHead)
{
bCurrent = bCurrent->next;
}
aCurrent->next = bCurrent->next->next;
free(bCurrent->next);
bCurrent->next = aHead;
return true;
}
完整代码
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct circularList
{
int value;
struct circularList* next;
}circularList;
struct circularList* g_circularListA;
struct circularList* g_circularListB;
static bool initcircularListHead(circularList* circularListHead, int N)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularListHead->next = circularListHead;
for (i = 0; i < N; i++)
{
circularList* newNode = (circularList*)malloc(sizeof(circularList));
newNode->value = i;
newNode->next = circularListHead->next;
circularListHead->next = newNode;
}
return true;
}
static bool initcircularListTail(circularList* circularListHead, int N)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularListHead->next = circularListHead;
circularList* circularListCurrent = circularListHead;
for (i = 0; i < N; i++)
{
circularList* newNode = (circularList*)malloc(sizeof(circularList));
newNode->value = i;
circularListCurrent->next = newNode;
circularListCurrent = newNode;
}
circularListCurrent->next = circularListHead;
return true;
}
static bool ergodicCircularListTail(circularList* circularListHead)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
if (circularListHead->next == circularListHead)
{
printf("circularList->next is NULL,please init it\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while (circularListCurrent != circularListHead)
{
printf("circularListCurrent->value=%d\n", circularListCurrent->value);
circularListCurrent = circularListCurrent->next;
}
return true;
}
static int getCircularListLength(circularList* circularListHead)
{
int length = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while (circularListCurrent != circularListHead)
{
circularListCurrent = circularListCurrent->next;
length++;
}
return length;
}
static bool insertCircularList(circularList* circularListHead, int pos, int data)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while ((circularListCurrent != circularListHead) && (i < pos - 1))
{
circularListCurrent = circularListCurrent->next;
i++;
}
if (circularListCurrent == circularListHead)
{
printf("circularListCurrent is equal circularListHead\n");
return false;
}
circularList* newNode = (circularList*)malloc(sizeof(circularList));
newNode->value = data;
newNode->next = circularListCurrent->next;
circularListCurrent->next = newNode;
return true;
}
static bool delCircularList(circularList* circularListHead, int pos)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while ((circularListCurrent != circularListHead) && (i < pos - 1))
{
circularListCurrent = circularListCurrent->next;
i++;
}
if (circularListCurrent == circularListHead)
{
printf("circularListCurrent is equal circularListHead\n");
return false;
}
circularList* toDelNode = circularListCurrent->next;
circularListCurrent->next = toDelNode->next;
free(toDelNode);
toDelNode = NULL;
return true;
}
static bool delCircularListAsElem(circularList* circularListHead, int value)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
circularList* toDelNodePre = circularListHead;
while ((circularListCurrent != circularListHead))
{
if (circularListCurrent->value != value)
{
toDelNodePre = circularListCurrent;
circularListCurrent = circularListCurrent->next;
}
else
{
circularList* toDelNode = circularListCurrent;
toDelNodePre->next = toDelNode->next;
free(toDelNode);
toDelNode = NULL;
break;
}
}
return true;
}
static int getCircularListIndexByVal(circularList* circularListHead, int val)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while ((circularListCurrent != circularListHead))
{
i++;
if (circularListCurrent->value == val)
{
return i;
}
circularListCurrent = circularListCurrent->next;
}
return -1;
}
static int getCircularListValByIndex(circularList* circularListHead, int pos, int* val)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
if (pos < 0 || pos > getCircularListLength(circularListHead) - 1)
{
printf("pos is invaild\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
while ((circularListCurrent != circularListHead) && (i < pos - 1))
{
circularListCurrent = circularListCurrent->next;
i++;
}
if (circularListCurrent->next == circularListHead)
{
printf("circularListCurrent is equal circularListHead\n");
return false;
}
*val = circularListCurrent->next->value;
return true;
}
static bool setCircularListValByIndex(circularList* circularListHead, int pos, int val)
{
int i = 0;
if (circularListHead == NULL)
{
printf("circularList is NULL\n");
return false;
}
if (pos > getCircularListLength(circularListHead) - 1)
{
printf("pos is invaild\n");
return false;
}
circularList* circularListCurrent = circularListHead->next;
for (i = 0; i < getCircularListLength(circularListHead); i++)
{
if (circularListCurrent == circularListHead)
{
break;
}
if (i == pos)
{
circularListCurrent->value = val;
return true;
}
circularListCurrent = circularListCurrent->next;
}
return false;
}
static bool clearCircularList(circularList* circularListHead)
{
if (circularListHead == NULL)
{
printf("CircularList is NULL\n");
return false;
}
circularList* toDelNode = circularListHead->next;
circularList* toDelNodeNext;
while (toDelNode != circularListHead)
{
toDelNodeNext = toDelNode->next;
free(toDelNode);
toDelNode = toDelNodeNext;
}
circularListHead->next = circularListHead;
return true;
}
static bool mergeCircularList(circularList* aHead, circularList* bHead)
{
circularList* aCurrent = aHead;
circularList* bCurrent = bHead;
while (aCurrent->next != aHead )
{
aCurrent = aCurrent->next;
}
while (bCurrent->next != bHead)
{
bCurrent = bCurrent->next;
}
aCurrent->next = bCurrent->next->next;
free(bCurrent->next);
bCurrent->next = aHead;
return true;
}
int main()
{
bool ret;
int val;
struct circularList* p = (circularList*)malloc(sizeof(circularList));
struct circularList* p1 = (circularList*)malloc(sizeof(circularList));
ret = initcircularListHead(p, 4);
if (ret == false)
{
printf("initcircularListHead failed\n");
}
else
{
}
printf("+++++++++++++++++++++++++++\n");
ret = initcircularListHead(p1, 8);
if (ret == false)
{
printf("initcircularListHead failed\n");
}
else
{
}
mergeCircularList(p, p1);
ergodicCircularListTail(p);
return 0;
}
|