数据结构(单循环链表)

#pragma once

typedef int ELEMTYPE;

typedef struct CNode {

ELEMTYPE data;

struct CNode* next;

}CNode,*PCNode;

void Init_Slink(CNode* plist);

CNode* BuyNode(ELEMTYPE val);

bool Insert_head(CNode* plist,ELEMTYPE val);

bool Insert_Tail(CNode* plist, ELEMTYPE val);

bool Insert_pos(CNode* plist, ELEMTYPE val, int pos);

bool Delete_head(CNode* plist);

bool Delete_Tail(CNode* plist);

bool Delete_pos(CNode* plist, int pos);

bool Delete_val_First(CNode* plist, ELEMTYPE val);

bool Delete_val_All(CNode* plist, ELEMTYPE val);

CNode* Search(CNode* plist, ELEMTYPE val);

bool IsEmpty(CNode* plist);

int Get_Length(CNode* plist);

void Destroy1(CNode* plist);

void Destroy2(CNode* plist);

void Show(CNode* plist);

//单循环链表 只有让其尾部节点的next指向头部

//初始状态下 若后面链表为空 则指向自己

#include<stdio.h>

#include<assert.h>

#include "Seqlist.h"

#include <stdlib.h>

#include <string.h>

#include <errno.h>

#include "Slink.h"

void Init_Slink(CNode* plist)

{

assert(plist != NULL);

plist->next =plist;

}

CNode* BuyNode(ELEMTYPE val)

{

CNode* pnewNode = (CNode*)malloc(1 * sizeof(struct CNode));

if (pnewNode == NULL)

{

exit(EXIT_FAILURE);

}

pnewNode->data = val;

pnewNode->next = NULL;

return pnewNode;

}

bool Insert_head(CNode* plist, ELEMTYPE val)

{

assert(plist != NULL);

CNode* pnewNode = BuyNode(val);

CNode* p = plist;

pnewNode->next = p->next;

p->next = pnewNode;

}

bool Insert_Tail(CNode* plist, ELEMTYPE val)

{

assert(plist != NULL);

CNode* pnewNode = BuyNode(val);

CNode* p = plist;

for (; p->next != plist; p = p->next);

pnewNode->next = p->next;//可以不写

p->next = pnewNode;

}

bool Insert_pos(CNode* plist, ELEMTYPE val, int pos)

{

assert(plist != NULL);

assert(pos >= 0 && pos <= Get_Length(plist));

CNode* pnewNode = BuyNode(val);

CNode* p = plist;

for (int i = 0; i < pos; i++)

{

p = p->next;

}

pnewNode->next = p->next;

p->next = pnewNode;

}

bool Delete_head(CNode* plist)

{

assert(plist != NULL);

if (IsEmpty(plist) == NULL)

return false;

CNode* q = plist->next;

CNode* p = plist;

p->next = q->next;

free(q);

q = NULL;

return 1;

}

bool Delete_Tail(CNode* plist)

{

assert(plist != NULL);

if (IsEmpty(plist) == NULL)

return false;

CNode* q = plist;

for (; q->next != plist; q = q->next);

CNode* p = plist;

for (; p->next !=q; p = p->next);

q->next = p->next;

free(q);

q = NULL;

return 1;

}

bool Delete_pos(CNode* plist, int pos)

{

assert(plist != NULL);

assert(pos >= 0 && pos <= Get_Length(plist));

if (IsEmpty == NULL)

return false;

CNode* p = plist;

for (int i = 0; i < pos-1; i++)

{

p = p->next;

}

CNode* q = p->next;

q->next = p->next;

free(q);

q = NULL;

return 1;

}

bool Delete_val_First(CNode* plist, ELEMTYPE val)

{

assert(plist != NULL);

CNode* q = Search(plist, val);

if (q == NULL)

return false;

CNode* p = plist;

for (; p->next != q; p = p->next);

q->next = p->next;

free(q);

q = NULL;

return 1;

}

bool Delete_val_All(CNode* plist, ELEMTYPE val)

{

assert(plist != NULL);

CNode* q = plist;

CNode* p = plist->next;

while (q != NULL)

{

if (q->data == val)

{

q->next = p->next;

free(q);

q = NULL;

}

else

{

q = q->next;

p ==p->next;

}

}

return true;

}

CNode* Search(CNode* plist, ELEMTYPE val)

{

assert(plist != NULL);

CNode* p = plist->next;

for (; p != plist; p = p->next)

{

if (p->data == val)

return p;

}

}

bool IsEmpty(CNode* plist)

{

return plist->next == NULL;

}

int Get_Length(CNode* plist)

{

int mun;

CNode* p = plist->next;

for (; p != NULL; p = p->next)

{

mun++;

}

return mun;

}

void Destroy1(CNode* plist)

{

while (!IsEmpty(plist))

{

//Del_Head;

}

}

void Destroy2(CNode* plist)

{

CNode* p = plist->next;

CNode* q = NULL;

while (p != NULL)

{

q = p->next;

free(q);

p = q;

}

}

void Show(CNode* plist)

{

CNode* p = plist;

for (; p->next != NULL; p = p->next)

printf("%d ", p->data);

}

相关推荐
风筝在晴天搁浅6 小时前
手撕快速排序
数据结构
图码6 小时前
矩阵数据结构入门指南:声明、初始化与基本操作
运维·数据结构·线性代数·算法·矩阵
地球资源数据云7 小时前
1960年-2024年中国棉花产量数据集
大数据·数据结构·数据仓库·人工智能
汉克老师8 小时前
GESP2025年6月认证C++五级( 第一部分选择题(1-8))
c++·链表·线性筛·最大公约数·gesp5级·gesp五级·埃氏筛
木木_王9 小时前
嵌入式Linux学习 | 数据结构(Day06)全解:线性表 + 栈队列 + 静态库 / 动态库(原理 + 代码 + 编译实战 + 易错点)
linux·数据结构·笔记·学习
@小码农9 小时前
2026年信息素养大赛【星火征途】图形化编程复赛和决赛模拟题B
开发语言·数据结构·c++·算法
人道领域9 小时前
【LeetCode刷题日记】347.前k个高频元素
java·数据结构·算法·leetcode
此生决int9 小时前
快速复习之数据结构篇——链表
数据结构·链表
深邃-9 小时前
【数据结构与算法】-二叉树(1):树的概念与结构,二叉树的概念与结构
数据结构·算法·链表·二叉树··顺序表
风筝在晴天搁浅9 小时前
手撕归并排序
数据结构·算法·排序算法