#define _LINKLIST_H
typedef void LinkList;
typedef struct _Tag_LinkListNode LinkListNode;
struct _Tag_LinkListNode
{
LinkListNode* next;
};
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
#include<stdio.h>
#include<malloc.h>
#include"linklist.h"
typedef struct _Tag_LinkList
{
LinkListNode header;
int length;
}TLinkList;
LinkList* LinkList_Create()
{
TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList));
if( ret != NULL)
{
ret->header.next = NULL;
ret->length = 0;
}
return ret;
}
void LinkList_Destroy(LinkList* list)
{
free(list);
}
void LinkList_Clear(LinkList* list)
{
TLinkList* sList = (TLinkList*)list;
if( sList != NULL)
{
sList->header.next = NULL;
sList->length = 0;
}
}
int LinkList_Length(LinkList* list)
{
TLinkList* sList = (TLinkList*)list;
int ret = -1;
if( sList != NULL)
{
ret = sList->length;
}
return ret;
}
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos)
{
TLinkList* sList = (TLinkList*)list;
int i = 0;
int ret = ((sList != NULL)&&(0 <= pos)&&(node != NULL));
if( ret )
{
LinkListNode* current = (LinkListNode*)sList;
for(i = 0;((i < pos)&&(current->next != NULL));i++)
{
current = current->next;
}
node->next = current->next;
current->next = node;
sList->length++;
}
return ret;
}
LinkListNode* LinkList_Get(LinkList* list, int pos)
{
TLinkList* sList = (TLinkList*)list;
LinkListNode* ret = NULL;
int i = 0;
if( (sList != NULL)&&(0 <= pos)&&(pos < sList->length) )
{
LinkListNode* current = (LinkListNode*)sList;
for(i = 0;i < pos;i++)
{
current = current->next;
}
ret = current->next;
}
return ret;
}
LinkListNode* LinkList_Delete(LinkList* list, int pos)
{
TLinkList* sList = (TLinkList*)list;
LinkListNode* ret = NULL;
int i = 0;
if( (sList != NULL)&&(0 <= pos)&&(pos < sList->length) )
{
LinkListNode* current = (LinkListNode*)sList;
for(i = 0;i < pos;i++)
{
current = current->next;
}
ret = current->next;
current->next = ret->next;
sList->length--;
}
return ret;
}
#include <stdio.h>
#include <stdlib.h>
#include"linklist.h"
struct value
{
LinkListNode header;
int v;
};
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
LinkList* list = LinkList_Create();
int i = 0;
struct value v1;
struct value v2;
struct value v3;
struct value v4;
struct value v5;
struct value v6;
v1.v = 1;
v2.v = 2;
v3.v = 3;
v4.v = 4;
v5.v = 5;
v6.v = 6;
LinkList_Insert(list,(LinkListNode*)&v1,0);
LinkList_Insert(list,(LinkListNode*)&v2,0);
LinkList_Insert(list,(LinkListNode*)&v3,0);
LinkList_Insert(list,(LinkListNode*)&v4,0);
LinkList_Insert(list,(LinkListNode*)&v5,0);
LinkList_Insert(list,(LinkListNode*)&v6,0);
for(i = 0;i < LinkList_Length(list);i++)
{
struct value* pv =(struct value*)LinkList_Get(list,i);
printf("%d\n",pv->v);
}
while( LinkList_Length(list) > 0)
{
struct value* pv =(struct value*)LinkList_Delete(list,0);
printf("%d\n",pv->v);
}
LinkList_Destroy(list);
return 0;
}