//author:xiaopeng
//date:2010.9.9
//blog:http://chengxiaopeng.cublog.cn
#include <stdio.h> #define MAXSIZE 100 #define LINE_ITEM_COUNT 5 //打印元素时,一行显示的个数
#define PRINT_ITEM "%5d" #define SCANF_ITEM "%d" #define SIZE sizeof(SEQUENCELIST)
typedef int ELEMENT_TYPE; typedef struct //定义线性表结构
{ ELEMENT_TYPE datas[MAXSIZE]; int length; //线性表的长度
}SEQUENCELIST;
SEQUENCELIST * INITIATE(); //初始化线性表
ELEMENT_TYPE GET(SEQUENCELIST *,int); //获取指定索引的元素值,如果错误返回-9999
int LENGTH(SEQUENCELIST *);//获取线性表的长度
void PRINT(SEQUENCELIST *); //打印线性表中的数据
int INSERT(SEQUENCELIST *,ELEMENT_TYPE,int);//插入操作,成功返回1,失败返回0
int LOCATE(SEQUENCELIST *,ELEMENT_TYPE);//定位函数,如果元素存在于线性表中,则返回所在位置;否则返回-1
int DELETE(SEQUENCELIST *,int); //删除线性表中的第index元素,删除成功返回1;删除失败返回0
int EMPTY(SEQUENCELIST *); //判断线性表是否为空,如果为空返回0;不为空返回1;
void CLEAR(SEQUENCELIST *);//将线性表制空,不管当前的线性表是否为空,都将其线性表制空.
void MENU(); //菜单
void insert_mul_element(SEQUENCELIST *); //插入多个元素
void insert_sig_element(SEQUENCELIST *); void search_element(SEQUENCELIST *); void get_index_value(SEQUENCELIST *); //获取指定索引处的值
void delete_index(SEQUENCELIST *);//删除索引
void is_empty(SEQUENCELIST *); void seq_clear(SEQUENCELIST *); void print_info(SEQUENCELIST *); int main(int argc,char *argv[]) { SEQUENCELIST *seq = INITIATE(); int sel_menu_value; MENU(); do { printf("\tyour select:"); scanf("%d",&sel_menu_value); switch(sel_menu_value) { case -1: break; case 1: insert_mul_element(seq); break; case 2: insert_sig_element(seq); break; case 3: printf("\tthe length is : %d\n",LENGTH(seq)); break; case 4: search_element(seq); break; case 5: get_index_value(seq); break; case 6: delete_index(seq); break; case 7: is_empty(seq); break; case 8: seq_clear(seq); break; case 9: print_info(seq); break; case 10: MENU(); break; default: MENU(); break; } }while(-1 != sel_menu_value); system("pause"); return 0; }
SEQUENCELIST * INITIATE() { SEQUENCELIST *seq = (SEQUENCELIST *)malloc(SIZE); seq->length = 0; return seq; }
void PRINT(SEQUENCELIST * seq) { if (seq->length > MAXSIZE) //当线性表的长度大于最大长度时,进行报错处理
{ printf("error:the data is out!\n"); return; } int i; printf("the sequence table length is: %d\n",LENGTH(seq)); printf("the sequence table item is :\n"); for(i = 0; i < seq->length; i++) { if (LINE_ITEM_COUNT == i) { printf("\n"); } printf(PRINT_ITEM,seq->datas[i]); } printf("\n"); } int INSERT(SEQUENCELIST * seq,ELEMENT_TYPE data,int i) { int k; if (i < 0 || i > seq->length || seq->length > MAXSIZE) { return 0; } else { for (k = seq->length;k >= i;k--) { seq->datas[k] = seq->datas[k - 1]; } seq->datas[i] = data; seq->length += 1; return 1; } } int LENGTH(SEQUENCELIST * seq) { return seq->length; }
ELEMENT_TYPE GET(SEQUENCELIST * seq,int index) { int len = LENGTH(seq); if (index < 0 || index > len - 1 || len > MAXSIZE) { printf("\terror:your input index %d is error!\n",index); return -9999; } else { return seq->datas[index]; } }
int LOCATE(SEQUENCELIST * seq,ELEMENT_TYPE item) { int i,len,result = -1; len = LENGTH(seq); for (i =0 ; i < len; i++) { if (seq->datas[i] == item) { break; } } if (seq->datas[i] == item) { result = i; } return result; }
int DELETE(SEQUENCELIST * seq,int index) { int len,i; len = LENGTH(seq); if (index < 0 || index >= len || len > MAXSIZE) { return 0; } else { for (i = index;i < len; i++) { seq->datas[i] = seq->datas[i + 1]; } seq->length -= 1; return 1; } }
int EMPTY(SEQUENCELIST * seq) { int len = LENGTH(seq); return len > 0 ? 1:0; }
void CLEAR(SEQUENCELIST * seq) { seq->length = 0; }
void MENU() { printf("\n\tthe sequence table manage system\n\n"); printf("\t1\tsequence insert mul element\n"); printf("\t2\tinsert 1 element\n"); printf("\t3\tget lenth\n"); printf("\t4\tsearch element\n"); printf("\t5\tget element by index\n"); printf("\t6\tdelete element by index\n"); printf("\t7\tjudge empty\t\n"); printf("\t8\tclear\t\n"); printf("\t9\tprint info\t\n"); printf("\t10\treturn menu\t\n"); printf("\t-1\texit system\t\n"); }
void insert_mul_element(SEQUENCELIST * seq) { int i,count,len = LENGTH(seq); int max = MAXSIZE - len; ELEMENT_TYPE item; printf("\tyou want input number count is :"); scanf(SCANF_ITEM,&count); while (count < 0 && count <= max) { printf("\terror:your input number must > 0 and < %d\n",max); scanf(SCANF_ITEM,&count); } for (i = 0;i < count; i++) { printf("\tinput a number:"); scanf(SCANF_ITEM,&item); INSERT(seq,item,len++); } printf("\tinput data is success.\n"); }
void insert_sig_element(SEQUENCELIST * seq) { ELEMENT_TYPE item; int index,result; printf("\tplease input you want input data:"); scanf("%d",&item); printf("\tplase input insert index:"); scanf("%d",&index); result = INSERT(seq,item,index); if (result) { printf("\tinput data is success.\n"); } else { printf("\tinput data is error.\n"); } }
void search_element(SEQUENCELIST * seq) { ELEMENT_TYPE item; int result; printf("\tinput you seach element value:"); scanf(SCANF_ITEM,&item); result = result = LOCATE(seq,item); if (result >= 0) { printf("\tyou search "); printf(SCANF_ITEM); printf(" index is : %d\n",result); } else { printf("\tsorry:do search this item\n"); } }
void get_index_value(SEQUENCELIST * seq) { int index; printf("\tinput search index value:"); scanf("%d",&index); ELEMENT_TYPE item; item = GET(seq,index); if (-9999 != item ) //
{ printf("\tindex %d :",index); printf(SCANF_ITEM,item); printf("\n"); } }
void delete_index(SEQUENCELIST * seq) { int index,result; printf("\tdelete index:"); scanf("%d",&index); result = DELETE(seq,index); if (result) { printf("\tdelete index %d is success.\n",index); } else { printf("\terror:delete is fail.\n"); } }
void is_empty(SEQUENCELIST * seq) { int result; result = EMPTY(seq); if (result) { printf("\tsequence table not EMPTY\n"); } else { printf("\tsequence table is EMPTY\n"); } }
void seq_clear(SEQUENCELIST * seq) { CLEAR(seq); printf("\tclear is success.\n"); }
void print_info(SEQUENCELIST * seq) { int i,len = LENGTH(seq); printf("\tthe lenth is : %d\n",len); printf("\t"); for (i = 0 ;i < len;i++) { if (i % LINE_ITEM_COUNT == 0) { printf("\n\t"); } printf(PRINT_ITEM,seq->datas[i]); } printf("\n"); }
|