Chinaunix首页 | 论坛 | 博客
  • 博客访问: 848089
  • 博文数量: 156
  • 博客积分: 6553
  • 博客等级: 准将
  • 技术积分: 3965
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-22 18:36
文章存档

2012年(3)

2011年(43)

2010年(110)

分类: C/C++

2010-10-03 15:55:44

#include
#include
#define MAX 6
typedef int datatype;
typedef struct
{
 datatype data[MAX];
 int last;
} sqlist;
sqlist *CreateEmptyList_1()
{
 sqlist *L;
 L = (sqlist *)malloc(sizeof(sqlist));
 L->last = -1;
 return L;
}
void CreateEmptyList_2(sqlist **L)
{
 *L = (sqlist *)malloc(sizeof(sqlist));
 (*L)->last = -1;
 return;
}
int LengthList(sqlist *L)
{
 return (L->last + 1);
}
int EmptyList(sqlist *L)
{
 return (-1 == L->last);
}
int FullList(sqlist *L)
{
 return (MAX-1 == L->last);
}
void ClearList(sqlist *L)
{
 L->last = -1;
 return;
}
void VisitList(sqlist *L)
{
 int i;
 for (i=0; i<=L->last; i++)
 {
  printf("%d ", L->data[i]);
 }
 printf("\n");
 return;
}
int LocateList(sqlist *L, datatype x)
{
 int i = 0;
 
 while (i <= L->last)
 {
  if (L->data[i] == x) return i;
  i++;
 }
 return -1;
}
datatype GetList_1(sqlist *L, int pos)
{
 return L->data[pos];
}
int GetList_2(sqlist *L, int pos, datatype *x)
{
 if ((pos < 0) || (pos > L->last)) return -1;
 *x = L->data[pos];
 return 0;
}
int InsertList(sqlist *L, datatype x, int pos)
{
 int i;
 if ((pos < 0) || (pos > L->last+1) || FullList(L)) return -1;
 
 for (i=L->last+1; i>pos; i--)
 {
  L->data[i] = L->data[i-1];
 }
 L->data[pos] = x;
 L->last++;
 return 0;
}
int DeleteList(sqlist *L, int pos)
{
 int i;
 if ((pos < 0) || (pos > L->last)) return -1;
 for (i=pos; ilast; i++)
 {
  L->data[i] = L->data[i+1];
 }
    L->last--;
 return 0;
}
阅读(594) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~