C語言 線性表描述 (實戰片)

來源:互聯網
上載者:User
#include<stdio.h>#include<stdlib.h>#define TRUE 1#define FALSE 0#define MAXSIZE 11typedef int elemType;typedef struct {    elemType *list;    int length;}sqList;//initvoid initList(sqList *L){    L -> length = 0;    L -> list = malloc(MAXSIZE * sizeof(elemType));    //printf("%d", sizeof(elemType));    //printf("%d", L -> list);    if(!L->list){        printf("failure!\n");exit(0);    }    printf("init success!\n");    return;}//destroyvoid destroyList(sqList *L){    //printf("%d", L -> list);    if(!L -> list){        printf("not exit!\n");        exit(0);    }    free(L -> list);    L -> list = NULL;    L -> length = 0;    printf("destroy successful ! \n");    return;}//clearvoid clearList(sqList *L){    if(L -> list != NULL){        L -> length = 0;    }    return;}//isEmptyint listEmpty(sqList *L){    return L -> length == 0 ? TRUE : FALSE;}//return the count of Lint listLength(sqList *L){    return L -> length;}//return element of the positionelemType getElem(sqList *L, int i){    if(i<1 || i > listLength(L)){        printf("out of bound!");        exit(0);    }    return L -> list[i-1];}//return index of the element, if not exit return 0int locateElem(sqList *L, elemType e){    elemType *p = L -> list;    int i = 0;    while(i < listLength(L) && *p != e){        p++;        i++;    }    if(i == listLength(L)){i=-1;    }    return i+1;}//return previous elementelemType preElem(sqList *L, elemType cur_e){    int i;    elemType *p = L -> list;    for(i=1; i<listLength(L); i++, p++){if(*p == cur_e){    break;        }    }    if(i == listLength(L)){printf("not exit in this sqList");   exit(0);    }    if(i == 1){printf("no previous element\n");   exit(0);    }    return L -> list[i-2];}//return next elementelemType nextElem(sqList *L, elemType cur_e){    int i;    elemType *p = L -> list;    for(i=1; i<listLength(L); i++, p++){if(*p == cur_e){    break;        }    }    if(i == listLength(L)){printf("not exit in this sqList\n");   exit(0);    }    if(i == listLength(L)){printf("no next element\n");   exit(0);    }    return L -> list[i];}//insert elementvoid listInsert(sqList *L, int i, elemType e){    if(i < 1 || i > listLength(L) + 1){printf("out of bound, insert failure !\n");exit(0);    }    if(listLength(L) >= MAXSIZE){printf("full, insert failure !\n");exit(0);    }    int j = listLength(L);    while(j >= i){L -> list[j] = L -> list[j-1];j--;    }    L -> list[i-1] = e;    L -> length++;    return;}//delete element and return this elementelemType listDelete(sqList *L, int i, elemType e){    if(i < 1 || i > listLength(L)){printf("out of bound delete failure !\n");exit(0);    }    e = L -> list[i-1];    while(i < listLength(L) + 1){L -> list[i-1] = L -> list[i];i++;    }    L -> length--;    return e;}//traverse sqListvoid listTraverse(sqList *L){    int i;    for(i=0; i<listLength(L); i++){printf("%d ", L -> list[i]);    }    printf("\n");    return;}main(){    sqList L;    initList(&L);    listInsert(&L, 1, 23);    listInsert(&L, 1, 12);    listInsert(&L, 1, 01);    printf("共有%d個元素\n", listLength(&L));    listTraverse(&L);    elemType e;    e = listDelete(&L, 1, e);    printf("被刪除元素是:%d\n", e);    printf("共有%d個元素\n", listLength(&L));    listTraverse(&L);    e = L.list[0];    printf("元素%d下一個元素是:%d\n", e, nextElem(&L, e));    //printf("元素%d上一個元素是:%d\n", e, preElem(&L, e));    printf("元素%d的位置是:%d\n", e, locateElem(&L, e));    e = getElem(&L, 1);    printf("1號元素是:%d\n", e);    destroyList(&L);    return 0;   }

如有錯誤,請指出。謝謝。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.