C 資料結構 — 線性表(順序表)

來源:互聯網
上載者:User
#include <stdio.h>#include <stdlib.h>#define MAXLENGTH 1000typedef struct{   int total;   int date[MAXLENGTH];}lineList;void showList(lineList* list){    int i;    if(list->total ==0){       printf("空的線性表\n");       return;    }     printf("當前線性表狀態:");    for(i=0;i<list->total;i++){        printf("%d\t",list->date[i]);    }    printf("\n");}void outPut(lineList* list){    system("cls");    printf("-- 順序表 --\n");    printf("a、 - 添加節點         i、 - 插入節點\n");    printf("d、 - 刪除節點         e、 - 退出\n");    showList(list);}lineList* createList(){        lineList* list = (lineList*)malloc(sizeof(lineList));    list->total = 0;    return list;}void appendNode(lineList* list,int val){    if(list->total < MAXLENGTH ){        list->date[list->total] = val;        list->total++;    }}void insertNode(lineList* list,int val ,int pos){    int i;    if(pos<0 || pos>list->total){        printf("插入位置不正確");    }    for(i=pos;i<list->total;i++){               list->date[i+1] = list->date[i];    }    list->date[pos] = val;        list->total += 1;}void deleteNode(lineList* list, int pos){    if(pos<0 || pos > list->total){                printf("所刪除的節點不存在");        }else{                 int i;        for(i=pos;i<list->total;i++)             list->date[i] = list->date[i+1];        list->total--;    }}void main(){    int key, pos;    char act;    lineList* myList = createList();    while(1){        outPut(myList);        printf("請操作:");        act = getchar();        fflush(stdin);        switch(act){           case 'a':                printf("請輸入要添加的 KEY");                scanf("%d",&key);                appendNode(myList,key);                break;           case 'i':                printf("請輸入要插入的 KEY 和 POS");                scanf("%d %d",&key,&pos);                insertNode(myList,key,pos);                break;           case 'd':                printf("請輸入要刪除的 POS");                scanf("%d",&pos);                deleteNode(myList,pos);                break;           case 'e':                exit(0);                outPut(myList);                fflush(stdin);                break;           default:               break;        }            }}

順序表 是把線性表中的所有元素按照其邏輯一次儲存到指定的一塊連續的儲存空間···

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.