1. 程式人生 > >順序表代碼

順序表代碼

list() str 輸出 scan 功能 ear style emp rep

#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"); } }

順序表代碼