嚴蔚敏數據結構C語言版——線性表(初始化、插入、刪除、遍歷)


  1 #include<stdio.h>
  2 #include<malloc.h>
  3 #include<stdlib.h>
  4 
  5 #define TRUE 1
  6 #define FALSE 0
  7 #define OK 1
  8 #define ERROR 0
  9 #define INFEASIBLE -1
 10 #define OVERFLOW -2
 11 
 12 #define LIST_INIT_SIZE 100          //存儲空間初始分配量
 13 #define LISTINCREMENT 10            //存儲空間不夠時,動態增加的分配量
 14 
 15 typedef int ElemType;
 16 typedef int Status;
 17 
 18 typedef struct {
 19     ElemType *elem;         //存儲空間基地址
 20     int length;             //當前長度
 21     int listsize;           //當前分配的存儲容量,即有多少個ElemType空間
 22 }SqList;
 23 
 24 Status InitList_Sq(SqList *L);          //初始化線性表
 25 Status ListInsert_Sq(SqList *L,int i,ElemType e);           //插入操作
 26 Status ListDelete_Sq(SqList *L,int i,ElemType *e);          //刪除操作
 27 void TraverseList_Sq(SqList L);         //遍歷線性表
 28 
 29 int main()
 30 {
 31     SqList L;
 32     ElemType e;
 33     int i,n = 10;
 34     InitList_Sq(&L);
 35     for(i= 1; i <= n; i++)
 36     {
 37         ListInsert_Sq(&L,i,i);           //插入10個元素,1,2,3,...,10
 38     }
 39     TraverseList_Sq(L);
 40 
 41     printf("輸入要刪除的位置:");
 42     scanf("%d",&i);
 43     ListDelete_Sq(&L,i,&e);
 44     TraverseList_Sq(L);
 45     printf("e=%d\n",e);
 46 
 47     printf("輸入要插入的位置和數值:");
 48     scanf("%d %d",&i,&e);
 49     ListInsert_Sq(&L,i,e);
 50     TraverseList_Sq(L);
 51     return 0;
 52 }
 53 
 54 Status InitList_Sq(SqList *L)
 55 {
 56     //分配存儲空間100*sizeof(ElmeType),並把存儲空間基地址返回給L->elem
 57     L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
 58     if(!L->elem) exit(OVERFLOW);
 59     L->length = 0;
 60     //初始化存儲空間容量為100
 61     L->listsize = LIST_INIT_SIZE;
 62     return OK;
 63 }
 64 
 65 //在第i個位置前,插入新元素,i是從1開始變化的
 66 Status ListInsert_Sq(SqList *L,int i,ElemType e)
 67 {
 68     //插入位置不合理
 69     if(i < 1 || i> L->length+1) return ERROR;
 70     //存儲空間不足,增加分配
 71     if(L->length >= L->listsize )
 72     {
 73         ElemType *newbase = (ElemType *) realloc (L->elem,
 74                                         (L->listsize + LISTINCREMENT)*sizeof(ElemType));
 75         if(!newbase) exit(OVERFLOW);
 76         L->elem = newbase;
 77         L->listsize += LISTINCREMENT;
 78     }
 79     ElemType *q = &(L->elem[i-1]);
 80     ElemType *p;
 81     for(p = &(L->elem[L->length-1]); p >= q; --p)
 82     {
 83         *(p+1)=*p;
 84     }
 85     *q = e;
 86     (L->length)++;
 87     return OK;
 88 }
 89 
 90 Status ListDelete_Sq(SqList *L,int i,ElemType *e)
 91 {
 92     //刪除位置不合理
 93     if(i < 1 || i > L->length) return ERROR;
 94     ElemType *p = &(L->elem[i-1]);
 95     *e = *p;
 96     ElemType *q = &(L->elem[L->length-1]);
 97     for(++p; p <= q; p++)
 98     {
 99         *(p-1) = *p;
100     }
101     (L->length)--;
102     return OK;
103 }
104 
105 void TraverseList_Sq(SqList L)
106 {
107     int i;
108     printf("線性表元素為:");
109     for(i = 0; i <= L.length-1; i++)
110     {
111         printf("%d,",L.elem[i]);
112     }
113     printf("\n");
114 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM