线性表顺序表

来源:互联网 发布:记账软件免费版 编辑:程序博客网 时间:2024/04/29 20:53

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//#include<free.h>
//#include<Define.h>
typedef int Stuts;
typedef int Elemt;

#define LIST_INIT_SIZE 100
#define sizenu 10
#define OVERFLOW -2
#define OK 1
#define ERROR 0

typedef struct{
  Elemt* elem;
  int length;
  int size;
  }Sqlist;

//建一个空的链表(包含一个数组,以及测定数组的元素个数length,还有分给数组的长度)
 Stuts init_kongbiao (Sqlist *l){

  l->elem=(Elemt*)malloc(LIST_INIT_SIZE*sizeof(Elemt));

  if(!l->elem){
   printf("init_list failure!!!\n");
   exit(OVERFLOW);
  }

  l->length=0;
  l->size=LIST_INIT_SIZE;

  printf("init_list successful!!!\n");
  return OK;
 }


//清空链表,每一次创建,都需要清空,切记
 Stuts clean_biao(Sqlist *l){
  if(l->elem!=NULL){
   free(l->elem);
   l->elem=NULL;
   l->length=0;
   l->size=0;
   printf("clean xianxingbiao successful!!!\n");
   }
  return OK;
 }
//判断链表是否为空,为空则为true,不空位faulse

 Stuts look_biaoEmpty(Sqlist L){

  if(L.elem==NULL) {printf("the list is empty !!\n");return OK;}
  else printf("the list isn't empty !!\n");
  return ERROR;
 }


 //查看链表数组的长度length
 Stuts look_biaolength(Sqlist L){

  if(L.elem!=NULL){  printf("the length is :%d\n",L.length); return OK;}
  else return ERROR; 
 
 }


 //用e返回链表中的第i个元素
 int back_i_elem(Sqlist L,int i,int *e){
  int t;
  if(L.elem==NULL){printf(".......................................................\n");}
  else if(i<1||i>100) {printf("your i is error!!\n");}
  if(L.elem[i-1]==NULL){printf("this number is null!\n"); return OK;}
  *e=L.elem[i-1];
  t=*e;
   //printf(" %d \n",t);

  return t;
 
 }

 //将n插入到链表中的所有位置
 Stuts inin_elem(Sqlist *L,int n){
  int i;
  if(L->elem==NULL) {printf(".................................");exit(OVERFLOW);}
 
  for(i=0;i<100;i++) L->elem[i]=n;
  L->length=i;
  printf("charu successful!!!!\n");
  return OK;
 }

 //删除第i个元素,即为null
 Stuts del_i_elem(Sqlist *L,int i){
  if(L->elem==NULL) {printf(".................................");exit(OVERFLOW);}
  L->elem[i-1]=NULL;
  printf("del successful!!\n");
  return OK;
 }

 //在链表中的第i个位置插入e
 Stuts in_i_elem(Sqlist *L,int i,int e){
  int *base,*p;
  int t;
  if(L->elem==NULL || i<1 || i>100 ) {printf(".................................");exit(OVERFLOW);}
  if(L->length>=L->size){
   base=(int*)realloc(L->elem,(LIST_INIT_SIZE+sizenu)*sizeof(int));
   if(!base) {printf("realloc failure !!!\n");exit(OVERFLOW);}
   L->elem=base;
   L->size+=sizenu; 
  }
  
 //q=&L->elem[i-1];
 p=&L->elem[L->length];
 for(t=0;t<(L->length-i+1);p--,t++){
 *(p+1)=*p;
 }
 *p=e;
 ++L->length;
 return OK;
 }

//返回链表中的第i个数的前驱值
 Stuts pre_i_elem(Sqlist L,int i,int *e){
 if(L.elem==NULL || i<1 || i>L.length) {printf(".................................");exit(OVERFLOW);}
 *e=L.elem[i-2];
 return OK;
 }


//根据自己的需要依次赋值算法如下:
 Stuts ned_IN_elem(Sqlist *L,int t){
 //int i;
 if(L->elem==NULL) {printf(".................................");return ERROR;}
 
 L->elem[L->length]=t;
 ++L->length;

 return OK;
 }

 //遍历所有链表中的值
 Stuts sear_ALL_elem(Sqlist *L){
  int i;
  if(L->elem==NULL) {printf(".................................");exit(OVERFLOW);}
  printf(".................................\n");
  for(i=0;i<L->length;i++){
  printf("the %d is %d \n",i+1,L->elem[i]);
  }
  
  return OK;
 }

 Stuts seek_e_elem(Sqlist l,int e){
 int i;
 if(l.elem==NULL) {printf(".................................");exit(OVERFLOW);}
 for(i=0;i<l.length;i++){
  if(l.elem[i]==e) {printf("find out i is :%d  ",i);return OK;}
 }
 
 if(i==l.length) return 0;
 }


//将表在lb中,不在la中的元素添加到la中
 Stuts union_la_lb(Sqlist *la,Sqlist lb){
 Stuts i,t;
 int e;
    //Sqlist lm;
 //lm=*la;


 for(i=1;i<=lb.length;i++){

//如果直接用la的话这里表示的是地址,和seek_e_elem(Sqlist l,int n)类型不兼容
 t=seek_e_elem(*la,back_i_elem(lb,i,&e));
//当在la中没有找到lb的一一返回元素,则执行下列操作:即插入la中
 if(t==0){
 la->elem[la->length++]=back_i_elem(lb,i,&e); 
 } 
 
 }
 
 return OK;
 }

 

 

//此算法有待考究
 Stuts add_la_lb_to_lc(Sqlist la,Sqlist lb,Sqlist *lc){

 int *i,*j;
 int n=1,m=1;

 i=la.elem;j=lb.elem;

 
 while(n<=la.length&&m<=lb.length){
 

  if(*i>*j){

   lc->elem[lc->length++]=*j;
   j++;
   m++;
   continue;  
   } 
   else if(*j>*i){
   lc->elem[lc->length++]=*i;
   i++;
   n++;
   continue;
      }
      else if(*i==*j){ 
       lc->elem[lc->length++]=*i;
       i++,j++;
       n++,m++;
       continue;
      }
 }


 while(n<=la.length){
 lc->elem[lc->length++]=la.elem[n-1];
 n++;
 }

 while(m<=lb.length){
 lc->elem[lc->length++]=lb.elem[m-1];
 m++;
 }

   return OK;
 
 }

//该主函数,主要是验证以上的各个函数

 

 
int main(){


int m[5];
int  q;
/////////////////////////////////////la和lb的相关操作////////////////////////////////////
Sqlist la,lb,lc;
init_kongbiao(&la);
init_kongbiao(&lb);
//将la和lb中的数据按递增的格式存入(la和lb也是递增存入)
init_kongbiao(&lc);

for(q=0;q<5;q++){

// if(!scanf("%d",&m[t])) {printf("..........................................\n");break;}

scanf("%d",&m[q]);
ned_IN_elem(&la,m[q]);

}

sear_ALL_elem(&la);

for(q=0;q<5;q++){

// if(!scanf("%d",&m[t])) {printf("..........................................\n");break;}

scanf("%d",&m[q]);printf(",,,,,,,,,,,,,,,,,,\n");
ned_IN_elem(&lb,m[q]);

}
sear_ALL_elem(&lb);


//向la中插入在lb中而不在la中的元素
//union_la_lb(&la,lb);

sear_ALL_elem(&la);

add_la_lb_to_lc(la,lb,&lc);

sear_ALL_elem(&lc);


clean_biao(&la);
clean_biao(&lb);
clean_biao(&lc);

/////////////////////////////////////la和lb的相关操作////////////////////////////////////
//插入空表
//init_kongbiao(&l);
//清除表,注意,在每次建立新表后都要进行清除
//clean_biao(&l);
//查看是否为空表,若为空表为OK(1),否则为EEROR(0)
//look_biaoEmpty(l);
//查看表的长度,若不为空,返回OK,且输出表的长度
//look_biaolength(l);
//用e返回l表中的第20个元素(在表中是从0开始计数)
//back_i_elem(l,20,e);
//将表l新建所有的空间全都插入5
//inin_elem(&l,5);
//用t返回l表中的第20个元素的值
//back_i_elem(l,20,&t);
//删除第20个元素
//del_i_elem(&l,20);
//返回第20个元素
//back_i_elem(l,20);
//在l中的第20个元素位置(在表中是19)插入e
//in_i_elem(&l,20,e);
//返回表中第e个元素
//back_i_elem(l,e);
//用t返回表中第e个元素的前驱
//pre_i_elem(l,e,&t);
return 0;

}

 

 

 

 

 


 

原创粉丝点击