Basic operations of ordered storage structure linear tables are implemented in pure C Language

Source: Internet
Author: User

I wrote the sequence table in C language, tested it, made a question, and merged la and lb.

# Include <stdio. h> # include <stdlib. h> # define true 1 # define false 0 # define OK 1 # define error 0 # define Infeasible-1 # define overflow-2 # define list_inst_size 5 # define listincrement 1 typedef int status; typedef int elemtype; typedef struct {elemtype * ELEM; int length; int listsize;} SQL; static elemtype element;/* function name: initlist () parameter: SQL l initial condition: no function: Construct an empty linear table return value: Storage Allocation failed: overflow storage allocation successful: OK */status initlist (S QL & L) {If (L. ELEM = (elemtype *) malloc (sizeof (elemtype) * list_inst_size) = NULL) return overflow; else {L. length = 0; L. listsize = list_inst_size; Return OK ;}/ * function name: destroylist () parameter: SQL l initial condition: Linear table L existing function: Destroy linear table return value: l. ELEM = NULL: Error L. ELEM! = NULL: OK */status destroylist (SQL & L) {If (L. ELEM = NULL) Return Error; else {free (L. ELEM); Return OK;}/* function name: clearlist () parameter: SQL L; initial condition: Linear existing table l function: clear linear table return value: l. ELEM = NULL: Error L. ELEM! = NULL: OK */status clearlist (SQL & L) {If (L. ELEM = NULL) Return Error; else {L. length = 0; Return OK;}/* function name: listempty () parameter: SQL L; initial condition: Linear table function: determines whether the linear table is empty. Return: NULL: true non-null: false */status listempty (SQL & L) {If (L. length = 0) return true; else return false;}/* function name: listlength () parameter: SQL l initial condition: a linear table already exists function: return the return value of the linear table length: linear table length (L. length) */status listlength (SQL & L) {return L. length;}/* function name: getelem () parameter: sqlis T l, int I, elemtype * E (the global variable element is used here) initial condition: a linear table already exists. function: use e to return the return value of element I in a linear table: (I <1) | (I> listlength (L): overflow 1 <= I <= listlength (l): OK */status getelem (SQL & L, int I, elemtype & E) {If (I <1) | (I> listlength (L) return overflow; Int J; elemtype * P; P = L. ELEM; For (j = 0; j <I-1; j ++) L. ELEM ++; E = * L. ELEM; L. ELEM = P; Return OK;}/* function name: locateelem () parameter: SQL L, elemtype element initial condition: Linear Table l already exists. function: return the returned values of the first element equal to the element in the sequence table. If l contains an element equal to the element, returns the first element that meets the condition. If no element exists, returns 0 */status locateelem (SQL & L, elemtype e) {int I; elemtype * P; P = L. ELEM; for (I = 1; I <= L. length; I ++) {If (* L. ELEM = e) {L. ELEM = P; return I;} l. ELEM ++;} l. ELEM = P; return 0;}/* function name: priorelem () parameter: SQL L, elemtype cur_e, elemtype * pre_e initial condition: the linear table already exists, i> 1 & I <= L. length, locationelem () function: Use pre_e to return the precursor of cur_e in a linear table Returned value: I <= 1 | I> L. length: Overflow I> 1 & I <= L. length: OK */status priorelem (SQL & L, elemtype cur_e, elemtype * pre_e) {elemtype * P = L. ELEM; int I; I = locateelem (L, cur_e); // Why not use & L because it is called by reference if (I = 0) return overflow; Int J; for (j = 0; j <I-2; j ++) {L. ELEM ++;} * pre_e = * L. ELEM; // note that pre_e = L. ELEM does not work l. ELEM = P; Return OK;}/* function name: listinsert (); parameter: SQL l, int I, elemtype e initial condition: Linear table l already exists, 1 <= I <= L. length + 1; function: insert E before the I element of a linear table. Return Value: Failed: Error successful: OK */status listinsert (SQL & L, int I, elemtype E) {int * q = & (L. ELEM [I-1]); elemtype * newbase, * P; if (I <1 | I> (L. length) + 1) Return Error; If (L. length> = L. listsize) {newbase = (elemtype *) realloc (L. ELEM, L. listsize + listincrement * sizeof (elemtype); If (newbase = NULL) Exit (overflow); L. ELEM = newbase; L. listsize + = listincrement;} For (P = & (L. ELEM [L. length-1]); P> = Q; p --) * (p + 1) = * P; * q = E; ++ L. length; Return OK;}/* function name: listdelete () parameter: SQL l, int I; elemtype * E; initial condition: Linear table l already exists. 1 <= I <= L. length function: Delete the I-th element in a linear table, and return the value of the deleted number with E. Return Value: Failed: Error success: OK */status listdelete (SQL & L, int I, elemtype & E) {If (I <1) | (I> L. length) Return Error; E = (L. ELEM [I-1]); elemtype * q; For (q = & (L. ELEM [I-1]); q <& (L. ELEM [L. length-1]); q ++) {* q = * (q + 1);} l. length --; Return OK;} status listsee (SQL & L) {If (L. ELEM = NULL) | (L. length = 0) Return Error; else {int I; for (I = 0; I <L. Length; I ++) {printf ("% d", L. ELEM [I]);} printf ("\ n");} Return OK;} status listscan (SQL & L) {int I, A; printf ("Enter numbers: "); for (I = 0; I <list_inst_size; I ++) {scanf (" % d ", & A); listinsert (L, L. length + 1, A) ;}} int main () {SQL la, Lb; int I, e; initlist (LA); initlist (LB); listscan (LA ); listscan (LB); for (I = 0; I <lb. length; I ++) {e = LB. ELEM [I]; If (! Locateelem (La, E) listinsert (La, la. length + 1, e); // lb. ELEM ++;} printf ("La ="); listsee (LA); System ("pause"); Return 0 ;}

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.