数据结构——顺序表的C语言代码实现
数据结构——单向不带头非循环链表的C语言代码实现
数据结构——单向不带头循环链表的C语言代码实现
数据结构——单向带头非循环链表的C语言代码实现
数据结构——单向带头循环链表的C语言代码实现
数据结构——双向带头循环链表的C语言代码实现
- 系列文章目录
- 前言
- 一、List.h
- 二、List.c
- 三、test.c
- 总结
前言
单向不带头循环链表的C语言代码。
一、List.h
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
typedef int SLLDataType;
typedef struct SLListNode
{
SLLDataType data;
struct SLListNode* next;
}SLL;
//创建节点
SLL* SLLCreateNode(SLLDataType n);
//尾插
void SLLPushBack(SLL** pphead, SLLDataType n);
//尾删
void SLLPopBack(SLL** pphead);
//头插
void SLLPushFront(SLL** pphead, SLLDataType n);
//头删
void SLLPopFront(SLL** pphead);
//查找
SLL* SLLFind(SLL* phead, SLLDataType n);
//在指定节点后面插入
void SLLInsertBack(SLL* phead, SLLDataType n, SLLDataType insert);
//在指定节点前面插入
void SLLInsertFront(SLL** pphead, SLLDataType n, SLLDataType insert);
//指定删除某节点
void SLLDelete(SLL** pphead, SLLDataType n);
//更改节点
void SLLChange(SLL* phead, SLLDataType n, SLLDataType change);
//打印链表
void SLLPrint(SLL* phead);
//删除链表
void FreeSLL(SLL* phead);
二、List.c
代码如下:
#include"List.h"
SLL* tail = NULL;
SLL* SLLCreateNode(SLLDataType n)
{
SLL* p = (SLL*)malloc(sizeof(SLL));
p->data = n;
p->next = NULL;
return p;
}
//尾插法
void SLLPushBack(SLL** pphead, SLLDataType n)
{
SLL* tem = SLLCreateNode(n);
if (*pphead == NULL)
{
*pphead = tem;
tail = tem;
tail->next = *pphead;
}
else
{
tail->next = tem;
tail = tem;
tail->next = *pphead;
}
}
//尾删法
void SLLPopBack(SLL** pphead)
{
if ((*pphead)->next == *pphead)
{
printf("链表为空\n");
return;
}
if ((*pphead)->next->next == *pphead)
{
free(tail);
(*pphead)->next = *pphead;
tail = *pphead;
}
else
{
SLL* tem = *pphead;
while (tem->next != tail)
{
tem = tem->next;
}
tem->next = *pphead;
free(tail);
tail = tem;
}
}
//头插法
void SLLPushFront(SLL * *pphead, SLLDataType n)
{
SLL* insert = SLLCreateNode(n);
insert->next = *pphead;
*pphead = insert;
tail->next = *pphead;
}
//头删法
void SLLPopFront(SLL** pphead)
{
if ((*pphead)->next == *pphead)
{
printf("链表为空\n");
return;
}
SLL* tem = (*pphead)->next;
free(*pphead);
*pphead = tem;
tail->next = *pphead;
}
//查找节点
SLL* SLLFind(SLL* phead, SLLDataType n)
{
if (phead->next == phead)
{
printf("链表为空\n");
exit(-1);
}
SLL* tem = phead;
if (tem->data == n)
{
return tem;
}
else
{
tem = phead->next;
while (tem != phead)
{
if (tem->data == n)
return tem;
tem = tem->next;
}
return NULL;
}
}
//在指定节点后面插入
void SLLInsertBack(SLL* phead, SLLDataType n, SLLDataType insert)
{
SLL* tem = SLLFind(phead, n);
if (tem == NULL)
{
printf("链表中没有%d\n",n);
return;
}
if (tem == tail)
{
SLLPushBack(&phead, insert);
}
else
{
SLL* Insert = SLLCreateNode(insert);
Insert->next = tem->next;
tem->next = Insert;
}
}
//在指定节点前面插入
void SLLInsertFront(SLL** pphead, SLLDataType n,SLLDataType x)
{
SLL* tem = SLLFind(*pphead, n);
if (tem == NULL)
{
printf("链表中没有%d\n", n);
return;
}
if (tem == *pphead)
{
SLLPushFront(pphead, x);
}
else
{
SLL* preinsert = *pphead;
while (preinsert->next != tem)
{
preinsert = preinsert->next;
}
SLL* Insert = SLLCreateNode(x);
Insert->next = tem;
preinsert->next = Insert;
}
}
//指定删除某节点
void SLLDeleteNode(SLL** pphead, SLLDataType n)
{
SLL* tem = SLLFind(*pphead, n);
if (tem == NULL)
{
printf("链表中没有%d\n", n);
return;
}
if (tem == *pphead)
{
SLLPopFront(pphead);
}
else if (tem == *pphead)
{
SLLPopBack(pphead);
}
else
{
SLL* pretem = *pphead;
while (pretem->next != tem)
{
pretem = pretem->next;
}
pretem->next = tem->next;
free(tem);
}
}
//更改节点
void SLLChange(SLL* phead, SLLDataType n, SLLDataType change)
{
SLL* tem = SLLFind(phead, n);
if (tem == NULL)
{
printf("链表中没有%d\n", n);
return;
}
tem->data = change;
}
//删除链表
void FreeSLL(SLL* phead)
{
SLL* tem = phead;
SLL* next = tem->next;
free(tem);
tem = phead->next;
while (tem != phead)
{
next = tem->next;
free(tem);
tem = next;
}
}
//打印链表
void SLLPrint(SLL* phead)
{
if (phead->next == phead)
{
printf("链表为空\n");
return 0;
}
printf("%d ", phead->data);
SLL* tem = phead->next;
while (tem != phead)
{
printf("%d ", tem->data);
tem = tem->next;
}
printf("\n");
}
三、test.c
代码如下:
#include"List.h"
int main()
{
SLL* sl = NULL;
SLLPushBack(&sl, 1);
SLLPushBack(&sl, 2);
SLLPushBack(&sl, 3);
SLLPrint(sl);
SLLPopBack(&sl);
SLLPrint(sl);
SLLPushFront(&sl, 4);
SLLPrint(sl);
SLLInsertFront(&sl, 4, 5);
SLLInsertFront(&sl, 4, 5);
SLLInsertFront(&sl, 4, 9);
SLLPrint(sl);
SLLInsertBack(sl, 2, 7);
SLLPrint(sl);
SLLPushBack(&sl, 3);
SLLPrint(sl);
SLLInsertBack(sl, 20, 7);
SLLDeleteNode(&sl, 5);
SLLPrint(sl);
SLLDeleteNode(&sl, 3);
SLLPrint(sl);
SLLDeleteNode(&sl, 1);
SLLPrint(sl);
SLLChange(sl, 5, 8);
SLLPrint(sl);
FreeSLL(sl);
return 0;
}
总结
该形式链表并不具有特殊价值,仅用于锻炼链表能力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)