#ifndef _STATICLIST_H_
#define _STATICLIST_H_
typedef void StaticList;
typedef void StaticListNode;
StaticList* StaticList_Create(int capacity);
void StaticList_Destroy(StaticList* list);
void StaticList_Clear(StaticList* list);
int StaticList_Length(StaticList* list);
int StaticList_Capacity(StaticList* list);
int StaticList_Insert(StaticList* list, StaticListNode* node, int pos);
StaticListNode* StaticList_Get(StaticList* list, int pos);
StaticListNode* StaticList_Delete(StaticList* list, int pos);
#endif
#include <stdio.h>
#include <malloc.h>
#include "xxx_static_list.h"
#define AVAILABLE -1
typedef struct _tag_StaticListNode
{
unsigned int data;
int next;
} TStaticListNode;
typedef struct _tag_StaticList
{
int capacity;
TStaticListNode header;
TStaticListNode node[];
} TStaticList;
StaticList *StaticList_Create(int capacity) // O(n)
{
TStaticList *ret = NULL;
int i = 0;
// Apply a space for static list. Capacitty plus one reperent node[0-capacity]
if (capacity >= 0)
{
ret = (TStaticList *)malloc(sizeof(TStaticList) + sizeof(TStaticListNode) * (capacity + 1));
}
// Assign value for variable
if (ret != NULL)
{
ret->capacity = capacity;
ret->header.data = 0;
ret->header.next = 0;
// AVAILABLE is for insert function, like a flag
for (i = 1; i <= capacity; i++)
{
ret->node[i].next = AVAILABLE;
}
}
return ret;
}
void StaticList_Destroy(StaticList *list) // O(1)
{
free(list);
}
void StaticList_Clear(StaticList *list) // O(n)
{
TStaticList *sList = (TStaticList *)list;
int i = 0;
if (sList != NULL)
{
sList->header.data = 0;
sList->header.next = 0;
for (i = 1; i <= sList->capacity; i++)
{
sList->node[i].next = AVAILABLE;
}
}
}
int StaticList_Length(StaticList *list) // O(1)
{
TStaticList *sList = (TStaticList *)list;
int ret = -1;
if (sList != NULL)
{
ret = sList->header.data;
}
return ret;
}
int StaticList_Capacity(StaticList *list) // O(1)
{
TStaticList *sList = (TStaticList *)list;
int ret = -1;
if (sList != NULL)
{
ret = sList->capacity;
}
return ret;
}
int StaticList_Insert(StaticList *list, StaticListNode *node, int pos) // O(n)
{
TStaticList *sList = (TStaticList *)list;
int ret = (sList != NULL);
int current = 0;
int index = 0;
int i = 0;
// 1. & 2.Judge weather static list and insert postion are legal
ret = ret && (sList->header.data + 1 <= sList->capacity);
ret = ret && (pos >= 0) && (node != NULL);
if (ret)
{
// 3.Finde available place for insert
for (i = 1; i <= sList->capacity; i++)
{
if (sList->node[i].next == AVAILABLE)
{
index = i;
break;
}
}
// 4.Assign the insert variable a value node
sList->node[index].data = (unsigned int)node;
// Problem: why we shuld define unnecessary node[0]
sList->node[0] = sList->header;
// 5.Poiner move to the insert palace
for (i = 0; (i < pos) && (sList->node[current].next != 0); i++)
{
current = sList->node[current].next;
}
// 6.Reconstruct the link between elementary
sList->node[index].next = sList->node[current].next;
sList->node[current].next = index;
// 7.The list length plus one
sList->node[0].data++;
// 8.Return value form node[0] to header
sList->header = sList->node[0];
}
return ret;
}
StaticListNode *StaticList_Get(StaticList *list, int pos) // O(n)
{
TStaticList *sList = (TStaticList *)list;
StaticListNode *ret = NULL;
int current = 0;
int object = 0;
int i = 0;
// 1. & 2.Judge weather static list and insert postion are legal
if ((sList != NULL) && (0 <= pos) && (pos < sList->header.data))
{
sList->node[0] = sList->header;
// 3.Finde the palace of get elementary
for (i = 0; i < pos; i++)
{
current = sList->node[current].next;
}
// 4.The object of elementary
object = sList->node[current].next;
// 5.The elementary value for return value
ret = (StaticListNode *)(sList->node[object].data);
}
return ret;
}
StaticListNode *StaticList_Delete(StaticList *list, int pos) // O(n)
{
TStaticList *sList = (TStaticList *)list;
StaticListNode *ret = NULL;
int current = 0;
int object = 0;
int i = 0;
// 1. & 2.Judge weather static list and insert postion are legal
if ((sList != NULL) && (0 <= pos) && (pos < sList->header.data))
{
sList->node[0] = sList->header;
// 3.Finde the delete position
for (i = 0; i < pos; i++)
{
current = sList->node[current].next;
}
// 4.Get the delete elementary place poiner
object = sList->node[current].next;
// 5.The poiner should move to next node
sList->node[current].next = sList->node[object].next;
// 6.Static list length minus one
sList->node[0].data--;
sList->header = sList->node[0];
// 7.Delete place is available
sList->node[object].next = AVAILABLE;
// 8.Return value
ret = (StaticListNode *)(sList->node[object].data);
}
return ret;
}