Implementation of linear tables

xiaoxiao2021-03-06  104

/ * ------------------------------------- Program illustrated: Linear chain head file MyList.h Date : 2004.3.26 author: junnyfeng ---------------------------------------- * /

#define list_init_size 20 // Initial Distribution #define ListINcrement 10 // Increment TypeDef Int ElemType; // Custom Basic Type TypeDef Struct {ELEMTYPE * ELEM; // Storage Space Extracus INT Length; // Summer INT LISTSIZE // Currently allocated storage capacity} SQLIST;

INT CREATLIST_SQ (SQList *); // Built a blank list

INT ListLength (SQLIST); // Returns INT ListInsert_sq (SQList *, INT I, ELEMTYPE E); // Insert E from the i-th position, i from 1

INT ListDelete_sq (SQList *, Int, ElemType *);

INT LISTEMPTY_SQ (SQLIST *); / / The empty table returns 1, otherwise returns 0

INT locateelem_sq (SQLIST *, INT, INT COMPARE_ELEM (ELMTYPE, ELEMTYPE)); // Returns the first order from E, otherwise returns zero

INT COMPARE_ELEM (ELEMTYPE, ELEMTYPE); // Compare two elements, the same return 1, different return zero

Void getlem (SQLIST, INT I, ELEMTYPE * E); // Put the elements of the i-th position to e

Void Clearlist (Sqlist *); // let the length = 0, and listsize = list_init_size

Void destorylist (sqlist *); // destroyed

Void Union_SQ (SQList * a, sqlist b); // Receive the part of the B table to a table in the table A.

Void Mergelist (Sqlist A, SQLIST B, SQLIST *); // A, B table is increasing, incorporate into a new table, and keep increment

/ * ------------------------------------ Serial Description: Linear List and Its Basic Operation Function MYLIST .c date: 2004/03/26 author: junnyfeng -------------------------------------- - * /

#include #include #include "mylist.h"

INT CREATLIST_SQ (SQList * L) // Built a sequential table, successful returns 1, not successful return 0 {l-> elem = (elemType *) malloc (List_init_size * sizeof (elemType)); if (! l-> elem) Return 0; l-> length = 0; l-> listsize = list_init_size; returnit 1;}

INT LISTLENGTH (SQList L) {IF (L.Length) Return L.LENGTH; RETURN 0;}

INT LISTINSERT_SQ (SQList * L, ElemType I, ELEMTYPE E) // Insert an element in the i-th position (first position 1), // Return 1 for success, 0 for flash {ELEMTYPE * Q, * Newbase, * p; if (i <1 || l-> length 1 length > = L-> listsize) {newbase = (elemType *) Realloc (l-> elem, (l-> listSize listincrement) * sizeof (elemType)); if (! Newbase) {printf ("can't Assign A NEWBASE / N "); return 0;} l-> elem = newbase; l-> listSize = listincrement;} q = & l-> elem [i-1]; // Waiting for location address Q for (p = & l -> elem [l-> length-1]; p> = q; p -) * (p 1) = * p; * q = E; L-> Length ; return 1;} int Listdelete_sq (SQList * L, elemType i, elemType * e) // Delete the i-th element in the sequence table, and returns its value with E, successfully returns 1 {IF (i <1 || i> L-> length) {Printf ("/ Ncan't locate the section% d ", i); exit (1);} * e = l-> elem [i-1]; while (i <= l-> length-1) {l-> elem [ I-1] = L-> ELEM [i]; i;} l-> length-; return 1;}

INT LISTEMPTY_SQ (SQList * L) {if (l-> length == 0) Return 1; Else Return 0;}

INT COMPARE_ELEM (ELMTYPE A, ELEMTYPE B) {IF (A-B == 0) Return 1; Else Return 0;}

INT locateelem_sq (SQLIST * L, ELEMTYPE E, INT COMPARE_ELEM (ELEMTYPE, ELEMTYPE)) {INT i; for (i = 0; i <= l-> length-1; i ) {ix (COMPARE_ELEM (L-> ELEM [ I], e)) RETURN I 1;} return 0;} Void getlem (Sqlist L, INT I, ELEMTYPE * E) {IF (i <1 || i> L.Length) {Printf ("over the bound Of the list! "); system (" pause "); exit (1);}

* E = L.Elem [i-1];

Void ClearList (SQList * L) {if (l-> length! = 0) {l-> length = 0; l-> listsize = list_init_size;}} void union_sq (SQList * a, sqlist b) {INT i, E For (i = 1; i <= B.LENGTH; I ) {getElem (B, I, & E); if (! Locateelem_sq (a, e, compare_elec)) listinsert_sq (A, A-> Length 1, E );}} Void destoryList (SQList * L) {if (l-> elem) free (l-> elem);

Void Mergelist (SQLIST M, SQLIST L, SQLIST * R) {INT MA, LA, I, J, K = 0; i = j = 1; if (! Creatlist_sq (r)) {Printf ("/ Ncan't Creat A list! "); exit (1);} while (i <= listlength (m) && j <= listLength (L)) {getElem (M, I, & MA); getElem (L, J, & La); IF (MA

Else {listinsert_sq (r, k, la); j ;}} while (i <= listLength (m)) {getElem (M, I , & ma); Listinsert_sq (R, K, MA);} While (j <= listlength (l)) {getElem (L, J , & la); Listinsert_sq (R, K, La);}}

转载请注明原文地址:https://www.9cbs.com/read-124797.html

New Post(0)