(一)線性表結構1數組實現

來源:互聯網
上載者:User

標籤:插入資料   strong   empty   padding   print   find   結構體   max   刪除   

(一)數組實現線性表

  1.定義集合(結構體實現)

1 #define MAX 102  typedef struct {3       int DATA[MAX];4       int last;5 }LIST;

    DATA數組用於存放整型資料,last表示最後元素的下標

   2.定義各類操作

    a.建立空表

    

LIST *MakeEmpty() {    LIST *PtrL;    PtrL = (LIST *)malloc(sizeof(LIST));    PtrL->last = -1;    return PtrL;}

    b.插入資料

void Insert(int x, LIST *ptrl, int i) {    if (ptrl->last == MAX - 1) {        printf("表滿");        return;    }    if (i<1 || i>ptrl->last + 2) {        printf("位置不合法");        return;    }    int j;    for (j = ptrl->last; j >= i - 1; j--) {        ptrl->DATA[j + 1] = ptrl->DATA[j + 1];    }    ptrl->DATA[i - 1] = x;    ptrl->last++;    return;}

     c.尋找

int Find(LIST *ptrl, int data) {    int i = 0;    while (i <= ptrl->last&&ptrl->DATA[i] != data)        i++;    if (i > ptrl->last)        return -1;    else        return i;}

    d.刪除

void Delete(LIST *l, int i) {    int j;    if (i<1 || i>l->last + 1) {        printf("不存在該位置!");        return;    }    for (j = i; j <= l->last; j++) {        l->DATA[j - 1] = l->DATA[j];    }    l->last--;}

    e.列印(遍曆)

void Print(LIST *l) {    int i;    for (i = 0; i <= l->last; i++)        printf("%d ", l->DATA[i]);}

 

 

 

 

 

完整實現

 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define MAX 10 4 typedef struct { 5     int DATA[MAX]; 6     int last; 7 }LIST; 8  9 LIST *MakeEmpty() {10     LIST *PtrL;11     PtrL = (LIST *)malloc(sizeof(LIST));12     PtrL->last = -1;13     return PtrL;14 15 }16 17 int Find(LIST *ptrl, int data) {18     int i = 0;19     while (i <= ptrl->last&&ptrl->DATA[i] != data)20         i++;21     if (i > ptrl->last)22         return -1;23     else24         return i;25 }26 27 void Insert(int x, LIST *ptrl, int i) {28     if (ptrl->last == MAX - 1) {29         printf("表滿");30         return;31     }32     if (i<1 || i>ptrl->last + 2) {33         printf("位置不合法");34         return;35     }36     int j;37     for (j = ptrl->last; j >= i - 1; j--) {38         ptrl->DATA[j + 1] = ptrl->DATA[j + 1];39     }40     ptrl->DATA[i - 1] = x;41     ptrl->last++;42     return;43 }44 45 void Delete(LIST *l, int i) {46     int j;47     if (i<1 || i>l->last + 1) {48         printf("不存在該位置!");49         return;50     }51     for (j = i; j <= l->last; j++) {52         l->DATA[j - 1] = l->DATA[j];53     }54     l->last--;55 }56 57 void Print(LIST *l) {58     int i;59     for (i = 0; i <= l->last; i++)60         printf("%d ", l->DATA[i]);61 }

 

(一)線性表結構1數組實現

聯繫我們

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