数据结构之顺序表实现



      顺序表是ADT中的基本类型,它一般用数组来储存数据的数据。顺序表的优点是存储数据方便,缺点是插入和删除数据时,需要移动结点。

#include <stdio.h>
#include <stdlib.h>

//typedef int datatype;//定义顺序表的元素类型

#define LIST_INIT_SIZE 20  //顺序表存储空间初始分配量
#define LIST_INCREMENT 2  //顺序表存储空间增量

typedef struct{
  int *elem; 	 //存储空间基址
  int length;    //顺序表的长度
  int listsize;  //申请的空间大小(sizeof(datatype))
}Sqlist;

//1.创建一个空表
int initlist(Sqlist *L)
{
  L->elem = malloc(LIST_INIT_SIZE * sizeof(int));
  if(L->elem == NULL)
  {
    printf("overflow!\n");
    //exit(0);
    return -1;
  }
  L->length = 0;//
  L->listsize = LIST_INIT_SIZE;//
  
  return 0;
}

//2.获取表的长度
int getlength(Sqlist *L,int len)
{
  len = L->length;
  if(len == 0)
  {
    printf("is a emptylist!");
    return -1;
  }
  return 0;
}

//3.获取表中位置i的元素
int getvalue(Sqlist *L,int i,int x)
{
  if((i<1) || (i>L->length))
    return -1;
  x =  *(L->elem + i - 1);

  return 0;
}

//4.取i的前驱元素
int getprevalue(Sqlist *L,int cur_e,int pre_e)
{
  int *p = L->elem;
  if(cur_e == *p)
  {
    printf("is the first!\n");
    return -1;
  }
  else if(cur_e <= L->length)
	  pre_e = *(L->elem + cur_e - 2);

  return 0;
}

//5.取i的后继元素
int getnextvalue(Sqlist *L,int cur_e,int next_e)
{
  int *p = L->elem;
  int i = 0;
  while( (i < L->length) &&(cur_e <= L->length))
  {
    i++;
    p = L->elem + i;
  }
  if(next_e == *p)
  {
    printf("is the last one!");
    return -1;
  }
  next_e = *(L->elem + cur_e);

  return 0;
}

//6.获取元素x在表中的位置
int getlocation(Sqlist *L,int x,int loction)
{
  int i = 0;
  int *p = L->elem;
  while(x != *p)
  {
    i++;
    p++;
  }
  loction = i + 1;

  return 0;
}

//7.把一个元素x插入位置location处
int insertlist(Sqlist *L,int x,int location)
{
  int *new_elem,*p,*q;
  int i = 0;
  if(i > L->length)
    return -1;

  new_elem = realloc(L->elem,((L->listsize +LIST_INCREMENT)*sizeof(int)));
  if(new_elem == NULL)
  {
    printf("realloc error!\n");
    return -1;
  }
  L->elem = new_elem;
  L->listsize += LIST_INCREMENT;
  
  p = L->elem + location - 1 ;
  for(q=L->elem+L->length-1; p<=q; --q)
    *(q+1) = *q;

  *p = x;
  L->length += 1;
  
  return 0;
}

//8.从表中删除位置location处的元素
int deletelist(Sqlist *L,int location)
{
  int *p,*q;
  if(location > L->length)
    return -1;
  
  p = L->elem + location -1;
  q = L->elem + L->length -1;
  for(p; p<=q; ++p)
    *p = *(p+1);
  L->length--;

  return 0;
}

//9.判断是否为空表
int isempty(Sqlist *L)
{
  if(L->length == 0)
    return 0;
  else
    return -1;
}

//10.清除表中所有的元素
int clearall(Sqlist *L)
{
  L->length = 0;
}

//11.遍历输出表中所有元素
void traverlist(Sqlist *L)
{
  int i = 0;
  for(i; i<L->length; i++)
  {
    printf("elem=%d\n",*(L->elem+i));
  }
}

//12.在表中查找元素x
int findelement(Sqlist *L,int x)
{
  int i = 0,j = 0;
  for(j; j<L->length; j++)
  {
    i++;
    if(x == *(L->elem + j))
      break;
  }
  if(i = L->length - 1)
  {
    printf("can't find!\n");
    return -1;
  }
  
  return 0;
}

//13.修改位置location处的元素
int updatalist(Sqlist *L,int location,int new)
{
  if(location > L->length)
    return -1;
  *(L->elem + location - 1) = new;

  return 0;
}

//14.对所有的元素重新按给定的条件排序


//15.销毁表
int destorylist(Sqlist *L)
{
  free(L->elem);
  L->elem = NULL;
  L->length = 0;
  L->listsize = 0;
}

int main(void)
{
  
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值