Use C language to implement linear table
A linear table is the most common and simple data structure. A linear table is a finite sequence of n data elements. Each data element in a sequence can be a number, a character, or a complex struct or object. For example, 1, 2, 3, 4, 5 is A linear table, A, B, C, D... Z is a linear table, with a column of vehicle carriage 1 and carriage 2... carriage n is a linear table. There are two types of linear table internal representation (also called storage structure): sequential Storage Structure and chained storage structure. The sequential storage structure, as its name implies, is a Storage Structure Stored in order, such as a linear table (1, 2, 3, 4, 5) with a total of five elements, assume that each int type data element occupies four storage units. Assume that the storage address of 1st element number 1 is 1000, then the storage address of 2nd element number 2 is 1004, the storage address of the 3rd element number 3 is 1008, and so on, the storage address of the nth data element is LOC (an) = LOC (a1) + (n-1) k. (k indicates the length of the storage unit occupied by each data element) Obviously, this storage structure, adjacent elements are physically adjacent. Generally, we call a linear table using this storage structure "sequential table ". Chained storage structure, which does not require Adjacent Elements to be physically adjacent. Therefore, it can store data elements of a linear table using a set of storage units at any location. In this case, how should we express the logical relationship between data elements? To represent the logical relationship between data elements, for Data Element a1, in addition to storing its own information, it also needs to store information indicating its direct successor, so we introduce the pointer concept: the domain that stores the data element information is called the data domain, and the domain that stores the direct successor address information is called the pointer domain. The data element that includes its own data information and its direct successor address information is "Node ". Obviously, in this storage structure, adjacent elements are not necessarily adjacent in physical locations, and they use pointers to represent logical relationships. Generally, we call a linear table using this storage structure as a "Linear Linked List ". With the basic concepts, we can use programming languages to describe C, C ++, C #, Java, etc. In this article, I use C language to describe. 1. In order to describe the sequence table, we must first declare a structure, as shown below: # define LIST_INIT_SIZE 100 // initial allocation of a linear table storage space # define LISTINCREMENT 10 // increment allocation of a linear table storage space (used when the storage space is insufficient) typedef int ElemType; // data element type. Suppose it is int type typedef struct {ElemType * elem; // The base address of the bucket int length; // The length of the current linear table int listsize; // currently allocated storage capacity} SqList; after defining a linear table, you can operate it. Common basic operations of linear tables include: create a linear table, search for elements, insert elements, delete elements, clear, and merge. Implementation of basic operations on a linear table in a sequence table: 1. create a linear table int InitList (SqList & L) {L. elem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType); // open up a bucket and assign the base address of the bucket to elem if (! L. elem) {return-1; // space allocation Failed} L. length = 0; // The current length of L. listsize = LIST_INIT_SIZE; // return 0 for the current allocation;} 2. the Value-Based Query of an element (value-based query) linear table is used to find data elements that are equal to the given value x in a linear table. The simplest way to complete this operation is to compare the first element a1 with x in sequence. If they are equal, the subscript of the element is returned. If no element equal to x is found in the entire table,-1 is returned. Time Complexity: the basic operations of the algorithm (compare x with the I <1, n> ELEMENTS IN L) are related to the position of element x in the table and the table length. When a1 = x, the comparison is successful once. When an = x, the comparison is successful n times. The average comparison times are n + 1/2, and the time complexity is O (n ). Int LocateElem (SqList L, ElemType x) {int pos =-1; for (int I = 0; I <L. length; I ++) {if (L. elem [I] = x) {pos = I ;}} return pos ;}3. insert element time complexity O (L. length) is O (n) int ListInsert (SqList & L, int I, ElemType e) {// determine the validity of the insert position if (I <1 | I> L. length + 1) return-1; // determine whether the bucket is sufficient if (L. length> = L. listsize) {ElemType * newbase = (ElemType *) realloc (L. elem, (L. listsize + LISTINCREMENT) * sizeof (ElemType); if (! Newbase) return-1; // The bucket allocation fails. elem = newbase; // new base address L. listsize + = LISTINCREMENT; // increase storage capacity} // insert operation ElemType * q, * p; // define two pointer variables q = & (L. elem [I-1]); // q is the insert position (note that the shape parameter I is the serial number, the serial number is from 1, and the subscript is from 0, therefore, after the subscript is converted to a i-1) for (p = & (L. elem [L. length-1]); p> = q; -- p) // move back from ai to an-1. Note that the move-back operation should be performed from the back to the back {* (p + 1) = * p;} * q = e; ++ L. length; // Add 1 return 0 to the table length;} 4. delete element time complexity O (L. length) is O (n) int ListDelete (SqList & L, int I, ElemType & E) {// determine the validity of the deleted location if (I <1 | I> L. length) return-1; // Delete the ElemType * q, * p; // define two pointer variables p = & (L. elem [I-1]); // p indicates the position of the deleted element (note that the parameter I is the sequence number, the sequence number starts from 1, and the subscript starts from 0, so here after conversion into subscript is i-1) e = * p; // The deleted element assigned to e (may not be used, may also be used, so save it to e) q = L. elem + L. length-1; // q points to the last element at the end of the table (q is the address of the last element) for (++ p; p <= q; ++ p) // step forward from the next element of p {* (p-1) = * p;} -- L. length; // The table length minus 1 return 0;} test code: # include <stdio. h> # include <stdlib. h> int main () {SqList list; InitList (list); int n = 10; // Add 10 numbers to the linear table list for (int I = 0; I <10; I ++) {ListInsert (list, I + 1, I + 1) ;}// Delete 5th ElemType e; ListDelete (list, 5, e); printf ("the deleted elements are: % d \ n ", e); // insert an element-1 ListInsert (list, 2,-1) at 2nd locations ); // output linear table for (int I = 0; I <10; I ++) {printf ("% d", list. elem [I]);} // The output result is: 1-1 2 3 4 6 7 8 9 10 system ("pause ");} 2. Linear Linked List in order to describe Linear Linked List, we must first declare a structure, as follows: typedef struct LN Ode {ElemType data; // data field struct LNode * next; // pointer field} LNode; after the Linear Linked List is defined, you can operate on it, common basic operations of a Linear Linked List include searching, inserting, deleting, and clearing. Implementation of basic operations on Linear tables in linear linked lists: 1.1 create a linked list (Head insertion) time complexity O (n) LNode * CreateListHead (int n) {LNode * L; L = (LNode *) malloc (sizeof (LNode); L-> next = NULL; LNode * p; // p is the new node, point to the last element for (int I = n; I> 0; -- I) {p = (LNode *) malloc (sizeof (LNode )); // create a space for the new node scanf ("% d", & p-> data); p-> next = L-> next; L-> next = p ;} return L;} 1.2 create a linked list (tail insertion) LNode * CreateListTail () {LNode * L; L = (LNode *) malloc (sizeof (LNode )); l-> next = NULL; // Empty table LNode * s, * r = L; // r is the tail pointer s is the new node int x; scanf ("% d", & x ); int flag = 0; // end mark while (x! = Flag) {s = (LNode *) malloc (sizeof (LNode); s-> data = x; r-> next = s; r = s; scanf ("% d", & x) ;}r-> next = NULL; // The last element, the next field, points to NULL return L;} 2. search for elements (take element I) int GetElem (LNode L, int I, ElemType & e) {LNode * head, * p; // head is the header pointer, p: Search for the subscript head = & L; p = head; // The initial value of p points to the 1st element int j = 0; while (p! = NULL & j <I) {p = p-> next; j ++;} if (p = NULL | j> I) return-1; // element I does not exist e = p-> data; return 0;} 3. insert elements to insert nodes in the linked list. You only need to modify the pointer. To insert an element before the I node, first you need to locate the I-1 node and then modify the pointer to the I-1 node. Time complexity O (n) int ListInsert (LNode * L, int I, ElemType e) {LNode * p; // Set p to the I-1 node // first find the I-1 node int j = 0; p = L; while (p! = NULL & j <i-1) {p = p-> next; j ++;} // apply for a new node LNode * s = (LNode *) malloc (sizeof (LNode); s-> data = e; s-> next = p-> next; // The direct successor of s points to p's original direct successor p-> next = s; // The direct successor of p points to s return 0;} 4. to delete an element to delete the I-th node, you only need to modify the pointer of the I-1-th node to point to the I + 1 to reduce the time complexity O (L. length) is O (n) int ListDelete (LNode * L, int I, ElemType & e) {LNode * p, * q; // Set p to the I-1 node q mark Delete location // first find the I-1 node int j = 0; p = L; // The base address where p is L while (p! = NULL & j <i-1) {p = p-> next; j ++;} q = p-> next; p-> next = q-> next; // The next field of p points to p-> next e = q-> data; return 0;} The test code is as follows: # include <stdio. h> # include <stdlib. h> int main () {LNode * L; L = CreateListTail (); // create a Linear Linked List. input 0 to end ListInsert (L, 1,100 ); // insert 1st ElemType e at 100 locations; // The ListDelete (L, 3, e) element to be deleted; // Delete the 3rd element LNode * p; p = L; printf ("Output Linear Linked List: \ n"); while (p-> next! = NULL) {p = p-> next; printf ("% d", p-> data);} system ("pause");} I am a beginner in data structure, if you have any shortcomings, please let us know.