数据结构与算法:单向链表实现与封装

数据结构与算法:单向链表实现与封装,第1张

概述今天小编就为大家分享一篇关于数据结构算法:单向链表实现与封装,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

概述

单向链表分为单向有头链表和单线无头链表,本文针对单向有头链表使用C语言来实现并进行封装。

实现

List_head.h文件

#ifndef _List_H_

#define _List_H_

typedef int datatype;

#define SUCC

#define MALLOC_FAIL 1

#define NOheadNODE 2

#define INDEXFAIL 3

#define List_EMPTY 4

#define List_NOEMPTY 5

#define FAIL 10

typedef struct List_Node

{

datatype data;

struct List_Node* pNext;

}List;

List*List_create();

int List_insert_at(List* phead,int i,datatype* pData);

int List_order_insert(List* phead,datatype* pData);

int List_delete_at(List* phead,int index);

int List_delete(List* phead,datatype* pData);

int List_isempty(List* phead);

voID List_display(List* phead);

voID List_destory(List* phead);

#endif // !_List_H_

List_head.c文件

/********************************************************

copyright (C),2016-2017,

filename: List

Author: woniu201

Description:单向有头链表使用

********************************************************/

#include

#include "List_head.h"

/************************************

@ BrIEf: 创建链表头

@ Author: woniu201

@ Return:

************************************/

List* List_create()

{

List* pNode = (List *)malloc(sizeof(List));

memset(pNode,sizeof(List));

if (pNode == NulL)

{

return MALLOC_FAIL;

}

pNode->pNext = NulL;

return pNode;

}

/************************************

@ BrIEf: 按位置插入节点

@ Author: woniu201

@ Return:

************************************/

int List_insert_at(List* phead,datatype* pData)

{

int j = 0;

if (phead == NulL)

{

return NOheadNODE;

}

List* pNode = phead;

if (i<0)

{

return INDEXFAIL;

}

while (j< i && pNode !=NulL)

{

pNode = pNode->pNext;

j++;

}

if (pNode == NulL)

{

return INDEXFAIL;

}

else

{

List* newNode = (List*)malloc(sizeof(List));

if (newNode ==NulL)

{

return MALLOC_FAIL;

}

memset(newNode,sizeof(List));

newNode->data = *pData;

pNode->pNext = newNode;

}

return SUCC;

}

/************************************

@ BrIEf: 按顺序插入节点

@ Author: woniu201

@ Return:

************************************/

int List_order_insert(List* phead,datatype* pData)

{

if (phead == NulL)

{

return NOheadNODE;

}

List* pNewNode = (List*)malloc(sizeof(List));

if (pNewNode == NulL)

{

return MALLOC_FAIL;

}

memset(pNewNode,sizeof(List));

pNewNode->data = *pData;

List* pNode = phead;

if (pNode->pNext == NulL)

{

pNode->pNext = pNewNode;

return SUCC;

}

while (pNode->pNext != NulL && pNode->pNext->data < *pData)

{

pNode = pNode->pNext;

}

if (pNode->pNext)

{

pNewNode->pNext = pNode->pNext;

pNode->pNext = pNewNode;

}

else

{

pNode->pNext = pNewNode;

}

return SUCC;

}

/************************************

@ BrIEf: 按位置删除节点

@ Author: woniu201

@ Return:

************************************/

int List_delete_at(List* phead,int index)

{

int j = 0;

if (phead == NulL)

{

return NOheadNODE;

}

if (index < 0)

{

return INDEXFAIL;

}

List* pCur = phead;

List* pNode = phead;

while (pCur->pNext)

{

pNode = pCur;

pCur = pCur->pNext;

if (index == j)

{

break;

}

j++;

}

if (j< index)

{

printf("不存在该节点n");

return INDEXFAIL;

}

else

{

if (pCur->pNext == NulL)

{

pNode->pNext = NulL;

}

else

{

pNode->pNext = pCur->pNext;

}

free(pCur);

pCur = NulL;

}

return SUCC;

}

/************************************

@ BrIEf: 按值删除节点

@ Author: woniu201

@ Return:

************************************/

int List_delete(List* phead,datatype* pData)

{

if (phead == NulL)

{

return NOheadNODE;

}

List* pCur = phead;

List* pNode = phead;

int bFind = 0;

while (pCur->pNext)

{

pNode = pCur;

pCur = pCur->pNext;

if (pCur->data == *pData)

{

bFind = 1;

break;

}

}

if (!bFind)

{

printf("不存在该节点n");

return INDEXFAIL;

}

else

{

if (pCur->pNext == NulL)

{

pNode->pNext = NulL;

}

else

{

pNode->pNext = pCur->pNext;

}

free(pCur);

pCur = NulL;

}

return SUCC;

}

/************************************

@ BrIEf: 判断链表是否为空

@ Author: woniu201

@ Return:

************************************/

int List_isempty(List* phead)

{

if (phead->pNext == NulL)

{

return List_EMPTY;

}

else

{

return List_NOEMPTY;

}

}

/************************************

@ BrIEf: 遍历打印链表

@ Author: woniu201

@ Return:

************************************/

voID List_display(List* phead)

{

if (List_isempty(phead) == List_EMPTY)

{

printf("链表为空n");

return FAIL;

}

List* pNode = phead->pNext;

while (pNode)

{

printf("%dn",pNode->data);

pNode = pNode->pNext;

}

}

/************************************

@ BrIEf: 释放链表内存

@ Author: woniu201

@ Return:

************************************/

voID List_destory(List* phead)

{

List* pCur = phead;

List* pNext = phead->pNext;

while (pNext)

{

pNext = pNext->pNext;

free(pCur);

pCur = NulL;

pCur = pNext;

}

}

main.c 测试

#include

#include "List_head.h"

int main()

{

List* phead = List_create();

int data1 = 1;

int data2 = 3;

int data3 = 2;

// int ret = List_insert_at(phead,&data1);

// ret = List_insert_at(phead,1,&data2);

// if (ret == INDEXFAIL)

// {

// printf("添加索引位置错误n");

// }

List_order_insert(phead,&data2);

List_order_insert(phead,&data1);

List_order_insert(phead,&data3);

List_delete_at(phead,3);

int deleteData = 1;

List_delete(phead,&deleteData);

List_display(phead);

List_destory(phead);

return 1;

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对内存溢出的支持。如果你想了解更多相关内容请查看下面相关链接

总结

以上是内存溢出为你收集整理的数据结构与算法:单向链表实现与封装全部内容,希望文章能够帮你解决数据结构与算法:单向链表实现与封装所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264909.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存