#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#define Error -10000
struct node
{
int *elem;
int len;
};
void InitList(struct node *l)
{
l->elem=(int *)malloc(sizeof(int)*100);
l->len=0;
//如果结构体是指针类型的取里面的值就用->
//而不用.
}
void DestroyList(struct node *l)
{
free(l->elem);
l->len=0;
}
void ClearList(struct node *l)
{
l->len=0;
}
int GetList(struct node *l,int id)
{
if(id<1||id>l->len)
return Error;
return l->elem[id-1];
}
bool ListEmpty(struct node *l)
{
if(l->len==0)
return true;
return false;
}
int ListLength(struct node *l)
{
return l->len;
}
int LocateList(struct node *l,int e)
{
int i;
for(i=0;i<l->len;i++)
{
if(l->elem[i]==e)
{
return i+1;
}
}
return Error;
}
int PriorList(struct node *l,int e)
{
int i;
for(i=0;i<l->len;i++)
{
if(l->elem[i]==e)
{
if(i>0)
{
return l->elem[i-1];
}
}
}
return Error;
}
int NextList(struct node *l,int e)
{
int i;
for(i=0;i<l->len;i++)
{
if(l->elem[i]==e)
{
if(i<l->len-1)
return l->elem[i+1];
}
}
return Error;
}
void ListInsert(struct node *l,int id,int e)
{
if(id>l->len)
{
l->elem[l->len++]=e;
return ;
}
if(id<1)
id=1;
int i;
for(i=l->len-1;i>=id-1;i--)
{
l->elem[i+1]=l->elem[i];
}
l->elem[id-1]=e;
l->len++;
}
void DeleteList(struct node *l,int id)
{
if(id<1||id>l->len)
return ;
int i;
for(i=id-1;i<l->len-1;i++)
{
l->elem[i]=l->elem[i+1];
}
l->len--;
}
void DisplayList(struct node *l)
{
int i;
for(i=0;i<l->len;i++)
printf("%d ",l->elem[i]);
printf("\n");
}
int main()
{
struct node list;
InitList(&list);
ListInsert(&list,3,5);
ListInsert(&list,1,4);
ListInsert(&list,1,3);
ListInsert(&list,1,2);
return 0;
}
线性表
最新推荐文章于 2016-09-02 20:03:30 发布