线性表的实现

#include <stdio.h>
#include <stdlib.h>
//数组长度
#define maxlength 20
#define elemtype int
typedef struct{
 elemtype arrary[maxlength];
 int length;
}sqllist;
void init(sqllist *list)
{
 list->length=0;
}
int insert(elemtype temp,sqllist *list)
{
  if(list->length>maxlength) 
   return -1;
 else
 {
  //list->length是线性表现在的元素个数,list->length-1就可以找到最后一个元素,list->length是往后面新添一个元素所在的位置
  list->arrary[list->length]=temp;
  list->length++;
 }
 return 0;
}
int delete(sqllist *list,int count)
{
 int i;
 if(list->length<count)
  return -1;
 for(i=count-1;i<=list->length;i++)
  list->arrary[i]=list->arrary[i+1];
 list->length--;
 return 0;
}
int show(sqllist *list)
{
 int i;
 if(list->length==0)
 {
  printf("There is no elem\n");
  return -1;
 }
 for(i=0;i<list->length;i++)
  printf("%d\n",list->arrary[i]);
 return 0;
}
//逆置线性表
int reverse(sqllist *list)
{
 int i;
 elemtype temp=0;
 for(i=0;i<list->length/2;i++){
  temp=list->arrary[i];
  list->arrary[i]=list->arrary[list->length-i-1];
  list->arrary[list->length-i-1]=temp;
 }
}
int main(void)
{
 sqllist *list=malloc(sizeof(sqllist));
 init(list);
 insert(1,list);
 insert(2,list);
 insert(3,list);
 show(list);
 reverse(list);
 show(list);
 delete(list,1);
 show(list);
 delete(list,1);
 show(list);
 delete(list,1);
 show(list);
 return 0;
}

你可能感兴趣的:(线性表的实现)