線性表的輸入輸出插入刪除查詢(順序表示)
阿新 • • 發佈:2019-02-02
#include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define ok 1 #define error -1 #define overflow -2 #define ElemType int typedef struct { ElemType *elem; int length; int listsize; int Status; }SqList; int InitList_Sq(SqList &L) { L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L.elem) exit(overflow); L.length=0; L.listsize=LIST_INIT_SIZE; return ok; } int CreateList_Sq(SqList &L) { int i,e; scanf("%d",&e); for (i=0;i<e;i++) { scanf ("%d",&L.elem[i]); L.length=L.length+1; } return 1; } int PrintList_Sq(SqList &L){ int i; for (i=0; i < L.length ; i++) { printf ("%d\n",L.elem[i]); } return 1; } int ListInsert_Sq(SqList &L,int i,ElemType e) { int *q,*p,*newbase; if(i<1||i>L.length+1) return error; if(L.length>=L.listsize) { newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(overflow); L.elem=newbase; L.listsize+=LISTINCREMENT; } 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 ListDelete_Sq(SqList &L,int i) { int *p,*q; if((i<1)||(i>L.length)) return error; p=&(L.elem[i-1]); q=L.elem+L.length-1; for(++p;p<=q;++p) *(p-1)=*p; --L.length; return ok; } int LocateElem_Sq(SqList &L,ElemType e){ printf("%d",L.elem[e-1]); return 0; } void main() { SqList pt; int a,b,c; InitList_Sq(pt); CreateList_Sq(pt); PrintList_Sq(pt); printf("\n"); scanf("%d,%d",&a,&b); ListInsert_Sq(pt,a,b); PrintList_Sq(pt); printf("\n"); scanf("%d",&a); ListDelete_Sq(pt,a); PrintList_Sq(pt); scanf("%d",&c); LocateElem_Sq(pt,c); printf("\n"); }