线性表—由数组实现

来源:互联网 发布:http index.php 编辑:程序博客网 时间:2024/05/29 15:25

#include <stdio.h>
#include <stdlib.h>
#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; i<L->last; i++)
 {
  L->data[i] = L->data[i+1];
 }
    L->last--;
 return 0;
}

原创粉丝点击