#include <stdio.h>
#include <stdlib.h>
typedef struct{
int *plist;
int size;
int len;
}list;
void InitList(list *l,int size);
void Distroy(list *l);
void ClearList(list *l);
int ListEmpty(list *l);
int ListLength(list *l);
int GetElem(list *l,int i,int *elem);
int LocateElem(list *l,int *elem);
int PriorElem(list *l,int *currentElem,int *preElem);
int NextElem(list *l,int *currentElem,int *NextElem);
int ListInsert(list *l,int i,int *elem);
int ListDelete(list *l,int i,int *elem);
void ListTraverse(list *l);
void InitList(list *l,int size)
{
l->size=size;
l->plist=(int *)malloc(sizeof(list)*size);
l->len=0;
}
void Distroy(list *l)
{
free(l->plist);
l->plist=NULL;
}
void ClearList(list *l)
{
l->len=0;
}
int ListEmpty(list *l)
{
if (l->len)
return 0;
return 1;
}
int ListLength(list *l)
{
return l->len;
}
int GetElem(list *l,int i,int *elem)
{
if(i<0||i>=l->size)
return 1;
*elem=l->plist[i];
return 0;
}
int LocateElem(list *l,int *elem)
{
int i;
for (i=0;i<l->len;i++)
{
if(l->plist[i]==*elem)
{
return i;
}
}
return -1;
}
int PriorElem(list *l,int *currentElem,int *preElem)
{
int temp=LocateElem(l,currentElem);
if(temp==-1)
{
return -1;
}
else
{
if(temp==0)
{
return -1;
}
else
{
*preElem=l->plist[--temp];
return 0;
}
}
}
int NextElem(list *l,int *currentElem,int *NextElem)
{
int temp=LocateElem(l,currentElem);
if(temp==-1)
{
return -1;
}
else
{
if(temp==l->size-1)
{
return -1;
}
else
{
*NextElem=l->plist[++temp];
return 0;
}
}
}
int ListInsert(list *l,int i,int *elem)
{
int k;
if (i<0||i>l->len)
{
return -1;
}
//l->plist=(int *)realloc(l->plist,(l->size+1)*sizeof(int));
for (k=l->len-1;k>=i;k--)
{
l->plist[k+1]=l->plist[k];
}
l->plist[i]=*elem;
l->len++;
return 0;
}
int ListDelete(list *l,int i,int *elem)
{
int k;
if (i<0||i>=l->len)
{
return -1;
}
*elem=l->plist[i];
for (k=i+1;k<l->len;k++)
{
l->plist[k-1]=l->plist[k];
}
//l->plist=(int *)realloc(l->plist,(l->size-1)*sizeof(int));
l->len--;
return 0;
}
void ListTraverse(list *l)
{
int i;
for (i=0;i<l->len;i++)
{
printf("%d\n",l->plist[i]);
}
}