Data structure (two operations for the sequence table)

Source: Internet
Author: User

Merge A and B of the sequence table, and merge a non-decreasing sequence table into a non-decreasing sequence table.

Because the header file contains a large amount of content, include ~~~ is not used here ~~~ Because on the blog ~~~ You just copied it ~~~ Wood has been deleted and modified ~~

# Include <string. h> # include <ctype. h> # include <malloc. h>/* malloc () and so on */# include <limits. h>/* int_max, etc. */# include <stdio. h>/* EOF (= ^ Z or F6), null */# include <stdlib. h>/* atoi () */# include <Io. h>/* EOF () */# include <math. h>/* floor (), Ceil (), ABS () */# include <process. h>/* exit () * // * function result status Code */# Define true 1 # define false 0 # define OK 1 # define error 0 # define Infeasible-1/* # define overflow-2 because in math. in H, the overflow value has been defined as 3, so removing this line */typedef int status;/* status is the function type, and its value is the function result status code, such as OK */typedef int Boolean;/* Boolean is of the boolean type, and its value is true or false */typedef int elemtype; # define list_init_size 10 # define listincrement 2 typedef struct {elemtype * ELEM; int length; int listsize;} sqlist; // ---- St Art ---------- basic operations on the sequence table (12) ---------------- start --------------- // status initlist (sqlist * l) {/* operation result: construct an empty ordered linear table */(* l ). ELEM = (elemtype *) malloc (list_init_size * sizeof (elemtype); If (! (* L ). ELEM) Exit (overflow);/* storage allocation failed */(* l ). length = 0;/* empty table length is 0 */(* l ). listsize = list_init_size;/* initial storage capacity */Return OK;} status destroylist (sqlist * l) {/* initial condition: the ordered linear table l already exists. Operation Result: destroy the ordered linear table L */free (* l ). ELEM); (* l ). ELEM = NULL; (* l ). length = 0; (* l ). listsize = 0; Return OK;} status clearlist (sqlist * l) {/* initial condition: the ordered linear table l already exists. Operation Result: reset L to an empty table */(* l ). length = 0; Return OK;} status listempty (sqlist L) {/* initial condition: the ordered linear table l already exists. Operation Result: if l is empty, true is returned; otherwise, false */If (L. length = 0) return true; else return false;} int listlength (sqlist L) {/* initial condition: the ordered linear table l already exists. Operation Result: returns the number of data elements in L */return L. length;} status getelem (sqlist L, int I, elemtype * E) {/* initial condition: the ordered linear table l already exists, 1 ≤ I ≤ listlength (l) * // * operation result: use e to return the value of the I-th Data Element in L */if (I <1 | I> L. length) Exit (error); * E = * (L. ELEM + i-1); Return OK;} int locateelem (sqlist L, elemtype E, status (* compare) (elemtype, elemtype) {/* initial condition: the ordered linear table l already exists, and compare () is the data element judgment function (satisfying 1, otherwise 0) * // * operation result: returns the order of the 1st data elements that meet the condition of compare () with E in L. * // * If such a data element does not exist, the return value is 0. algorithm 2.6 */elemtype * P; int I = 1;/* The initial value of I is the order of 1st elements */P = L. ELEM;/* The initial value of P is the storage location of the 1st elements */while (I <= L. length &&! Compare (* P ++, E) ++ I; if (I <= L. length) return I; else return 0;} status priorelem (sqlist L, elemtype cur_e, elemtype * pre_e) {/* initial condition: the sequence linear table l already exists * // * operation result: If cur_e is a data element of L and is not the first, pre_e is used to return its precursor, * // * otherwise, the operation fails. pre_e is not defined */INT I = 2; elemtype * P = L. ELEM + 1; while (I <= L. length & * P! = Cur_e) {P ++; I ++;} if (I> L. length) return infeasible; else {* pre_e = * -- p; Return OK;} status nextelem (sqlist L, elemtype cur_e, elemtype * next_e) {/* initial condition: the sequence linear table l already exists * // * operation result: If cur_e is a data element of L and is not the last one, use next_e to return its successor, * // * otherwise, the operation fails. next_e is not defined */INT I = 1; elemtype * P = L. ELEM; while (I <L. length & * P! = Cur_e) {I ++; P ++;} if (I = L. length) return infeasible; else {* next_e = * ++ P; Return OK;} status listinsert (sqlist * l, int I, elemtype E) /* algorithm 2.4 */{/* initial condition: the ordered linear table l already exists, 1 ≤ I ≤ listlength (l) + 1 * // * operation result: insert a new data element E before position I in L, and Add 1 */elemtype * newbase, * q, * P to the length of L; if (I <1 | I> (* l ). length + 1)/* The I value is invalid */Return Error; If (* l ). length> = (* l ). listsize)/* The current bucket is full, increase the allocation */{newbase = (elemtype *) realloc (* l ). ELEM ,(( * L). listsize + listincrement) * sizeof (elemtype); If (! Newbase) Exit (overflow);/* storage allocation failed */(* l ). ELEM = newbase;/* new base address */(* l ). listsize + = listincrement;/* increase storage capacity */} q = (* l ). ELEM + I-1;/* q For insert position */For (P = (* l ). ELEM + (* l ). length-1; P> = Q; -- p)/* right shift of the inserted position and the elements after insertion */* (p + 1) = * P; * q = E; /* Insert E */++ (* l ). length;/* increase the table length by 1 */Return OK;} status listdelete (sqlist * l, int I, elemtype * E)/* algorithm 2.5 */{/* initial condition: the ordered linear table l already exists. 1 ≤ I ≤ listlength (l) * // * operation result: Delete the I-th data element of L and return its value with E, l length minus 1 */Elemtype * P, * q; if (I <1 | I> (* l ). length)/* The I value is invalid */Return Error; P = (* l ). ELEM + I-1;/* P is the location of the deleted element */* E = * P;/* the value of the deleted element is assigned to E */q = (* l ). ELEM + (* l ). length-1;/* position of the end element of the table */For (++ P; P <= Q; ++ P) /* move the element after it is deleted to the left */* (p-1) = * P; (* l ). length --;/* Table length minus 1 */Return OK;} status listtraverse (sqlist L, INT (* VI) (elemtype *) {/* initial condition: the sequence linear table l already exists * // * operation result: the VI () function is called for each data element of L in sequence (). Once VI () fails, the operation fails * // * VI () participates in '&', indicating that the value of the element can be changed by calling VI () */elemtype * P; int I; P = L. ELEM; for (I = 1; I <= L. length; I ++) VI (P ++); printf ("\ n"); Return OK;} // --------- end ----- basic operations on the sequence table (12) --------------- end -------------- // status equal (elemtype C1, elemtype C2) {// checks whether the functions are equal. Union () uses if (C1 = c2) return true; else return false;} void Union (sqlist & la, sqlist lb) // algorithm 2.1 {// insert all the data elements in the linear table LB but not in LA into ELEM in LA Type E; int la_len, lb_len; int I; la_len = listlength (LA); // evaluate the length of a linear table lb_len = listlength (LB); for (I = 1; I <= lb_len; I ++) {getelem (LB, I, & E); // obtain the I data element in LB and assign it to e if (! Locateelem (La, E, equal) // if the same element as E does not exist in LA, insert listinsert (& la, ++ la_len, e );}} int print (elemtype * c) {printf ("% d", * C); Return 0;} void mergelist (sqlist la, sqlist LB, sqlist * LC) /* algorithm 2.2 */{/* data elements in the known linear tables la and lB are arranged in non-descending order by value. * // * Merge La and lb to obtain the new linear table LC. The data elements of LC are also arranged in non-decreasing order by value */INT I = 1, j = 1, K = 0; int la_len, lb_len; elemtype AI, BJ; initlist (LC);/* Create an empty table LC */la_len = listlength (LA); lb_len = listlength (LB ); while (I <= la_len & J <= lb_len)/* Table la and table lB are not empty */{getelem (LA, I, & AI); getelem (LB, j, & BJ); If (AI <= BJ) {listinsert (LC, ++ K, AI); ++ I ;}else {listinsert (LC, ++ K, BJ); ++ J ;}}while (I <= la_len)/* The table la is not empty and the table lb is empty */{getelem (LA, I ++, & AI); listinsert (LC, ++ K, AI);} while (j <= lb_len)/* Table lb is not empty and table la is empty */{getelem (LB, j ++, & BJ); listinsert (LC, ++ K, BJ) ;}} int main () {sqlist la, LB, LC; status I; Int J; I = initlist (& la); if (I = 1) // The empty table la is created successfully for (j = 1; j <= 5; j ++) // insert 5 elements in the table la: I = listinsert (& la, J, J); printf ("La = "); // output the content of the La Table listtraverse (La, print); initlist (& Lb); // you cannot determine whether the table is successfully created for (j = 1; j <= 5; j ++) // insert 5 elements in Table LB: I = listinsert (& Lb, J, 2 * j); printf ("lB = "); // output table LB content listtraverse (LB, print); Union (La, LB); printf ("new LA = "); // output the content of the new La Table listtraverse (La, print); initlist (& lc); // initialize LC mergelist (La, LB, & lc ); printf ("\ n non-decreasing sequence lc =");/* output table LC content */listtraverse (LC, print ); printf ("\ n"); 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.