zoukankan      html  css  js  c++  java
  • 线性表顺序存储方式的C语言实现

      1 /*
      2 编译器VC6++
      3 文件名1.cpp
      4 代码版本号:1.0
      5 时间:2015年9月14日16:39:21
      6 */
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 
     10 #define OK 1
     11 #define ERROR 0
     12 #define TRUE 1
     13 #define FALSE 0
     14 #define OVERFLOW -2
     15 #define LIST_INIT_SIZE 10 
     16 #define LIST_INCREMENT 10
     17 
     18 typedef int ElemType;
     19 typedef int Status;
     20 typedef struct{
     21     ElemType *base;
     22     int length;
     23     int listsize;
     24 } Sqlist;
     25 
     26 Status initSqlist(Sqlist *l)//初始化线性表,分配容量
     27 {
     28     (*l).base = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
     29     
     30     if (!((*l).base))
     31         exit(OVERFLOW);
     32     (*l).length =0;
     33     (*l).listsize =LIST_INIT_SIZE;
     34     return OK;
     35 }
     36 
     37 Status assignSqlist(Sqlist *l) //按位序assign值到线性表中
     38 {
     39     int i;
     40     if(l->base!=NULL)
     41     {
     42         l->length=LIST_INIT_SIZE;
     43         for (i = 0; i < LIST_INIT_SIZE; i++)
     44             (l->base)[i] = i;
     45         return OK;
     46     }
     47     else
     48         return ERROR;
     49 }
     50 
     51 Status ListInsert(Sqlist *l, int i, ElemType e) //元素插入
     52 {
     53     ElemType *newbase=NULL;
     54     ElemType *q;
     55     //ElemType *p = (*l).base + i - 1;
     56     if (i<1 || i>(*l).length + 1)
     57     {
     58         printf("请插入在第1-%d个元素之间", (*l).length + 1);
     59         return 0;
     60     }
     61     if ((*l).length >= (*l).listsize)
     62     {
     63         printf("我执行了");
     64         newbase = (ElemType *)realloc((*l).base,
     65             ((*l).listsize + LIST_INCREMENT) * sizeof(ElemType));
     66         if (!newbase)
     67             exit(0);
     68         (*l).base = newbase; // 新基址
     69         (*l).listsize += LIST_INCREMENT; // 增加存储容量
     70     }
     71     for (q = (*l).base + (*l).length - 1; q >= (*l).base + i-1;q--)
     72         *(q + 1) = *q;
     73 
     74     *((*l).base + i - 1) = e;
     75     (*l).length++;
     76     return OK;
     77 }
     78 
     79 Status GetElem(Sqlist *l,int i,ElemType *e)
     80 {
     81     if(i>=1&&i<=l->length)
     82     {
     83         *e=l->base[i-1];
     84         return OK;
     85     }
     86     else
     87     {
     88         printf("您输入的元素位序不合法
    ");
     89         return ERROR;
     90     }
     91 }
     92 
     93 Status mergeList(Sqlist *lc,Sqlist la,Sqlist lb)
     94 {
     95     int i=1;
     96     int j=1;
     97     ElemType *a=la.base;
     98     ElemType *b=lb.base;
     99     ElemType *c;
    100     lc->listsize=lc->length=la.length+lb.length;
    101     lc->base=(ElemType *)malloc(lc->listsize*sizeof(ElemType));
    102     if(!lc->base)
    103         exit(OVERFLOW);
    104 
    105     c=lc->base;
    106 
    107     while(i<=la.length&&j<=lb.length)
    108     {
    109         if(*a<=*b)
    110         {
    111             *c++=*a++;
    112             i++;
    113         }
    114         else
    115         {
    116             *c++=*b++;
    117             j++;
    118         }
    119     }
    120 
    121     while(i<=la.length)
    122     {
    123         *c++=*a++;
    124         i++;
    125     }
    126     while(j<=lb.length)
    127     {
    128         *c++=*b++;
    129         j++;
    130     }
    131 
    132     return OK;
    133 }
    134 
    135 Status listDelete(Sqlist *l,int i,ElemType *e)
    136 {
    137     if(i<1||i>l->length)    return ERROR;
    138     ElemType *p=l->base+i-1;
    139     *e=*p;
    140     for(*p;p<(l->base+l->length-1);p++)
    141         *p=*(p+1);
    142 
    143     l->length--;
    144     return OK;
    145 }
    146 
    147 Status priorElem(Sqlist *l,ElemType e,ElemType *prior_elem)
    148 {
    149     int i=1;
    150     while(i<=l->length&&l->base[i-1]!=e)
    151     {
    152         i++;
    153     }
    154     if(i==(l->length+1))
    155     {
    156         printf("
    元素%d不在线性表中
    ",e);
    157         return ERROR;
    158     }
    159     else
    160     {
    161         if(i==1)
    162         {
    163             printf("该元素没有前驱
    ");
    164             return ERROR;
    165         }
    166         else
    167         {
    168             *prior_elem=l->base[i-2];
    169             return OK;
    170         }
    171     }
    172 }
    173 
    174 Status nextElem(Sqlist *l,ElemType e,ElemType *next_elem)
    175 {
    176     int i=1;
    177     while(i<=l->length&&l->base[i-1]!=e)
    178     {
    179         i++;
    180     }
    181     if(i==(l->length+1))
    182     {
    183         printf("
    元素%d不在线性表中
    ",e);
    184         return ERROR;
    185     }
    186     else
    187     {
    188         if(i==l->length)
    189         {
    190             printf("该元素没有后驱
    ");
    191             return ERROR;
    192         }
    193         else
    194         {
    195             *next_elem=l->base[i];
    196             return OK;
    197         }
    198     }
    199 }
    200 /*
    201 返回L中第1个与e满足关系compare()的数据元素的位序。
    202 若这样的数据元素不存在,则返回值为0。
    203 */
    204 int locateElem(Sqlist *l,ElemType e,bool (*compare)(ElemType e,ElemType a))
    205 {
    206     int i=0;
    207     while(i<=l->length&&!compare(e,l->base[i]))
    208         i++;
    209     if(i<=l->length)
    210         return i+1;
    211     else 
    212         return 0;
    213 }                                    
    214 
    215 bool comp(ElemType e,ElemType a){
    216     if(e==a)
    217         return TRUE;
    218     else
    219         return FALSE;
    220 }
    221 
    222 Status printLinklist(Sqlist l) //遍历输出线性表中的元素
    223 {
    224     
    225     for (int i = 0; i < l.length; i++){
    226         printf("
    第%d个数据为%d",i+1,(l.base)[i]);
    227     }
    228     
    229     return OK;
    230 }
    231 
    232 Status destroyList(Sqlist *l){//销毁线性表,释放空间
    233 
    234     if(l->base!=NULL)
    235     {
    236         free(l->base);
    237         l->length=0;
    238         l->listsize=0;
    239 
    240         return OK;
    241     }
    242     return ERROR;
    243 }
    244 
    245 Status clearList(Sqlist *l)//将线性表重置为长度为0的空表
    246 {    
    247     l->length=0;
    248     return OK;
    249 }
    250 
    251 bool isEmpty(Sqlist *l) //判断线性表是否是空表
    252 {
    253     if(l->length==0)
    254         return TRUE;
    255     else
    256         return FALSE;
    257 }
    258 
    259 void main()
    260 {
    261     Sqlist L;
    262     Sqlist La,Lb,Lc;
    263     Status s = 1;//s为程序运行状态 
    264     int i;
    265     ElemType theElem;
    266     ElemType prior_elem;
    267     ElemType next_elem;
    268     ElemType e;
    269 
    270     s = initSqlist(&L);  
    271     
    272     //合并3个线性表开始
    273     initSqlist(&La);
    274     initSqlist(&Lb);
    275     initSqlist(&Lc);
    276 
    277     int a[5]={1,5,8,12,20};
    278     int b[10]={6,13,35,56,120,288,390,510,700,1000};
    279 
    280     for(i=0;i<5;i++)
    281         ListInsert(&La,La.length+1,a[i]);
    282 
    283     for(i=0;i<10;i++)
    284         ListInsert(&Lb,Lb.length+1,b[i]);
    285 
    286     printLinklist(La);
    287     printf("
    ==================
    ");
    288     printLinklist(Lb);
    289     printf("
    ==================
    ");
    290 
    291     s=mergeList(&Lc,La,Lb);
    292     printLinklist(Lc);
    293     printf("
    ==================
    ");
    294     
    295     //合并3个线性表结束
    296 
    297     if(s) 
    298         s=assignSqlist(&L);
    299     
    300     printLinklist(L);
    301     printf("
    ==================
    ");
    302     printf("请输入要获得的元素的位序: ");
    303     scanf("%d",&i);
    304     s=GetElem(&L,i,&e);
    305     printf("
    线性表中位序为%d的元素为%d",i,e);
    306     printf("
    ==================
    ");
    307     printf("
    元素6在线性表表中的位序为%d",locateElem(&L,6,comp));
    308     /*printf("请输入您要插入的元素: ");
    309     scanf("%d", &theElem);
    310     printf("请输入您要插入元素的位置: ");
    311     scanf("%d", &i);
    312     ListInsert(&L, i, theElem);*/
    313     printf("
    ==================
    ");
    314     printf("
    您删除第一个元素后的线性表为
    ");
    315     listDelete(&L,1,&e);
    316     printLinklist(L);
    317     printf("
    您删除的这个元素为%d",e);
    318     printf("
    ==================
    ");
    319     //前驱开始
    320     priorElem(&L,1,&prior_elem);
    321     printf("
    ==================
    ");
    322     printf("元素1的前驱为%d",prior_elem);
    323     printf("
    ==================
    ");
    324     //前驱结束
    325     //后继开始
    326     nextElem(&L,9,&next_elem);
    327     printf("
    ==================
    ");
    328     printf("元素9的后继为%d",next_elem);
    329     printf("
    ==================
    ");
    330     //后继结束
    331     if(s)
    332     {
    333         clearList(&L);
    334     }
    335 
    336     printLinklist(L);
    337     system("pause");
    338 }
  • 相关阅读:
    【java编程】使用System.getProperty方法,如何配置JVM系统属性
    【java多线程】CountDownLatch
    【java多线程】ConcurrentLinkedHashMap
    【java编程】ServiceLoader使用看这一篇就够了
    【JVM】java对象
    【git】Git常用命令
    centos7安装配置mysql5.7
    第十三章 redis-cluster原理
    《mysql技术内幕 InnoDB存储引擎(第二版)》阅读笔记
    第二章 BIO与NIO
  • 原文地址:https://www.cnblogs.com/gangtiexia/p/4807573.html
Copyright © 2011-2022 走看看