C linear table

Source: Internet
Author: User

The linear table in C language is not complete yet

1 # include <stdio. h> 2 # define MAXSIZE 100 // initial space allocation 3 # define OK 1 4 # define ERROR 0 5 # define TRUE 1 6 # define FALSE 1 7 typedef int ElemType; // The type is int 8 typedef int Status; // The return type of the function, which is int 9 typedef struct10 {11 ElemType data [MAXSIZE]; 12 int length; 13} SqList; 14/* use e to return the obtained function value. If I exceeds the linear table range, ERROR */15 Status GetElem (SqList L, int I, ElemType * e) is returned) 16 {17 if (L. length = 0 | I <1 | I> L. length) 18 return ERROR; 19 * e = L. data [I-1]; 20 return OK; 21} 22 Status LisrInsert (SqList * L, int I, ElemType e) // insert element operation 23 {24 int k; 25 if (L-> length = MAXSIZE) // The linear table is full 26 return ERROR; 27 if (I <1 | I> L-> length) // when I is not in the range 28 {29 return ERROR; 30} 31 if (I <= L-> length) // The inserted data is not at the end of the table 32 {33 for (k = L-> length-1; k >=i-1; k --) // move the inserted data element to a position 34 L-> data [k + 1] = L-> data [k]; 35} 36 L-> data [I-1] = e; // Insert the new element 37 L-> length ++; 38 return OK; 39} 40/* result, delete the I-th element of L and return the value with e. The length of L is reduced by 1 */41 Status ListDelete (SqList * L, int I, ElemType * e) 42 {43 int k; 44 if (L-> length = 0) // The linear table is empty 45 return ERROR; 46 if (I <1 | I> L-> length) // deletion location incorrect 47 return ERROR; 48 * e = L-> data [I-1]; 49 if (I <L-> length) // if the last position is not deleted, 50 {51 for (k = I; k <L-> length; k ++) // move the delete position successor element forward 52 L-> data [k-1] = L-> data [k]; 53 54} 55 L-> length --; 56 return OK; 57 58} 59/* initialize the ordered linear table */60 void InitList (SqList * L) 61 {62 L-> length = 0; 63 64}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.