#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CSignleLinkList.h"
typedef struct _strTLinkList
{
CNode header;
int lenght;
}TLinkList;
CLinkList * CreateLinkList()
{
TLinkList *list = (TLinkList*)malloc(sizeof(TLinkList));
memset(list, 0, sizeof(TLinkList));
list->header.next = NULL;
list->lenght = 0;
return (CLinkList*)list;
}
int InsertLinkListNode( const CLinkList *list, CNode *node, int pos )
{
int index = 0;
CNode *current = NULL;
CNode *temp = NULL;
TLinkList *tlist = (TLinkList*)list;
if (!list)
{
return -1;
}
current = &((TLinkList *)list)->header;
pos = pos < 0 ? 0 : pos;
pos = pos > tlist->lenght ? tlist->lenght : pos;
for (index = 0; index < pos; index++)
{
current = current->next;
}
temp = (CNode*)malloc(sizeof(CNode));
memset(temp, 0, sizeof(CNode));
temp = node;
temp->next = current->next;
current->next = temp;
tlist->lenght++;
return pos;
}
const CNode * GetLinkListNode( const CLinkList *list, int pos )
{
int index = 0;
CNode *current = NULL;
TLinkList *tlist = NULL;
if (!list)
{
return current;
}
current = &((TLinkList *)list)->header;
tlist = (TLinkList*)list;
if (pos < 0 || pos > tlist->lenght)
{
return current;
}
for (index = 0; index < pos; index++)
{
current = current->next;
}
return current->next;
}
int DeleteLinkListNode( const CLinkList *list, int pos, CNode **node )
{
int index = 0;
CNode *current = NULL;
CNode *temp = NULL;
TLinkList *tlist = NULL;
if (!list)
{
return -1;
}
current = &((TLinkList *)list)->header;
tlist = (TLinkList*)list;
if (pos < 0 || pos > tlist->lenght)
{
return -1;
}
for (index = 0; index < pos; index++)
{
current = current->next;
}
temp = current->next;
current->next = temp->next;
tlist->lenght--;
temp->next = NULL;
if (node)
{
*node = temp;
}
// free(temp);
// temp = NULL;
return index;
}
int GetLinkListLenght( const CLinkList *list )
{
TLinkList *tlist = NULL;
if (!list)
{
return 0;
}
tlist = (TLinkList*)list;
return tlist->lenght;
}
int ClearLinkList( const CLinkList *list )
{
TLinkList *tlist = NULL;
CNode *first = NULL;
CNode *temp = NULL;
if (!list)
{
return -1;
}
tlist = (TLinkList*)list;
first = tlist->header.next;
while (first != NULL)
{
temp = first->next;
first->next = NULL;
// free(first);
// first = NULL;
first = temp;
}
tlist->lenght = 0;
return tlist->lenght;
}
void DestoryLinkList( CLinkList *list )
{
TLinkList *tlist = NULL;
if (!list)
{
return;
}
ClearLinkList(list);
tlist = (TLinkList*)list;
free(tlist);
}
C/C++实现单向链表
需积分: 38 196 浏览量
2019-01-16
14:21:10
上传
评论 5
收藏 551KB ZIP 举报
laying_1230
- 粉丝: 3
- 资源: 2
最新资源
- 基于springboot+vue实现的在线考试系统+源代码+文档
- RTL8723DS 2022 版本 Linux驱动,android驱动 支持4.0-10x
- 要玩NDS的遊戲,必須要先下載三個bios檔案到你的檔案資料夾
- 各类型数据库4月排名,基于排名网站数据爬虫json结果
- 基于springboot+vue实现的在线考试系统+源代码+文档
- 淮北市杜集区人才补贴+生活补贴
- JAVA-JSP技术文档
- 课内实验02-决策表(共享单车月卡).docx
- 基于【React + Node+SpringBoot】疫情数据查看系统的设计与实现【源码+lw+部署+讲解】
- 基于【React + Node】云课堂系统设计与实现【源码+lw+部署+讲解】
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈