//linklist.h
#ifndef _MYLINKLIST_H_
#define _MYLINKLIST_H_
typedef void LinkList;
/*
typedef struct _tag_LinkListNode LinkListNode;
struct _tag_LinkListNode
{
LinkListNode* next;
};
*/
typedef struct _tag_LinkListNode
{
struct _tag_LinkListNode *next;
}LinkListNode;
LinkList* LinkList_Create();
void LinkList_Destroy(LinkList* list);
void LinkList_Clear(LinkList *list);
int LinkList_Length(LinkList *list);
int LinkList_Insert(LinkList *list, LinkListNode *node, int pos);
LinkListNode *LinkList_Get(LinkList *list, int pos);
LinkListNode *LinkList_Delete(LinkList *list, int pos);
#endif
//linklist.cpp
#include"linklist.h"
#include<iostream>
typedef struct _tag_LinkList
{
LinkListNode header;
int length;
}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)
{
if (list == NULL)
{
return;
}
TLinkList *tlist = NULL;
tlist = (TLinkList*)list;
tlist->length = 0;
tlist->header.next = NULL;
return;
}
int LinkList_Length(LinkList *list)
{
if (list == NULL)
{
return -1;
}
TLinkList *tlist = NULL;
tlist = (TLinkList*)list;
return tlist->length;
}
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos)
{
int ret = 0, i = 0;
LinkListNode *current = NULL;
TLinkList *tList = NULL;
if (list == NULL || node == NULL || pos < 0)
{
ret = 0;
printf("func LinkList_Insert()err:%d\n", ret);
}
tList = (TLinkList *)list;
current = &(tList->header);
for (i = 0; i < pos&¤t->next!=NULL; i++)
{
current = current->next;
}
node->next = current->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:%d\n", ret);
return NULL;
}
tList = (TLinkList *)list;
current = &(tList->header);//让辅助指针变量指向链表头部
for (i = 0; i < pos&¤t->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&¤t->next != NULL; i++)
{
current = current->next;
}
ret = current->next;
current->next = ret->next;
return NULL;
}
//线性表链式存储测试框架
#include<iostream>
#include"linklist.h"
typedef struct Teacher
{
LinkListNode node;
int age;
char name[64];
}Teacher;
void main()
{
Teacher t1, t2, t3, t4, t5;
t1.age = 31;
t2.age = 32;
t3.age = 33;
t4.age = 34;
t5.age = 35;
int i=0,len = 0, ret = 0;
LinkList* list = NULL;
list = LinkList_Create();
if (list == NULL)
{
return;
}
len = LinkList_Length(list);
ret = LinkList_Insert(list, (LinkListNode*)(&t1), 0);
ret = LinkList_Insert(list, (LinkListNode*)(&t2), 2);
ret = LinkList_Insert(list, (LinkListNode*)(&t3), 0);
ret = LinkList_Insert(list, (LinkListNode*)(&t4), 0);
ret = LinkList_Insert(list, (LinkListNode*)(&t5), 0);
//遍历
for(i = 0; i < LinkList_Length(list);i++ )
{
Teacher *tmp = (Teacher*)LinkList_Get(list, i);
if (tmp == NULL)
{
return;
}
printf("tmp->age:%d ", tmp->age);
}
while (LinkList_Length(list)>0)
{
Teacher *tmp = (Teacher*)LinkList_Delete(list, 0);
if (tmp == NULL)
{
return;
}
printf("tmp->age:%d ", tmp->age);
}
LinkList_Destroy(list);
}