顺序表的具体实现

来源:互联网 发布:陕西官员被网络大V攻击 编辑:程序博客网 时间:2024/05/19 00:40
  1. //线性顺序表  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #define LIST_INIT_SIZE 1000 //线性表存储空间的初始分配量  
  5. #define LISTINCRESEMENT 100 //线性表存储空间的分配增量  
  6. #define OK 1  
  7. #define ERROR 0  
  8. #define OVERFLOW -2  
  9. typedef int elemType;//元素类型  
  10. typedef struct  
  11. {  
  12.     elemType *List;//线性表首地址  
  13.     int length;//当前的长度  
  14.     int listsize;//当前分配的存储容量,以elemType为单位  
  15. } SqList;  
  16.   
  17. void AgainMalloc(SqList *L)//空间不够时重新分配空间的函数  
  18. {  
  19.     elemType *newbase;//分配一个临时基址  
  20.     newbase=(elemType *)realloc(L->List,(L->listsize+LISTINCRESEMENT)*sizeof(elemType));  
  21.     if(!newbase) exit(OVERFLOW);  
  22.     L->List=newbase;  
  23.     L->listsize+=LISTINCRESEMENT;  
  24. }  
  25.   
  26. //初始化一个空的线性表  
  27. int InitList_Sq(SqList *L)  
  28. {  
  29.     L->List=(elemType *)malloc(LIST_INIT_SIZE*sizeof(elemType));  
  30.     if(!L->List)exit(OVERFLOW);//overflow  
  31.     L->length=0;//初始表为空表  
  32.     L->listsize=LIST_INIT_SIZE;//初始表的存储容量,为LIST_INIT_SIZE个elemType单位  
  33.     return OK;  
  34. }  
  35. //求表中元素的个数  
  36. int ListLength(SqList *L)  
  37. {  
  38.     return L->length;  
  39. }  
  40.   
  41. //遍历顺序表  
  42. void TraverseList(SqList *L)  
  43. {  
  44.     int i;  
  45.     for(i=0; i<L->length; i++)  
  46.     {  
  47.         printf("%d ",L->List[i]);  
  48.     }  
  49.     printf("\n");  
  50.     return;  
  51. }  
  52. //向表头插入元素  
  53. void InsertFirst(SqList *L,elemType e)  
  54. {  
  55.     int i;  
  56.     if(L->length>=L->listsize)  
  57.         AgainMalloc(L);  
  58.     for(i=L->length-1; i>=0; i--)  
  59.         L->List[i+1]=L->List[i];  
  60.     L->List[0]=e;  
  61.     L->length++;  
  62.     return;  
  63. }  
  64.   
  65. //向表尾插入元素  
  66. void InsertLast(SqList *L,elemType e)  
  67. {  
  68.   
  69.     if(L->length>=L->listsize)  
  70.         AgainMalloc(L);  
  71.     L->List[L->length]=e;  
  72.     L->length++;  
  73.     return;  
  74. }  
  75. //在表中第pos个位置之前插入新元素e  
  76. int Insert_Sq(SqList *L,elemType e,int pos)  
  77. {  
  78.     int i;  
  79.     if(pos<1||pos>L->length+1) return ERROR;  
  80.     if(L->length>=L->listsize)//存储空间不够,要分配新的空间  
  81.         AgainMalloc(L);  
  82.     for(i=L->length-1; i>=pos-1; i--)  
  83.         L->List[i+1]=L->List[i];  
  84.     L->List[pos-1]=e;  
  85.     L->length++;  
  86.     return OK;  
  87. }  
  88. //查找给出元素的位置,若存在,给出位置(从1开始算);若不存在,返回-1  
  89. void Search(SqList *L,elemType e)  
  90. {  
  91.     int i;  
  92.     for(i=0; i<L->length; i++)  
  93.     {  
  94.         if(L->List[i]==e)  
  95.         {  
  96.             printf("找到,%d在第%d个位置\n",e,i+1);  
  97.             return;  
  98.         }  
  99.     }  
  100.     printf("没找到\n");  
  101.     return;  
  102. }  
  103. //删除第pos个元素,并返回其值  
  104. elemType DeleteElem(SqList *L,int pos)  
  105. {  
  106.     int i;  
  107.     elemType temp;  
  108.     if(pos<1||pos>L->length)  
  109.     {  
  110.         printf("pos值越界\n");  
  111.         exit(1);  
  112.     }  
  113.     temp=L->List[pos-1];  
  114.     for(i=pos; i<L->length; i++)  
  115.         L->List[i-1]=L->List[i];  
  116.     L->length--;  
  117.     return temp;  
  118. }  
  119. //判断线性表是否为空,为空返回1,不为空返回0  
  120. int isEmpty(SqList *L)  
  121. {  
  122.     if(L->length==0)  
  123.         return 1;  
  124.     else  
  125.         return 0;  
  126. }  
  127.   
  128. //顺序表的逆置  
  129. void Inverse(SqList *L)  
  130. {  
  131.     int low=0,high=L->length-1;  
  132.     elemType temp;  
  133.     int i;  
  134.     for(i=0; i<L->length/2; i++)  
  135.     {  
  136.         temp=L->List[low];  
  137.         L->List[low++]=L->List[high];  
  138.         L->List[high--]=temp;  
  139.     }  
  140. }  
  141.   
  142. void MergeList(SqList *La,SqList *Lb,SqList *Lc)  
  143. {  
  144.     //elemType *pa=La->List;elemType *pb=Lb->List;  
  145.     Lc->listsize=Lc->length=La->length+Lb->length;  
  146.     Lc->List=(elemType *)malloc(sizeof(elemType));  
  147.     if(!Lc->List) exit(OVERFLOW);  
  148.     int i=0,j=0,k=0;  
  149.     while(i<La->length&&j<Lb->length)  
  150.     {  
  151.         if(La->List[i]<=Lb->List[j])  
  152.         {  
  153.             Lc->List[k++]=La->List[i++];  
  154.         }  
  155.         else  
  156.         {  
  157.             Lc->List[k++]=Lb->List[j++];  
  158.         }  
  159.     }  
  160.     while(i<La->length)  
  161.     {  
  162.         Lc->List[k++]=La->List[i++];  
  163.     }  
  164.     while(j<Lb->length)  
  165.     {  
  166.         Lc->List[k++]=Lb->List[j++];  
  167.     }  
  168. }  
  169. int main()  
  170. {  
  171.     SqList list1;  
  172.     InitList_Sq(&list1);  
  173.     int length;  
  174.     scanf("%d",&length);  
  175.     int i;  
  176.     elemType temp;  
  177.     for(i=0; i<length; i++)  
  178.     {  
  179.         scanf("%d",&temp);  
  180.         InsertLast(&list1,temp);  
  181.     }  
  182.     printf("创建好的线性表La=");  
  183.     TraverseList(&list1);//创建好的顺序表  
  184.     int pos;  
  185.     scanf("%d%d",&temp,&pos);  
  186.     Insert_Sq(&list1,temp,pos);  
  187.     printf("插入一个元素后的线性表La=");  
  188.     TraverseList(&list1);//插入一个数字后的线性表  
  189.     scanf("%d",&pos);  
  190.     DeleteElem(&list1,pos);  
  191.     printf("删除一个元素后的线性表La=");  
  192.     TraverseList(&list1);  
  193.     scanf("%d",&temp);  
  194.     Search(&list1,temp);//查找元素  
  195.     printf("逆置后的线性表La=");  
  196.     Inverse(&list1);  
  197.     TraverseList(&list1);  
  198.     SqList list2;  
  199.     InitList_Sq(&list2);  
  200.     scanf("%d",&length);  
  201.     for(i=0; i<length; i++)  
  202.     {  
  203.         scanf("%d",&temp);  
  204.         InsertLast(&list2,temp);  
  205.     }  
  206.   
  207.     SqList list3;  
  208.     MergeList(&list1,&list2,&list3);  
  209.     printf("合并La和Lb后的线性表=");  
  210.     TraverseList(&list3);  
  211.     return 0;  
  212. }  
  213. 原博:http://blog.csdn.net/mpbchina/article/details/7384283

原创粉丝点击