首页 > 代码库 > 线性表插入、删除实现

线性表插入、删除实现

  1 ////////////////////////////////////////////  2 // sequencelist.cpp  3 //  4 // author:Leetao  5 ////////////////////////////////////////////  6 // 简介:  7 //   线性表的实现  8 ////////////////////////////////////////////  9 /** 10 * 线性表初始化,插入,删除功能的实现 11 * 12 * @author Leetao 13 * @version 1.0 14 * @date 2014.11.18   15 */  16 #include<stdio.h> 17 #include<malloc.h> 18 #define LIST_INIT_SIZE 100  //线性表储存空间的初始分配量  19 #define LISTINCREMENT 10    //线性表储存空间的分配增量  20  21 //类型宏定义,定义为int型  22 typedef int Status; 23 typedef int Elem; 24  25 typedef struct 26 { 27     Elem *elem; 28     int length; 29     int listsize; 30      31 }SqList; 32  33 //线性表初始化  34 Status InitList_Sq(SqList &list) 35 { 36     int i,n; 37     printf("Input the length of the sequence list:\n"); 38     scanf("%d",&n); 39      40      41     list.elem=(Elem *)malloc(LIST_INIT_SIZE*sizeof(Elem)); 42     if(list.elem==NULL) 43     { 44         printf("error!\n"); 45     } 46      47     list.length=0; 48     list.listsize=LIST_INIT_SIZE; 49      50     printf("Input the data of the sequence list:\n"); 51      52     for(i=0;    i<n;    i++) 53     { 54         scanf("%d",list.elem+i*sizeof(Elem)); 55         list.length++; 56     } 57      58     return 0; 59 } 60  61 //线性表插入  62 Status ListInsert_Sq(SqList &list,int i,Elem e) 63 { 64     Elem *newbase; 65     int j; 66     printf("now we are goint to insert %d at list.elem[%d]\n",e,i-1); 67     if(i<1||i>(list.length+1)) 68     { 69         printf("illegal index!\n");  70     } 71      72     if(list.length>=list.listsize) 73     { 74         newbase=(Elem *)realloc(list.elem, 75          (list.listsize+LISTINCREMENT)*sizeof(Elem)); 76           77          if(newbase==NULL) 78          { 79              printf("error!\n"); 80          } 81           82          list.elem=newbase; 83          list.listsize+=LISTINCREMENT; 84     } 85      86     for(j=list.length-1;    j>=i-1;        j--) 87     { 88      89         *(list.elem+(j+1)*sizeof(Elem))=*(list.elem+j*sizeof(Elem)); 90          91          92     }    *(list.elem+(i-1)*sizeof(Elem))=e; 93         list.length++;     94          95  96     return 0; 97 }  98  99 //线性表删除100 Status ListDelete_Sq(SqList &list,int i)101 {102     int j;103     Elem e;104     105     if(i<1||i>list.length)106     {107         printf("Invalid index!\n");108     }    109      110      e=*(list.elem+(i-1)*sizeof(Elem)); //删除的元素赋值给e 111     for(j=i-1;    j<list.length;    j++)112     {113         *(list.elem+j*sizeof(Elem))=*(list.elem+(j+1)*sizeof(Elem));114     }115         --list.length;116 } 117 //线性表打印118 Status PrintList_Sq(SqList &list)119 {120     int i;121     printf("the data of the sequence is :\n\t");122     123     for(i=0;    i<list.length;    i++)124     {125         printf("%d->",*(list.elem+i*sizeof(Elem)));126     }127     128     printf("NULL\n");129     130     return 0;131 } 132 133 int main()134 {    135     int in,di;136     Elem ie;137     138     SqList list;139     InitList_Sq(list);140     PrintList_Sq(list);141     142         //插入143         printf("Input the position and data you want to insert:\n");144         scanf("%d%d",&in,&ie); 145         ListInsert_Sq(list,in,ie);146         PrintList_Sq(list);147     148         //删除 149         printf("Input the position  you want to delete:\n");150         scanf("%d",&di); 151         ListDelete_Sq(list,di);152         PrintList_Sq(list);153     154     return 0;155 }

插入、删除

线性表插入、删除实现