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

 /*
*the sequence of the list
*author:JanneLee
*data:2013-10-26
*/ #include <stdio.h>
#include <stdlib.h>/*rand()*/
#include <time.h>/*time()*/
#ifdef DEBUG
#include <assert.h>
#endif
#define MAX_SIZE 1000
#define N 10
typedef struct Sqlist{
int data[MAX_SIZE];
int length;
}Sqlist;
int InsertElement(Sqlist *s,int pos,int val);
int DelElemByPos(Sqlist *s,int pos);
int DelElemByVal(Sqlist *s,int val);
int UpdateElemByPos(Sqlist *s,int pos,int val);
int FindElemByVal(Sqlist s,int val);
int ShowSqlist(Sqlist s);
int main(){
printf("====program begin========\n");
Sqlist s;
s.length=;
//InsertElement(&s,0,10);
//InsertElement(&s,1,20);
/*generate a sequence with random value*/
/*begin*/
int i=;
srand(time(NULL));
for(;i<N;i++){
InsertElement(&s,i,rand()%N);
}
/*end*/
//DelElemByPos(&s,3);
//DelElemByVal(&s,3);
UpdateElemByPos(&s,,);
printf("pos:%d\n",FindElemByVal(s,));
ShowSqlist(s);
printf("======program end======\n");
return ;
}
/*
*function name:InsertElement
*feature:insert a value in the position of pos
*parameters:*s-the sequence of the list,pos-the insert position,val-the value
*there if the c++ we can use &s,the c we shoud use star s to make the s change.
*return: if 1-insert succeed,if 0 the failed
*author:Jannelee
*data:2013-10-26
*/
int InsertElement(Sqlist *s,int const pos,int const val){
int ret_flag=;
if(pos>MAX_SIZE||pos<){
ret_flag=;
}else{
int i=;
for(i=pos;i<s->length;i++){
s->data[i+]=s->data[i];
}
s->data[pos]=val;
s->length++;
}
return ret_flag;
}
/*
*function name:ShowSqlist
*feature:show the sequence of the list
*parameters:s-the sequence
*return :1-succeed,0-error
*author:JanneLee
*date:2013-10-26
*/
int ShowSqlist(Sqlist const s){
int ret_flag=;
printf("=========show the Sqlist===========\n");
int i=;
for(;i<s.length;i++){
printf("%d ",s.data[i]);
}
printf("\n==========the show end=============\n");
return ret_flag;
}
/*
*function name:DelElemByPos
*feature:delete the element by the position of the element in the sequence.
*parameters:*s- the sequence ,pos-the position for delete
*return : if 1- succeed ,0-faild
*author:JanneLee
*date:2013-10-26
*/
int DelElemByPos(Sqlist *s,int pos){
int ret_flag=;
if(pos<||s->length<pos){
ret_flag=;
}else{
int i=;
for(;i<s->length;i++){
s->data[i]=s->data[i+];
}
s->length--;
}
return ret_flag;
}
/*
*function name:DelElemByVal
*feature:delete the elements by the value in the sequence
*this may delect every elements have the same value of val in the sequence.
*parameters:*s - the sequence , val- the value
*reutrn : 1- succeed ,0 -faild
*author:JanneLee
*date:2013-10-26
*/
int DelElemByVal(Sqlist *s ,int val){
int ret_flag=;
int i=,j=;
for(;i<s->length;i++){
if(s->data[i]==val){
for(j=i;j<s->length;j++){
s->data[j]=s->data[j+];
s->length--;
}
}else{
ret_flag=;
}
}
return ret_flag;
}
/*
*function name:UpdateElemByPos
*feature:update the value of the sequence int the position of pos
*parameters:*s - the sequence,pos-the position,val-the value
*return:-1-no this value,0-faild
*author:JanneLee
*date:2013-10-26
*/
int UpdateElemByPos(Sqlist *s,int pos,int val){
int ret_flag=;
if(pos<||pos>s->length){
ret_flag=;
}else{
s->data[pos]=val;
}
return ret_flag;
}
/*
*function name:FindElemByVal
*feature:get the vale of the position in the sequence,if the value has repeat in the
*sequence ,just find the first position in the sequence.
*parameters:s - the sequence,val-the value
*return:-1-no this value,otherwise ,the position of the value
*author:JanneLee
*date:2013-10-26
*/
int FindElemByVal(Sqlist s,int val){
printf("the sequence length:%d\n",s.length);
int ret_pos=-;
int i=;
for(;i<s.length;i++){
if(val==s.data[i]){
ret_pos=i+;
i=s.length+;
}
}
return ret_pos;
}
05-11 17:55
查看更多