数据结构——顺序表的C语言代码实现
数据结构——单向不带头非循环链表的C语言代码实现
数据结构——双向带头循环链表的C语言代码实现
数据结构——单向带头非循环链表的C语言代码实现
- 系列文章目录
- 前言
- 一、List.h
- 二、List.c
- 三、test.c
- 总结
前言
该篇文章是剩余六个链表之一的c语言代码实现,基于对常见的两个链表的学习(单向不带头非循环和双向带头循环),我们已基本掌握链表C语言实现的技能,故该篇只放代码!
一、List.h
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode* next;
struct ListNode* prev;
}LN;
//初始头结点
void ListInit(LN** phead);
//创建节点
LN* ListCreateNode(DataType x);
//尾插法
void ListPushBack(LN* phead, DataType x);
//尾删法
void ListPopBack(LN* phead);
//头插法
void ListPushFront(LN* phead, DataType x);
//头删法
void ListPopFront(LN* phead);
//查找节点
LN* ListFindNode(LN* phead, DataType x);
//在指定节点前插入
void ListInsertFront(LN* phead,DataType x, DataType insert);
//在指定节点后插入
void ListInsertBack(LN* phead, DataType x, DataType insert);
//更改指定节点
void ListNodeChange(LN* phead, DataType x, DataType change);
//删除指定节点
void ListDeleteNode(LN* phead, DataType x);
//删除链表
void ListFree(LN* phead);
//打印链表
void ListPrint(LN* phead);
二、List.c
代码如下:
#include"List.h"
LN* tail = NULL ;
LN* ListCreateNode(DataType x)
{
LN* tem = (LN*)malloc(sizeof(LN));
tem->data = x;
tem->next = NULL;
return tem;
}
//初始化头结点
void ListInit(LN** phead)
{
*phead = (LN*)malloc(sizeof(LN));
(*phead)->next = *phead;
tail = *phead;
}
//尾插法
void ListPushBack(LN* phead, DataType x)
{
LN* tem = ListCreateNode(x);
tail->next = tem;
tem->next = phead;
tail = tem;
}
//尾删法
void ListPopBack(LN* phead)
{
if (phead == phead->next)
{
printf("链表为空\n");
return;
}
LN* tem = phead->next;
while (tem->next != tail)
{
tem = tem->next;
}
tem->next = phead;
free(tail);
tail = tem;
}
//头插法
void ListPushFront(LN* phead, DataType x)
{
LN* tem = ListCreateNode(x);
tem->next = phead->next;
phead->next = tem;
}
//头删法
void ListPopFront(LN* phead)
{
if (phead == phead->next)
{
printf("链表为空\n");
return;
}
LN* tem = phead->next;
phead->next = tem->next;
free(tem);
}
//查找节点
LN* ListFindNode(LN* phead, DataType x)
{
if (phead == phead->next)
{
printf("链表为空\n");
exit(-1);
}
LN* tem = phead->next;
while (tem != phead)
{
if (tem->data == x)
{
return tem;
}
tem = tem->next;
}
return NULL;
}
//在指定节点前插入
void ListInsertFront(LN* phead, DataType x, DataType num)
{
LN* tem = ListFindNode(phead, x);
if (tem == NULL)
{
printf("链表中没有%d\n", x);
return;
}
LN* insert = ListCreateNode(num);
LN* tempre = phead->next;
while (tempre != tem)
{
tempre = tempre->next;
}
tempre->next = insert;
insert->next = tem;
}
//在指定节点后插入
void ListInsertBack(LN* phead, DataType x, DataType num)
{
LN* tem = ListFindNode(phead, x);
if (tem == NULL)
{
printf("链表中没有%d\n", x);
return;
}
LN* insert = ListCreateNode(num);
if (tem == tail)
{
tem->next = insert;
insert->next = phead;
tail = insert;
}
else
{
insert->next = tem->next;
tem->next = insert;
}
}
//更改指定节点
void ListNodeChange(LN* phead, DataType x, DataType num)
{
LN* tem = ListFindNode(phead, x);
if (tem == NULL)
{
printf("链表中没有%d\n", x);
return;
}
tem->data = num;
}
//删除节点
void ListDeleteNode(LN* phead, DataType x)
{
LN* tem = ListFindNode(phead, x);
if (tem == NULL)
{
printf("链表中没有%d\n", x);
return;
}
if (tem == tail)
{
LN* tailpre = phead->next;
while (tailpre->next != tail)
{
tailpre = tailpre->next;
}
free(tail);
tail = tailpre;
}
else
{
LN* tempre = phead->next;
while (tempre->next != tem)
{
tempre = tempre->next;
}
tempre->next = tem->next;
free(tem);
}
}
//删除链表
void ListFree(LN* phead)
{
LN* tem = phead->next;
while (tem!=phead)
{
LN* next = tem->next;
free(tem);
tem = next;
}
free(phead);
}
//打印链表
void ListPrint(LN* phead)
{
LN* tem = phead->next;
if (tem == phead)
{
printf("链表为空\n");
return;
}
while (tem != phead)
{
printf("%d ", tem->data);
tem = tem->next;
}
printf("\n");
}
三、test.c
代码如下:
#include"List.h"
int main()
{
LN* plist = NULL;
ListInit(&plist);
ListPushBack(plist, 1);
ListPushBack(plist, 2);
ListPushBack(plist, 3);
ListPushBack(plist, 4);
ListPrint(plist);
ListPopBack(plist);
ListPrint(plist);
ListPushFront(plist, 5);
ListPrint(plist);
/*ListPopFront(plist);
ListPopFront(plist);
ListPopFront(plist);
ListPopFront(plist);*/
ListInsertFront(plist, 50, 6);
ListPrint(plist);
ListInsertBack(plist, 5, 7);
ListPrint(plist);
ListNodeChange(plist, 5, 7);
ListPrint(plist);
ListDeleteNode(plist, 7);
ListPrint(plist);
ListDeleteNode(plist, 9);
ListFree(plist);
return 0;
}
总结
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)