Data Structure-table, data structure

Source: Internet
Author: User

Data Structure-table, data structure
Data Structure-table

1. Definition:
A linear table is a linear structure. It is a finite sequence Containing n or greater than 0 knots. For the nodes, there is only one starting node, there is no precursor, but there is a successor node, there is only one terminal node without any successor, but there is a precursor node. Other nodes have only one and only one successor node.
2. features/Properties
1) A unique first element must exist in the collection.
2) A unique last element must exist in the collection.
3) except for the last element, it has a unique successor.
4) Apart from the first element, each has a unique precursor.
3. Basic operations on Linear tables
1) initialize a linear tableInitList(L)
2) destroy a linear tableDestoryList(L)
3) Clear the linear tableClearList(L)
4) Evaluate the length of a linear tableListLength(L)
5) Determine whether the linear table is emptyIsEmpty(L)
6) obtain the content of a Data Element in linear table L.GetElem(L,i,e)
7) Search for data elements with a value of eLocateElem(L,e)
8) returns the direct precursor element of E in linear table L.PriorElem(L,e)
9) returns the direct successor element of E in linear table L.NextElem(L,e)
10) insert a Data Element in linear table L.ListInsert(L,i,e)
11) Delete the I-th Data Element in the linear table L.ListDelete(L,i,e)
4. Sequential Representation of linear tables: ArrayList
The unit of the I-1 in the array stores the content of the I Data Element in the linear table. In other words, the subscript of the array in the C language starts from "0, if L is an ordered table, the I Data Element in the table is L. elem [I-1].
Advantage: Random Access element disadvantages: a large number of elements need to be moved during insertion and deletion.
Supplement: complex linear table operations
1. Calculate the Union of linear tables

Idea: Expand the linear table LA and insert the data elements that exist in the linear table LB that do not exist in the linear table LA into the linear table LA. You only need to extract each data element from the linear table LB in sequence, and visit it in the linear table LA according to the value. If it does not exist, insert it

Void union (List & La, List Lb) {La_len = ListLength (La); Lb_len = ListLength (Lb); for (I = 1; I <= Lb_len; I ++) {GetElem (Lb, I, e); // obtain the I-th Data Element in Lb and assign it to e if (! LocateElem (La, e, equal) // if the same data element as e does not exist in La, insert ListInsert (La, ++ La_len, e );}}

Algorithm complexity: O (LALB)
2. Linear tables are merged and arranged in descending order by values.

Void MergeList (List La, List Lb, List & Lc) {// data elements of the known linear tables La and Lb are sorted by values in a non-descending order. InitList (LC); I = j = 1; k = 0; La_len = ListLength (La); Lb_len = ListLength (Lb); while (I <= La_len) & (j <= Lb_len) {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) {GetElem (La, I ++, ai ); listInsert (Lc, ++ k, ai);} while (j <= Lb_len) {GetElem (Lb, j ++, bj); ListInsert (Lc, ++ k, bj );}}

Algorithm complexity: O (LA + LB)

Reference code-typical operation code implementation

// The array defines typedef struct {Elemtype elem [LIST_MAX_LENGTH]; // points to the int length of the data element in the linear table; // The current length of the linear table int listsize;} SEQLIST; // initialize the linear table int InitList (SEQLISI * L) {L. elem = (Elemtype *) malloc (LIST_MAX_LENGTH * sizeof (Elemtype); // allocate space if (L. elem = NULL) exit (OVERFLOW); // storage allocation failed L. length = 0; // The length of the empty table is 0 L. listsize = LIST_MAX_LENGTH; // initial storage capacity return OK;} // destroy the linear table void DestoryList (SEQLIST * L) {if (L. elem) free (L. elem); // Release all buckets occupied by the linear table} // clear the void ClearList (SEQLIST * L) {L. length = 0;} // calculates the length of a linear table. int GetLength (seqlist l) {return (L. length);} // determines whether the linear table is empty. int IsEmpty (seqlist l) {if (L. length = 0) return true; return false;} // obtain the content of a Data Element in the linear table L int GetElem (seqlist l, int I, Elemtype * e) {if (I <1 | I> L. length) return error; * e = L. elem [I-1]; // The I-1 units in the array store the I Data Element Content in the linear table return OK ;} // retrieve the data element int Lo with the value of e in the linear table L CateElem (seqlist l, Elemtype e) {for (I = 0; I <L. length; I ++) if (L. elem [I] = e) return I + 1; return 0;} // determine the order of the first data element in L that satisfies the compare () Relationship with e. If no, return 0 LocateElem (L, e, int (* compare) (ElemType, ElemType) {I = 1; // The initial value of I is the order p = L of the first element. elem; // The initial value of p is the storage location of the first element while (I <= L. length &&! (* Compare) (* p ++, e) ++ I; if (I <= L. length) return I; else return 0;} // Insert the data element e int ListInsert (SEQLIST * L, int I, Elemtype e) before the I Data Element in the linear table) {if (L. length> = L. listsize) exit (OVERFLOW); if (I <1 | I> L. length + 1) return error; // check whether the I value is reasonable q = & (L. elem [I-1]); // q is the position of the insert for (p = L. elem [Length-1]; p> = q; -- p) {* (p + 1) = * p ;} // move * q = e to the right of the entry and exit locations and the elements after the entry and exit; // insert e L. length ++; return OK;} // Delete the first data element in the linear table int ListDelete (SEQLIST * L, int I, Elemtype * e) {if (IsEmpty (L) return error; if (I <1 | I> L. length) 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; // The Position of the end element of the table for (p = p + 1; p <= q; ++ p) * (p-1) = * p; // move the element left after the element is deleted -- L. length; return OK ;}

Conclusion: The linear table represented by the sequential storage structure needs to move about half of the data elements on average during insert or delete operations. For linear tables with large length changes, enough storage space should be allocated at one time, but these spaces are often not fully utilized; the capacity of linear tables is difficult to expand.
Supplement: Merge sequence tables

Void MergeList_Sq (SqList La, SqList Lb, SqList & Lc) {pa = La. elem; pb = Lb. elem; Lc. listsize = Lc. length = La. length + Lb. length; pc = Lc. elem = (ElemType *) malloc (Lc. listsize * sizeof (ElemType); if (! Lc. elem) exit (OVERFLOW); pa_last = La. elem + La. length-1; pb_last = Lb. elem + Lb. length-1; while (pa <= pa_last & pb <= pb_last) {if (* pa <* pb) * pc ++ = * pa ++; else * pc ++ = * pb ++;} // Insert the remaining La element while (pa <= pa_last) * pc ++ = * pa ++; // Insert the remaining element of La while (pb <= pb_last) * pc ++ = * pb ++; // Insert the remaining element of Lb}

The algorithm complexity is O (La. length + Lb. length)
5. The linked list of a linear table indicates a linear list.
It is generally described using a linked list.



Advantage: it is convenient to add and remove new and delete operations without moving elements. disadvantage: it is inconvenient to randomly access elements because the pointer list must move the pointer.
Terms:

1) indicates that the information of each data element is combined and called a node;
2) the data element content is called the data field data.
3) The part of the direct successor element storage address is called the pointer or pointer field next
4) head is the head pointer. It points to the first node in the single-chain table. This is the entry point for single-chain table operations. The last node does not directly follow the node, so it is placed in NULL.
5) to simplify the operations on the linked list, a node is often attached to the first node of the linked list, which is called the header node.
Features:
1) the storage sequence of data elements in a linear table in the storage unit is not necessarily the same as that in the logical sequence. The data elements in a linear table are stored in any group of storage units (these storage units can be continuous, it can also be discontinuous)
2) When operating a linear table, you can only use the header pointer to enter the linked list, and scan the remaining nodes backward through the pointer field of each node, this will lead to time difference between finding the first node and finding the last node. An access method with this characteristic is called sequential access.
Reference code-typical operation implementation

// Defines the type of chain storage structure for linear tables. typedef struct linklist // node type {Elemtype elem; struct linklist * next;} LINKLIST; typedef struct // chain table type {LINKLIST * head;} LINK_LIST; // initialize the chain table int InitList (LINK_LIST * L) {L-> head = (* LINKLIST) malloc (sizeof (LINKLIST); // assign a storage unit to the header node if (L-> head) {L-> head-> next = NULL; return OK ;} return error;} // destroy the linked table void DestoryList (LINK_LIST * L) {LINKLIST * p; while (L-> head) // delete all nodes in the linked list in sequence {p = L-> head; L-> Head = L-> head-> next; free (p) ;}// clear the void ClearList (LINK_LIST * L) {LINKLIST * p; while (L-> head-> next) {p = L-> head-> next; // p points to the first node after the head node in the linked list L-> head-> next = p-> next; // Delete p node free (p ); // release the bucket occupied by the p node} // calculate the length of the linked list. int ListLength (LINK_LIST L) {LINK_LIST * p; int len; for (p = L-> head, len = 0; p-> next = NULL; p = p-> next, len ++) return len;} // judge whether the linked list is empty int IsEmpty (LINK_LIST L) {if (L-> head-> next = NULL) return True; return false;} // return the content of the I-th element in the linked list through e. int GetElem (LINK_LIST L, int I, Elemtype * e) {p = L-> next; j = 1; // initialization, p points to the first node, j is the counter while (p & j <I) // search backward clockwise, until p points to the I element or p is null {p = p-> next; ++ j;} if (! P | j> I) return ERROR; e = p-> elem; // obtain the I-th element return OK;} // The algorithm complexity is O (n) // retrieve the data element LINKLIST * LocateELem (LINK_LIST L, Elemtype e) {LINKLIST * p; for (p = L-> head-> next; p & p-> elem! = E; p = p-> next); return (p) ;}// return the direct precursor node LINKLIST * PriorElem (LINK_LIST L, LINKLIST * e) of node e in the linked list L) {LINKLIST * P; if (L-> head-> next = e) return NULL; // check as the first node for (p = L-> head; p-> next & p-> next! = E; p = p-> next); if (p-> next = e) return p; return NULL ;} // return the direct successor node LINKLIST * NextElem (LINK_LIST L, LINKLIST * e) {LINKLIST * p; for (p = L-> head-> next; p & p! = E; p = p-> next); if (p) p = p-> next; return p ;}

Supplement: Advanced Operations
1) insert a node

// Insert the Element e int ListInsert (LinkList * L, int I, ElemType) {p = L; j = 0 before position I in the single-link linear table L at the leading node; while (p & j <i-1) {p = p-> next; ++ j} // You Need To Find The I-1 node if (! P | j> i-1) return error; // I <1 or greater than the table length plus 1 s = (LinkList) malloc (sizeof (LNode )); s-> data = e; s-> next = p-> next; p-> next = s; return OK ;}

Complexity: O (n)
2) delete a node


// Delete the I-th element in the single-chain linear table L at the leading node, and e returns its value int LisyDelete (LinkList * L, int I, ElemType & e) {p = L; j = 0; while (p-> next & j <i-1) {p = p-> next; + + j; // find the I-th node and point p to its precursor} if (! (P-> next) | j> i-1) return error; q = p-> next; p-> next = q-> next; e = q-> elem; free (q); return OK ;}

Complexity: O (n)
3) merged ordered linked list

Void MergeList_L (LinkList & La, LinkList & Lb, LinkList & Lc) {pa = La-> next; pb = Lb-> next; Lc = pc = La; // use the La header node as the Lc header node while (pa & pb) // because the length of the linked list is hidden, modify the cycle condition {if (pa-> elem <= pb-> elem) {pc-> next = pa; // After the pc node is linked to the pc node, pc = pa; pa = pa-> next;} else {pc-> next = pb; pc = pb; pb = pb-> next ;}} pc-> next = pa? Pa: pb; free (fb );}

6. Circular linked list



The operation of the cyclic linked list is basically the same as that of the Linear Linked List. The difference is that the cyclic conditions in the algorithm are not p or p-> whether next is null, but whether they are equal to the header pointer.
7. Two-way linked list


Data Structure sequence table

I don't know if this program can meet your requirements. This program uses the sequence table method to delete the elements of a linear table, and then inserts the elements into the linear table, with a running diagram attached to it:
The procedure is as follows:
# Include <iostream>
Using namespace std;
# Include <malloc. h>
# Define LIST_INIT_SIZE 100
# Define LISTINCREMENT 10
# Define OVERFLOW-1
# Define OK 1
# Define ERROR 0
Typedef int Status;
Typedef int ElemType;
Typedef int KeyType;
Typedef struct {
ElemType * elem;
KeyType * key;
Int length;
Int listsize;
} SqList;
Typedef struct {
KeyType key;
} SElemType;
Status InitList (SqList & L) {/* defines a linear table */
Int length1;
Printf ("determine the sequence table length :");
Scanf ("% d", & length1 );
L. listsize = length1;
L. elem = (ElemType *) malloc (length1 * sizeof (ElemType ));
If (! L. elem ){
Printf ("out of space ");
Exit (OVERFLOW );
}
L. length = 0;
Return OK;
}

Status Listinsert (SqList & L, int I, ElemType e) {/* insert element e before element I */
ElemType * p, * q, * newbase;
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 ){
Printf ("out of space ");
Return (OVERFLOW );
}
L. listsize + = LISTINCREMENT;
}
P = & (L. elem [I-1]);
For (q = & (L. elem [L. length-1]); q> = p; q --){
* (Q + 1) = * q;
}
L. elem [I-1] = e;
L. length ++;

Return OK;
}
Status DeleteList (SqList & L, int I) {/* Delete I elements */
ElemType * q, * p;
If (I <1 | I> L. length ){
Return ERROR;
}
Q = & (L. elem [I-1]);
P = L... the remaining full text>


Data Structure table

1. Ignore it;
2. Repeat the questions.

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.