#include <stdio.h>#include <stdlib.h>#define MAXLENGTH 1000typedef struct{ int total; int date[MAXLENGTH];}lineList;void showList(lineList* list){ int i; if(list->total ==0){ printf("空的線性表\n"); return; } printf("當前線性表狀態:"); for(i=0;i<list->total;i++){ printf("%d\t",list->date[i]); } printf("\n");}void outPut(lineList* list){ system("cls"); printf("-- 順序表 --\n"); printf("a、 - 添加節點 i、 - 插入節點\n"); printf("d、 - 刪除節點 e、 - 退出\n"); showList(list);}lineList* createList(){ lineList* list = (lineList*)malloc(sizeof(lineList)); list->total = 0; return list;}void appendNode(lineList* list,int val){ if(list->total < MAXLENGTH ){ list->date[list->total] = val; list->total++; }}void insertNode(lineList* list,int val ,int pos){ int i; if(pos<0 || pos>list->total){ printf("插入位置不正確"); } for(i=pos;i<list->total;i++){ list->date[i+1] = list->date[i]; } list->date[pos] = val; list->total += 1;}void deleteNode(lineList* list, int pos){ if(pos<0 || pos > list->total){ printf("所刪除的節點不存在"); }else{ int i; for(i=pos;i<list->total;i++) list->date[i] = list->date[i+1]; list->total--; }}void main(){ int key, pos; char act; lineList* myList = createList(); while(1){ outPut(myList); printf("請操作:"); act = getchar(); fflush(stdin); switch(act){ case 'a': printf("請輸入要添加的 KEY"); scanf("%d",&key); appendNode(myList,key); break; case 'i': printf("請輸入要插入的 KEY 和 POS"); scanf("%d %d",&key,&pos); insertNode(myList,key,pos); break; case 'd': printf("請輸入要刪除的 POS"); scanf("%d",&pos); deleteNode(myList,pos); break; case 'e': exit(0); outPut(myList); fflush(stdin); break; default: break; } }}
順序表 是把線性表中的所有元素按照其邏輯一次儲存到指定的一塊連續的儲存空間···