• 顺序表代码(指针实现)


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 10
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FLASE 0
    #define OVERFLOW -2
    
    typedef int ElemType;
    typedef int Status;
    typedef struct{
      ElemType * elem;
      int len; 
    } Sqlist;
    
    /**************************
        创建
    输入:顺序表指针
    输出:状态码
    功能:初始化顺序表
    **************************/
    Status initList(Sqlist *L){
      L->elem = (ElemType*)malloc(SIZE * sizeof(ElemType*));
      if(!L->elem){
       printf("overflow!
    ");
       return OVERFLOW;
      }
      L->len = 0;
      return OK;
    }
    
    /**************************
        插入
    输入:顺序表指针,插入位,插入值
    输出:状态码
    功能:插入数据
    **************************/
    Status insertList(Sqlist *L, int index,ElemType e){
    
      index--;    //输入index起始为1,而程序起始为0,下面亦同
    
      //overflow
      if( index>L->len || index<0 ||L->len>=SIZE) {
       printf("overflow!
    ");
       return OVERFLOW;
      }
    
      //insert last one
      if( index == L->len){
        *(L->elem+L->len)=e;
        L->len++;
        return OK;
      }
    
      //ElemType * q = L->elem + index;
      ElemType * q = &(L->elem[index]);
      ElemType * p = &(L->elem[L->len]);
      
      for(;p>q;p--)
         *p=*(p-1);
    
      *p  = e;
      L->len++;
     
      return OK;
    }
    
    /**************************
        删除
    输入:顺序表指针,删除位,存储指针
    输出:状态码
    功能:删除数据并返回其值
    **************************/
    Status deleteList(Sqlist *L,int index,ElemType *e){
      index--;
    
      if(index>L->len||index<0){
        printf("overflow!!
    ");
        return OVERFLOW;
      }
    
      if(index == L->len){
        L->len--;
        return OK;
      }
    
      ElemType * q = L->elem+L->len;
      ElemType * p = &(L->elem[index]);
    
      while(p<q){
        *p = *(p+1);
        p++;
      }
    
      L->len--;
    
      return OK;
    }
    
    /**************************
          追加
    输入:顺序表指针,追加值
    输出:状态码
    功能:追加数据
    **************************/
    Status appendList(Sqlist * L,ElemType e){
      
      if(L->len >= SIZE){
        printf("overflow!
    ");
        return OVERFLOW;
      }
    
      *(L->elem + L->len++) = e;
    
      return OK;
    }
    
    /**************************
          打印
    输入:顺序表指针
    输出:状态码
    功能:将顺序表值一一打印出来
    **************************/
    Status printList(Sqlist L){
      if(L.len == 0)printf("list is empty!
    ");
      ElemType * p = L.elem;
      for(;p<(L.elem+L.len);p++)
         printf("[%d] ",*p);
    
      printf("
    ");
      return OK;
    }
    
    
    int main(){
     int index,e;
     Sqlist L;
     initList(&L);
     printList(L);
     
     //append
     printf("[append] enter value:");
     scanf("%d",&e);
     appendList(&L,e);
     printList(L);
     
     //append
     printf("[append] enter value:");
     scanf("%d",&e);
     appendList(&L,e);
     printList(L);
    
     //insert
     printf("[insert] enter index:");
     scanf("%d",&index);
     printf("[insert] enter value:");
     scanf("%d",&e);
     insertList(&L,index,e);
     printList(L);
    
     //delete
     printf("[delete] enter index:");
     scanf("%d",&index);
     deleteList(&L,index,&e);
     printList(L);
     return 0;
    }

     
     
    下面是另一种写法(但推荐使用上面一种)

    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0
    #define OVERFLOW -2
    
    typedef int ElemType;
    typedef int Status;
    typedef struct{
      ElemType * elem;
      int last;
    } Sqlist;
    
    Sqlist initList(){
      Sqlist L;
      L.elem = (ElemType*)malloc(MAX * sizeof(ElemType));
      if(!L.elem){
        printf("overflow!!
    ");
        exit(OVERFLOW);
      }
      L.last = 0;
      return L;
    }
    
    Sqlist insertList(Sqlist L,int index,ElemType e){
      index--;
      if( L.last >= MAX || index<0 || index>L.last){
        printf("overflow!
    ");
        exit(OVERFLOW);
      }
    
      if( L.last == index ){
         *(L.elem + L.last++) = e;
         return L;
      }
    
      ElemType * p = L.elem + L.last;
      ElemType * q = &L.elem[index];
      while(p>q){
        *p = *(p-1);
        p--;
      }
    
      *p = e;
    
      L.last++;
      return L;
    }
    
    Sqlist deleteList(Sqlist L,int index,ElemType *e){
      index--;
      if( index < 0 || index >=L.last){
        printf("overflow!!
    ");
        exit(OVERFLOW);
      }
    
      if(index == L.last-1){
        L.last--;
        return L;
      }
    
      ElemType * p = &(L.elem[index]);
      ElemType * q = L.elem + L.last - 1;
     
      while(p<q){
        *p = *(p+1);
        p++;
      }
      L.last--;
      return L;
    }
    
    
    Status printList(Sqlist L){
      if(L.last == 0){
        printf("empty
    ");
        return OK;
      }
      ElemType * p = L.elem;
      ElemType * q = L.elem+L.last;
      while(p<q){
       printf("[%d] ",*p);
       p++;
      }
    
      printf("
    ");
      return OK;
    }
    
    Sqlist appendList(Sqlist L,ElemType e){
      if(L.last >= MAX){
       printf("overflow!
    ");
       exit(OVERFLOW);
      }
    
      *(L.elem + L.last++) = e;
      return L;
    }
    
    int main(){
      int index,e;
      Sqlist L = initList();
      printList(L);
    
      //append
      printf("[append] enter value:");
      scanf("%d",&e);
      L = appendList(L,e);
      printList(L);
    
      //append
      printf("[append] enter value:");
      scanf("%d",&e);
      L = appendList(L,e);
      printList(L);
    
      //insert
      printf("[insert] enter index:");
      scanf("%d",&index);
      printf("[insert] enter value:");
      scanf("%d",&e);
      L = insertList(L,index,e);
      printList(L);
    
      //delete
      printf("[delete] enter index:");
      scanf("%d",&index);
      L = deleteList(L,index,&e);
      printList(L);
    
      return 0;
    }
    
     
  • 相关阅读:
    树链剖分
    后缀自动机
    莫队算法。
    线性递推BM模板
    笛卡尔积
    2019牛客暑期多校训练营(第三场) J LRU management 模拟链表操作
    线性基
    bitset 位运算
    Lindström–Gessel–Viennot lemma定理 行列式板子
    三角形
  • 原文地址:https://www.cnblogs.com/demonxian3/p/7118999.html
Copyright © 2020-2023  润新知