线性表_顺序存储/基本操作

线性表_顺序存储/基本操作


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

 

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define Elemtype int
#define OK 1
#define OVERFLOW -2
#define status int
#define TRUE 1
#define FALSE 0
#define ERROR 0
typedef struct
{
     Elemtype *head;
     int length;
     int listsize;
}Sqlist;
status initSqlist(Sqlist &L)
{//构造顺序表
     L.head=(Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
     if(!L.head) exit(OVERFLOW);//分配失败
     L.length=0;//表长度置空
     L.listsize=LIST_INIT_SIZE;
     return OK;
}//initlist
void clearSqlist(Sqlist &L)
{
     L.length=0;
}//clearlist
status destroySqlist( Sqlist &L)
{//销毁顺序表
     clearSqlist(L);
     free(L.head);
}//destroylist

 

status SqlisTempty(Sqlist L)
{
     if(!L.length) return TRUE;
     else return  FALSE;
}//listempty
int SqlistLength(Sqlist L)
{
     return (L.length);
}//listlength
status getSqlistElem(Sqlist L,int i,Elemtype &e)
{
     if(i<1||i>L.length) return FALSE;
     e=L.head[i-1];
     return OK;
}//getSqlist

status locateElem(Sqlist L,int i)
{


}//locateElem

status priorElem(Sqlist L,Elemtype cur_e,Elemtype &pre_e)
{
     int i=1;
     while(i<=L.length&&L.head[i-1]!=cur_e){
          i++;
     }
     if(i>L.length||i==1) return FALSE;
     else {pre_e=L.head[i-2];return TRUE;}
}//priorElem

status nextElem(Sqlist L,Elemtype cur_e,Elemtype &next_e)
{
     int i=1;
     while(i<=L.length&&L.head[i-1]!=cur_e){i++;}
     if(i>=L.length) return FALSE;
     else next_e=L.head[i];
}//nextElem

status insertSqlist(Sqlist &L,int i,Elemtype e)
{
     if(i<1||i>L.length+1) return FALSE;
     if(L.length>=L.listsize){
         Elemtype *newhead;
         newhead=(Elemtype *)realloc(L.head,(LIST_INIT_SIZE+LISTINCREMENT)*sizeof(Elemtype));
         if(!newhead) exit(OVERFLOW);
         L.head=newhead;
         L.listsize+= LISTINCREMENT;
     }
     Elemtype *q,*p=&L.head[i-1];
     for(q=p;q<=&L.head[L.length-1];q++){
          *(q+1)=*q;
     }
     *p=e;
     ++L.length;
     return OK;
}//insertSqlist

status deleteSqlist(Sqlist &L,int i,Elemtype &e)
{
    if(i<1||i>L.length) return ERROR;
    int *q,*p=&L.head[i-1];
    e=*p;
    for(q=++p;q<&L.head[L.length-1];q++){
        *(q-1)=*q;       
        }
    --L.length;
    return OK;        
}//deleteSqlist

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值