線性表的建立————C語言資料結構學習
阿新 • • 發佈:2018-12-13
#include <stdio.h> #include <malloc.h> #define MaxSize 50 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int length; } SqList; void CreateList(SqList *&L, ElemType a[], int n);//用陣列建立線性表 void InitList(SqList *&L);//初始化線性表InitList(L) void DestroyList(SqList *&L);//銷燬線性表DestroyList(L) bool ListEmpty(SqList *L);//判定是否為空表ListEmpty(L) int ListLength(SqList *L);//求線性表的長度ListLength(L) void DispList(SqList *L);//輸出線性表DispList(L) bool GetElem(SqList *L,int i);//求某個資料元素值GetElem(L,i,e) int LocateElem(SqList *L, ElemType e);//按元素值查詢LocateElem(L,e) bool ListInsert(SqList *&L,int i,ElemType e);//插入資料元素ListInsert(L,i,e) bool ListDelete(SqList *&L,int i);//刪除資料元素ListDelete(L,i,e) //用陣列建立線性表 void CreateList(SqList *&L, ElemType a[], int n) //引用型指標 { int i; L=(SqList *)malloc(sizeof(SqList)); for (i==0;i<n;i++) L->data[i]=a[i]; L->length=n; } //初始化線性表InitList(L) void InitList(SqList *&L) //引用型指標 { L=(SqList *)malloc(sizeof(SqList)); //分配存放線性表的空間 L->length=0; } //銷燬線性表DestroyList(L) void DestroyList(SqList *&L) { free(L); } //判定是否為空表ListEmpty(L) bool ListEmpty(SqList *L) { return(L->length==0); } //求線性表的長度ListLength(L) int ListLength(SqList *L) { return(L->length); } //輸出線性表DispList(L) void DispList(SqList *L) { int i; if (ListEmpty(L)) return; for (i=0;i<L->length;i++) printf("%d",L->data[i]); printf("\n"); } //求某個資料元素值GetElem(L,i,e) bool GetElem(SqList *L,int i) { int e; if (i<1 || i>L->length) return false; e=L->data[i-1]; return true; } //按元素值查詢LocateElem(L,e) int LocateElem(SqList *L, ElemType e) { int i=0; while (i<L->length && L->data[i]!=e) i++; if (i>=L->length) return 0; else return i+1; } //插入資料元素ListInsert(L,i,e) bool ListInsert(SqList *&L,int i,ElemType e) { int j; if (i<1 || i>L->length+1) return false; //引數錯誤時返回false i--; //將順序表邏輯序號轉化為物理序號 for (j=L->length; j>i; j--) //將data[i..n]元素後移一個位置 L->data[j]=L->data[j-1]; L->data[i]=e; //插入元素e L->length++; //順序表長度增1 return true; //成功插入返回true } //刪除資料元素ListDelete(L,i,e) bool ListDelete(SqList *&L,int i) { int j,e; if (i<1 || i>L->length) //引數錯誤時返回false return false; i--; //將順序表邏輯序號轉化為物理序號 e=L->data[i]; for (j=i; j<L->length-1; j++) //將data[i..n-1]元素前移 L->data[j]=L->data[j+1]; L->length--; //順序表長度減1 return true; //成功刪除返回true } int main() { SqList *sq; ElemType a[6]= {5,8,7,2,4,9}; int i,x,choice; printf("第一步.建立線性表......\n"); CreateList(sq, a, 6); while(1){ printf("建立成功!請選擇選單執行相關操作\n"); printf(" 1.銷燬線性表\n"); printf(" 2.判定線性表是否為空\n"); printf(" 3.求線性表的長度\n"); printf(" 4.輸出線性表\n"); printf(" 5.求線性表第i個位置上的元素\n"); printf(" 6.查詢線性表中元素e的位置\n"); printf(" 7.線上性表第i個位置插入元素\n"); printf(" 8.刪除線性表第i個位置上的元素\n"); printf(" 0.退出\n"); printf(" 請輸入你的選擇:"); scanf("%d",&choice); switch(choice) { case 0: return 0; case 1: DestroyList(sq); printf("線性表銷燬成功~~程式直接退出!!"); return 0; case 2: if(ListEmpty(sq)) printf("線性表為空!"); else printf("線性表不為空!"); break; case 3: printf("該線性表長度為%d\n",ListLength(sq)); break; case 4: printf("輸出線性表:\n"); DispList(sq); break; case 5: printf("請輸入待查詢元素的位置:");scanf("%d",&i); printf("第%d個位置上的元素為%d",i,GetElem(sq,i)); break; case 6: printf("請輸入要查詢的元素:");scanf("%d",&x); printf("%d元素線上性表的第%d個位置\n",x,LocateElem(sq,x)); break; case 7: printf("請輸入要插入位置和值(空格隔開):");scanf("%d%d",&i,&x); ListInsert(sq,i,x); break; case 8: printf("請輸入要刪除的位置");scanf("%d",&i); ListDelete(sq,i); break; } printf("按任意鍵繼續……"); getchar();getchar(); } return 0; }