Data structure - linear order table

Data structure - linear order table

demand analysis

This procedure is a demonstration program sequence table, the basic object is to be familiar with the various arithmetic operations, various basics of the operation sequence table order table, and show the result of the basic operation.

type of data

typedef int Status;
typedef int ElemType;

typedef struct {
 ElemType *elem;
 int length;
 int listsize;
}SqList;

Realize the function

Initialization sequence table

Status Init_SqList(SqList *L){//初始化 
 L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L->elem){
     printf("空间分配不成功\n");
     printf("-----------------------------\n");
     return ERROR;
 }else{
  printf("线性表初始化成功\n"); 
  printf("-----------------------------\n");
 } 
 L->length=0;
 L->listsize=LIST_INIT_SIZE;
 return OK;
}

Input sequence table

Status Input_SqList(SqList *L){//输入 
 ElemType e;
 int i=0;
 printf("请输入初始顺序表(输入-32768时结束):"); 
 while(1){
  scanf("%d",&e);
  if(e==-32768) break;
  L->elem[i++]=e; 
 }
 L->length=i;
 return OK;
} 

Find Elemental order and return

int Locate_SqList(SqList L,ElemType e){//查找元素位序并返回 
  int j=1;
 while(j<=L.length&&L.elem[j-1]!=e)
  j++;
 if(L.elem[j-1]==e)
  return j;
 else
  return 0;
} 

Inserted in the bit sequence

Status Insert_Locate_SqList(SqList *L,int i,ElemType e){//按位序插入 
 if(i<1||i>L->length+1){
  printf("插入位序出错,插入不成功\n");
  printf("-----------------------------\n");
  return ERROR;
 } 
 if(L->length>=L->listsize){
  ElemType *newbase;
  newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
  if(!newbase){
   printf("空间分配不成功\n");
   printf("-----------------------------\n");
         return ERROR;
  }else printf("重新分配空间成功\n");
  L->elem=newbase;
  L->listsize+=LISTINCREMENT;
 }
 ElemType *p=&(L->elem[i-1]);
 ElemType *q;
 for(q=&(L->elem[L->length-1]);q>=p;q--){
  *(q+1)=*q;
 }
 *p=e;
 L->length++;
 printf("插入成功\n"); 
 printf("-----------------------------\n");
 return OK;
}

Prior to insertion e2 e1

Status Insert_Date_SqList(SqList *L,ElemType e1,ElemType e2){//e1之前插入e2 
 if(L->length>=L->listsize){
  ElemType *newbase;
  newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
  if(!newbase){
   printf("空间分配不成功\n");
   printf("-----------------------------\n");
         return ERROR;
  }else printf("重新分配空间成功\n");
  L->elem=newbase;
  L->listsize+=LISTINCREMENT;
 }
 int i=-1,j=0;
 for(;j<L->length;j++){
  if(L->elem[j]==e1){
   i=j;
   break;
  }
 }
 if(i==-1){
  printf("未找到数据,插入表尾\n");
  L->elem[L->length]=e2;
 } 
 else{
  for(j=L->length-1;j>=i;j--)
   L->elem[j+1]=L->elem[j];
  L->elem[i]=e2;
 }    
 L->length++;
 printf("插入成功\n");
 printf("-----------------------------\n");
 return OK;
}

Delete bit sequence

Status Delete_Locate_SqList(SqList *L,int i){//按位序删除 
 if(L->length==0){
  printf("线性表为空\n");
  printf("-----------------------------\n");
  return ERROR; 
 }
 if(i<1||i>L->length){
  printf("删除位序错误\n");
  printf("-----------------------------\n");
  return ERROR; 
 }
 int j;
 for(j=i-1;j<L->length-1;j++)
  L->elem[j]=L->elem[j+1];
 L->length--;
 return OK;
} 

Find data deletion

Status Delete_Date_SqList(SqList *L,ElemType e){//查找数据删除
 int i=-1,j=0;
 for(;j<L->length;j++){
  if(L->elem[j]==e){
   i=j;
   break;
  }
 }
 if(i==-1){
  printf("未找到数据\n");
  return ERROR;
 } 
 else{
  for(;i<L->length-1;i++)
   L->elem[i]=L->elem[i+1];
  L->length--;
  
 }
 return OK;
}

Delete all duplicate elements

Existing function calls

Status Delete_AllDate_SqList(SqList *L,ElemType e){//删除所有重复元素 
 int count=0,i=0,j=0;
 while(i<L->length){
  if(L->elem[i]==e){
   Delete_Locate_SqList(L,i+1);
   count++;
   i--; 
  }
  i++;
 }
 if(count==0) printf("未找到元素\n");
 else{
  printf("删除成功\n");
  printf("-----------------------------\n");
 }
 return OK;
}

Delete

By covering the data to be deleted delete

Status Delete_AllDate_SqList(SqList *L,ElemType e){//删除所有重复元素 
 int count=0,i=0,j=0;
 for(i=0;i<L->length;i++){
  if(L->elem[i]==e){
   count++;
  }else{
   L->elem[i-count]=L->elem[i];
  }
 } 
 if(count==0) printf("未找到元素\n");
 else{
  L->length-=count;
  printf("删除成功\n");
  printf("-----------------------------\n");
 }
 return OK;
}

Display linear form

Status Display_SqList(SqList L){//显示线性表
    if(L.length==0) printf("线性表为空\n");
    ElemType *p=&(L.elem[0]),*q=&(L.elem[L.length-1]);
 for(;p<=q;p++) printf("%d\t",*p);
 printf("\n");
 printf("-----------------------------\n");
 return OK;
}
Published 22 original articles · won praise 1 · views 1058

Guess you like

Origin blog.csdn.net/Doro_/article/details/103996992