您的位置:首页 > 其它

线性表链式存储_api函数实现

2017-03-08 18:12 330 查看
传智课程学习笔记。

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "linklist.h"

typedef struct _tag_LinkList
{
int length;
LinkListNode header;
}TlinkList;

LinkList* LinkList_Create()
{
TLinkList* ret = NULL;
ret = (TLinkList *)malloc( sizeof(TLinkList));
memset( ret, 0, sizeof(TLinkList));
ret->length = 0;
ret->header.next = NULL;

return ret;
}

void LinkList_Destroy( LinkList* list)
{
if( list != NULL)
{
free( list);
list = NULL;
}

return ;
}

void LinkList_Clear( LinkList* list)
{
TLinkList* tList = NULL;
if( list == NULL )
{
return ;
}
tList = ( TLinkList *)list;
tList->length = 0;
tList->header.next = NULL;
return ;
}

int LinkList_Length( LinkList *list)
{
TLinkList *tList = NULL;
if( list == NULL )
{
return -1;
}
tList = (TlinkList *)list;
return tList->length;
}

int LinkList_Insert( LinkList* list, LinkListNode* node, int pos)
{
int ret = 0, i=0;
LinkListNode *current = NULL;
if( list == NULL || node == NULL || pos<0 )
{
ret = 0;
printf("func LinkList_Insert() err:%d\n", ret);
return ret;
}
tList = ( TLinkList *)list;
current = &( tList->header);
for( i=0; i<pos&¤t->next!=NULL; i++)
{
current = current->next;
}

node->next = currentt->next;

current->next = node;
tList->length++;
return 0;
}

LinkListNode* LinkList_Get( LinkList* list, int pos)
{
int ret = 0 ,i=0;
LinkListNode *current = NULL;

TLinkList *tList = NULL;
if( list == NULL || pos<0)
{
ret = 0;
printf("func linklist insert() err:%\n", ret);
}

tList = (TLinkList *)list;
current = &(tList->header);

for( i=0; i<pos&&( current->next!=NULL); i++)
{
current = current->next;
}

return current->next;
}

LinkListNode* LinkList_Delete( LinkList* list, int pos)
{
int i=0;
LinkListNode *current = NULL;
LinkListNode *ret = NULL;

TLinkList *tList = NULL;
if( list == NULL || pos<0)
{
printf("func LinkList_Insert() err:%d\n", ret);
return NULL;
}
tList = (TLinkList *)list;
current = &(tList->header);
for( i=0; i<pos&&(current->next!=NULL); i++)
{
current = current->next;
}

ret = current->next;
current->next = ret->next;
return ret;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: