顺序表代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define OK 1
#define ERROR 0
#define MAX 1024        //顺序表最大长度
#define delay 2         //延迟两秒

typedef int ElemType;
typedef int STATUS;
typedef struct{
  ElemType data[MAX];   //数据
  int last;             //长度
} List;
 

/************************
          创建
输入:无
输出:顺序表指针
功能:初始化顺序表
************************/
List * createList(){
  List * list = (List *)malloc(sizeof(List));
  list->last = 0;
  return list;
}

/************************
          清空
输入:顺序表指针
输出:无
功能:清空顺序表
************************/
void clearList(List * list){
  list->last = 0;
}

/************************
          追加
输入:顺序表指针,追加值
输出:状态码
功能:追加数据
************************/
STATUS appendList(List *list,ElemType val){

  if(list->last >= MAX){
    printf("list length is overflow the MAX length\n");
    return ERROR;
  }

  list->data[list->last]=val;
  list->last++;

  return OK;
}

/************************
          插入
输入:顺序表指针,插入位,插入值
输出:状态码
功能:插入数据
************************/
STATUS insertList(List *list,int index,ElemType val){

  index--;    //index从1开始,而处理是从0开始,后面亦同

  if(index<0 || index>list->last){
    printf("index is invalid\n");
    return ERROR;
  }

  int i;
  for(i=list->last;i>index;i--)
     list->data[i]=list->data[i-1];
   
  list->data[index] = val;    
  list->last++;
   return OK;
}

/************************
          替换
输入:顺序表指针,替换位,替换值
输出:状态码
功能:替换数据
************************/
STATUS replaceList(List *list,int index,ElemType val){
   
   index--;  
  
   if(list->last==0){
     printf("list is empty!!\n");
     return ERROR;
   }
 
   if(index<0 || index > list->last){
     printf("input index is invaild\n");
     return ERROR;
   }

   list->data[index]=val;
   return OK;
}

/************************
          删除
输入:顺序表指针,删除位,存储指针
输出:状态码
功能:删除数据
************************/
STATUS deleteList(List *list,int index,ElemType * docker){
  
  index--; 
 
  if(list->last==0){
    printf("no element can be deleted\n");
    return ERROR;
  };

  if(index<0 || index>list->last){
    printf("index is invalid\n");
    return ERROR;
  }

  *docker = list->data[index];  

  int i;
  for(i=index;i<(list->last-1);i++)
     list->data[i]=list->data[i+1];
  
  list->last--;
  return OK;
}

/************************
          获取值
输入:顺序表指针,位置,存储指针
输出:状态码
功能:获取值
************************/
STATUS getElement(List *list,int index,ElemType * docker){
  
  index--;
  
  if(index<0 || index>list->last){
     printf("index is invalid\n");
     return ERROR;
  }
  
  *docker = list->data[index];
  return OK;
}

/************************
         获取索引
输入:顺序表指针,开始位置,查询值
输出:状态码
功能:获取索引
************************/
STATUS getIndex(List *list,int beginIndex,ElemType value){

  beginIndex--;    //because array begin num is 0,but list begin num is 1;

  if(beginIndex >=list->last || beginIndex<0){
     printf("beginIndex is invalid\n");
     return ERROR;
  }

  int i=beginIndex;
  while(i<list->last){
    if(list->data[i]==value)return ++i;
    i++;
  }

  return OK;
}

/************************
         打印列表
输入:顺序表指针
输出:状态码
功能:将数据一一打印出来
************************/
STATUS printList(List *list){
  
  if(list->last == 0){
     printf("List is empty\n"); 
     return ERROR;
  }
 
  int i;
  for(i=0;i<list->last;i++){
    printf("[%d] ",list->data[i]);
    if((i+1)%10==0)printf("\n");
  }
   
  printf("\n");
  return OK;
}

/************************
          打印信息
输入:无
输出:无
功能:将操作信息打印出来
************************/
void printInfo(void){
  system("clear");
  printf("welcome to use sequence list\n");
  printf("----------------------------\n");
  printf("a = append    d = delete\n");
  printf("i = insert    r = replace\n");
  printf("g = getValue  G = getIndex\n");
  printf("c = clear     e = exit\n");
  printf("=========== list ===========\n");
}

int main(){
  ElemType val;
  int index;
  char select;
  List * list = createList();


  while(1){
     printInfo();
     printList(list);
     printf("please enter your choose:"); 
     select = getchar();
   
     if(select == 'a'){
        printf("please enter value:");
        scanf("%d",&val);
        appendList(list,val);
     }
  
     if(select == 'c'){
        clearList(list);
     }

     else if(select == 'i'){
       printf("please enter index:");
       scanf("%d",&index);
       printf("please enter value:");
       scanf("%d",&val);
       insertList(list,index,val);
     }

     else if(select == 'd'){
       printf("please enter index:");
       scanf("%d",&index);
       deleteList(list,index,&val);
       printf("last delete value: [%d]\n",val);
       sleep(delay);
     }
 
     else if(select == 'r'){
       int tmp;
       printf("please enter index:");
       scanf("%d",&index);
       printf("please enter new value:");
       scanf("%d",&val);
       replaceList(list,index,val);
     }

     else if(select == 'g'){
       printf("please enter index:");
       scanf("%d",&index);
       getElement(list,index,&val);
       printf("list[%d] = %d\n",index,val);
       sleep(delay);
     }

     else if(select == 'G'){
       printf("please enter beginIndex:");
       scanf("%d",&index);
       printf("please enter value:");
       scanf("%d",&val);
       int num = getIndex(list,index,val);
       if(num)
         printf("%d's index is %d\n",val,num);
       else
         printf("sorry not find\n");
       sleep(delay);
     }

     else if(select == 'e'){
       return 0;
     }
     printf("\n");
  } 
}

 

 

posted @ 2017-07-05 00:27  Khazix  阅读(468)  评论(0编辑  收藏  举报