資料結構-C語言實現順序表
阿新 • • 發佈:2018-12-21
#include<stdio.h> #include<stdlib.h> #define ERROR 0 #define LIST_INIT_SIZE 100 #define LISTINCREAMENT 10 #define OK 1 #define OVERFLOW -1 #define TURE 1 #define FALSE 0 typedef int Status; typedef int ElemType; typedef struct{ ElemType *elem; int length; int listsize; }Sqlist; //初始化 Status InitList_Sq(Sqlist *L) { L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(L->elem==0) exit(OVERFLOW); L->length=0; L->listsize=LIST_INIT_SIZE; return OK; } //獲取 Status Getelem(Sqlist L,int i,int *e) { if(i<1||i>L.length)return FALSE; *e=L.elem[i-1]; return OK; } //清空 void ClearList(Sqlist *L) { L->length=0; } //判空 Status ListEmpty(Sqlist L) { return L.length==0?TURE:FALSE; } //長度 int ListLength(Sqlist L) { return L.length; } //錄入資料 void inputData(Sqlist *L) { int n,i; printf("input n:\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("No.%d:",(i+1)); scanf("%d",&L->elem[i]); } L->length=n; } //輸出 void output(Sqlist L) { int i; for(i=0;i<L.length;i++) printf("%d ",L.elem[i]); } //摧毀 Status DestroyList(Sqlist *L) { free(L->elem); L->length=0; L->listsize=0; return OK; } //插入 Status InsertList(Sqlist *L,int i,int e) { int *newbase,*q,*p;; if(i<1||i>L->length+1) return FALSE; if(L->length>=L->listsize) { newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREAMENT)*sizeof(ElemType)); if(newbase==0) exit(OVERFLOW); L->elem=newbase; L->listsize+=LISTINCREAMENT; } q=&(L->elem[i-1]); for(p=&(L->elem[L->length-1]);p>=q;p--) { *(p+1)=*p; } *q=e; ++L->length; return OK; } //查詢 int LocateElem_Sq(Sqlist L,ElemType e) { int i; int *p; i=1; p=L.elem; while(i<=L.length&&*p!=e) { i++; p++; } if(i<=L.length) return i; else return 0; } //刪除 Status ListDeletd_Sq(Sqlist *L,int i,ElemType *e) { int *p; int *q; if((i<1)||(i>L->length)) return ERROR; p=&L->elem[i-1]; *e=*p; q=L->elem+L->length-1; for(p++;p<=q;p++) *(p-1)=*p; L->length--; return OK; } void main() { Sqlist L; int choice=1; InitList_Sq(&L); while(choice!=0) { system("cls"); printf("1.InitLIst\t2.ClearList\t3.LIstEmpty\n4.ListLength\t5.inputData\t6.output\n7.DestroyList\t8.Getelem\t9.InsertList\n10.FindList\t11.ListDeletd_Sq\n"); printf("\nplease input your choice:\n"); scanf("%d",&choice); switch(choice) { case 0:printf("byebye!\n"); break; case 1: if(InitList_Sq(&L)==OK) printf("success\n"); else printf("false\n"); break; case 2: printf("clear successfully\n"); break; case 3: if(ListEmpty(L)==TURE) printf("the list is empty\n"); else printf("the list is not empty\n"); break; case 4:{ int length; length=ListLength(L); printf("the listlength is %d\n",length); } break; case 5:inputData(&L); break; case 6:output(L); break; case 7:DestroyList(&L); break; case 8:{ int i; int e; printf("please input i(1--L.length):\n"); scanf("%d",&i); if(Getelem(L,i,&e)==OK) printf("NO.%d is %d\n",i,e); else printf("there is not a number\n"); break; } case 9: { int i,e; printf("please input i and e:\n"); scanf("%d %d",&i,&e); if(InsertList(&L,i,e)==OK) printf("insert successfully\n"); else printf("false\n"); break; case 10: { int e,pos; printf("please input you want number:\n"); scanf("%d",&e); pos=LocateElem_Sq(L,e); if(pos!=0) printf("found,its position is %d\n",pos); else printf("no found\n"); break; } case 11:{ int i; int e; printf("please input you want to delete No:"); scanf("%d",&i); if(ListDeletd_Sq(&L,i,&e)==OK) printf("success,you delete number is %d\n",e); else printf("fail\n"); break; } } } system("pause"); } }
效果圖如下: