本文是关于链表的一些操作(包括单链表和双向循环链表)
1、单链表,双链表的创建。
2、单链表和双链表的打印。
3、单链表的插入,删除。
4、双链表的插入和删除。
5、单链表的逆置。
6、单链表节点的个数。
7、单链表,双链表的查找。
函数代码:
//链表相关问题
typedef int DataType;
typedef struct LinkNode //单链表结构
{
struct LinkNode* next;
DataType data;
}LinkNode;
LinkNode *CreateNode(DataType x) //创建单链表节点
{
LinkNode *tmp = (LinkNode *)malloc(sizeof(LinkNode));
if (NULL == tmp)
{
printf("分配内存失败!\n");
return NULL;
}
tmp->next=NULL;
tmp->data=x;
return tmp;
}
void PrintLinkList(LinkNode *phead) //单链表打印
{
while (phead)
{
printf("%d ",phead->data);
phead = phead->next;
}
printf("\n");
}
void InsertLinkList(LinkNode **phead,LinkNode *pos,DataType x) //单链表插入
{
LinkNode *newNode,*tmp = *phead;
assert(phead);
if (NULL==*phead)
{
*phead = CreateNode(x);
return;
}
while(*phead != pos)
{
tmp = *phead;
*phead = (*phead)->next;
}
newNode = CreateNode(x);
newNode->next = tmp->next;
tmp->next = newNode;
}
size_t ListNodeCount(LinkNode* phead) //计算单链表的节点数
{
size_t count = 0;
while (phead)
{
count++;
phead = phead->next;
}
return count;
}
LinkNode *LinkListSearch(LinkNode *phead,DataType x) //在单链表中查找一个数
{
while(phead)
{
if (phead->data == x)
return phead;
phead = phead->next;
}
return NULL;
}
LinkNode *ReverseLinkList(LinkNode *phead) //单链表的逆置
{
LinkNode *first = phead;
LinkNode *cur = first->next;
first->next=NULL;
while (cur)
{
LinkNode *tmp = cur->next;
cur->next = first;
first = cur;
cur = tmp;
}
return first;
}
size_t RemoveLinkList(LinkNode **phead,LinkNode *pos) //单链表任意节点删除
{
LinkNode *first = *phead;
while (first)
{
if (*phead == pos) //删头节点
{
*phead = first->next;
free(pos);
pos = NULL;
return 1;
}
else if (first->next == pos) //非头节点情况
{
first->next = pos->next;
free(pos);
pos = NULL;
return 1;
}
first = first->next;
}
return 0;
}
typedef struct DoubleLinkList //双链表结构
{
DataType data;
struct DoubleLinkList *prev;
struct DoubleLinkList *next;
}DoubleList;
DoubleList *CreateDoubleList(DataType x) //创建双链表节点
{
DoubleList *newNode = (DoubleList *)malloc(sizeof(DoubleList));
assert(newNode);
newNode->next = NULL;
newNode->prev = NULL;
newNode->data = x;
return newNode;
}
void PrintDoubleList(DoubleList *phead) //打印双链表
{
DoubleList *tmp = phead;
while (tmp)
{
printf("%d ",tmp->data);
tmp = tmp->next;
if (tmp == phead)
break;
}
printf("\n");
}
DoubleList *DoubleListSearch(DoubleList *phead,DataType x) //双链表查找
{
DoubleList *tmp = phead;
while (phead)
{
if (phead->data == x)
return phead;
if (tmp == phead->next)
break;
phead = phead->next;
}
return NULL;
}
void DoubleListInsert(DoubleList **phead, DataType x) //双链表的头插
{
DoubleList *tmp = (*phead);
DoubleList *newNode = CreateDoubleList(x);
if (NULL == *phead)
{
*phead = newNode;
(*phead)->next = *phead;
(*phead)->prev = *phead;
}
else
{
newNode->next = (*phead)->next;
(*phead)->next = newNode;
newNode->prev = *phead;
newNode->next->prev = newNode;
}
}
size_t RemoveDoubleListNode(DoubleList **phead,DataType x) //删除双链表节点
{
DoubleList *tmp = *phead;
while (*phead)
{
if (tmp->data == x)
{
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
if (tmp->data == (*phead)->data)
*phead = tmp->next;
if ((*phead)->next == *phead)
{
free(*phead);
*phead = NULL;
}
free(tmp);
tmp = NULL;
return 1;
}
if (*phead == tmp->next)
break;
tmp = tmp->next;
}
return 0;
}