C-线性顺序表的增删改查

  闲来无事,练练手,写点C代码,对于线性表的简单操作。编辑工具Notpad++,编译工具tcc.

  1 /*

  2 *the sequence of the list

  3 *author:JanneLee

  4 *data:2013-10-26

  5 */

  6 

  7 #include <stdio.h>

  8 #include <stdlib.h>/*rand()*/

  9 #include <time.h>/*time()*/

 10 #ifdef DEBUG

 11 #include <assert.h>

 12 #endif

 13 #define MAX_SIZE 1000

 14 #define N 10

 15 typedef struct Sqlist{

 16     int data[MAX_SIZE];

 17     int length;

 18 }Sqlist;

 19 int InsertElement(Sqlist *s,int pos,int val);

 20 int DelElemByPos(Sqlist *s,int pos);

 21 int DelElemByVal(Sqlist *s,int val);

 22 int UpdateElemByPos(Sqlist *s,int pos,int val);

 23 int FindElemByVal(Sqlist s,int val);

 24 int ShowSqlist(Sqlist s);

 25 int main(){

 26     printf("====program begin========\n");

 27     Sqlist s;

 28     s.length=0;

 29     //InsertElement(&s,0,10);

 30     //InsertElement(&s,1,20);

 31     /*generate a sequence with random value*/

 32     /*begin*/

 33     int i=0;

 34     srand(time(NULL));

 35     for(;i<N;i++){

 36         InsertElement(&s,i,rand()%N);

 37     }

 38     /*end*/

 39     //DelElemByPos(&s,3);

 40     //DelElemByVal(&s,3);

 41     UpdateElemByPos(&s,3,3);

 42     printf("pos:%d\n",FindElemByVal(s,3));

 43     ShowSqlist(s);

 44     printf("======program end======\n");

 45     return 0;

 46 }

 47 /*

 48 *function name:InsertElement

 49 *feature:insert a value in the position of pos

 50 *parameters:*s-the sequence of the list,pos-the insert position,val-the value

 51 *there if the c++ we can use &s,the c we shoud use star s to make the s change.

 52 *return: if 1-insert succeed,if 0 the failed

 53 *author:Jannelee

 54 *data:2013-10-26

 55 */

 56 int InsertElement(Sqlist *s,int const pos,int const val){

 57     int ret_flag=1;

 58     if(pos>MAX_SIZE||pos<0){

 59         ret_flag=0;

 60     }else{

 61         int i=0; 

 62         for(i=pos;i<s->length;i++){

 63             s->data[i+1]=s->data[i];

 64         }

 65         s->data[pos]=val;

 66         s->length++;

 67     }

 68     return ret_flag;

 69 }

 70 /*

 71 *function name:ShowSqlist

 72 *feature:show the sequence of the list

 73 *parameters:s-the sequence

 74 *return :1-succeed,0-error

 75 *author:JanneLee

 76 *date:2013-10-26

 77 */

 78 int ShowSqlist(Sqlist const s){

 79     int ret_flag=1;

 80     printf("=========show the Sqlist===========\n");

 81     int i=0;

 82     for(;i<s.length;i++){

 83         printf("%d ",s.data[i]);

 84     }

 85     printf("\n==========the show end=============\n");

 86     return ret_flag;

 87 }

 88 /*

 89 *function name:DelElemByPos

 90 *feature:delete the element by the position of the element in the sequence.

 91 *parameters:*s- the sequence ,pos-the position for delete

 92 *return : if 1- succeed ,0-faild

 93 *author:JanneLee

 94 *date:2013-10-26

 95 */

 96 int DelElemByPos(Sqlist *s,int pos){

 97     int ret_flag=1;

 98     if(pos<0||s->length<pos){

 99         ret_flag=0;

100     }else{

101         int i=0;

102         for(;i<s->length;i++){

103             s->data[i]=s->data[i+1];

104         }

105         s->length--;

106     }

107     return ret_flag;

108 }

109 /*

110 *function name:DelElemByVal

111 *feature:delete the elements by the value in the sequence

112 *this may delect every elements have the same value of val in the sequence.

113 *parameters:*s - the sequence , val- the value

114 *reutrn : 1- succeed ,0 -faild

115 *author:JanneLee

116 *date:2013-10-26

117 */

118 int DelElemByVal(Sqlist *s ,int val){

119     int ret_flag=1;

120     int i=0,j=0;

121     for(;i<s->length;i++){

122         if(s->data[i]==val){

123             for(j=i;j<s->length;j++){

124                 s->data[j]=s->data[j+1];

125                 s->length--;

126             }

127         }else{

128             ret_flag=0;

129         }

130     }

131     return ret_flag;

132 }

133 /*

134 *function name:UpdateElemByPos

135 *feature:update the value of the sequence int the position of pos

136 *parameters:*s - the sequence,pos-the position,val-the value

137 *return:-1-no this value,0-faild

138 *author:JanneLee

139 *date:2013-10-26

140 */

141 int UpdateElemByPos(Sqlist *s,int pos,int val){

142     int ret_flag=1;

143     if(pos<0||pos>s->length){

144         ret_flag=0;

145     }else{

146         s->data[pos]=val;

147     }

148     return ret_flag;

149 }

150 /*

151 *function name:FindElemByVal

152 *feature:get the vale of the position in the sequence,if the value has repeat in the 

153 *sequence ,just find the first position in the sequence.

154 *parameters:s - the sequence,val-the value

155 *return:-1-no this value,otherwise ,the position of the value

156 *author:JanneLee

157 *date:2013-10-26

158 */

159 int FindElemByVal(Sqlist s,int val){

160     printf("the sequence length:%d\n",s.length);

161     int ret_pos=-1;

162     int i=0;

163     for(;i<s.length;i++){

164         if(val==s.data[i]){

165             ret_pos=i+1;

166             i=s.length+1;

167         }

168     }

169     return ret_pos;

170 }

 

你可能感兴趣的:(增删改查)