Sequence Table-single-chain table

Source: Internet
Author: User

Single-chain table:

Typedef struct lnode {elemtype data;/* data field, save the value of the node */struct lnode * Next;/* pointer field */} lnode, * linklist; /* node type */

Table creation:

1) insert the table in the header: Each inserted node serves as the first node of the linked list. If n knots are inserted when a single linear linked list is created, the time complexity of the algorithm is O (n ).

Void create_linklist (linklist & L, int N)/* Insert a group of data in reverse order */{lnode * P, * q; L = (linklist) malloc (sizeof (lnode); L-> next = NULL;/* create a single-chain table of the leading node L */for (I = N; I> 0; I --) {P = (linklist) malloc (sizeof (lnode); scanf (& P-> data);/* data field assignment */p-> next = L-> next; l-> next = P;/* insert to the header */}}

2) Insert table at the end: Insert the new node to the end of the table of the current linked list to make it the end node of the current linked list.

Void * create_linklist (linklist & L, int N)/* create a single-chain table by means of end insertion */{lnode * P, * q; L = P = (linklist) malloc (sizeof (lnode); P-> next = NULL;/* Create the header node L */for (I = 1; I <= N; I ++) {q = (linklist) malloc (sizeof (lnode); scanf (& Q-> data ); /* data field assignment */Q-> next = p-> next; P-> next = Q; P = Q;/* insert to the end of the table */}}

Search:

1) Search for the I element in the list receiving list by sequence number

Status get_elem (lnode * l, int I, elemtype & E) {Int J; lnode * P; P = L-> next; j = 1; /* point P to the first node */while (P & J <I) {P = p-> next; j ++;}/* Move pointer P, j count */If (! P | j> I) Return Error; E = p-> data; Return OK;} frequency of moving pointer P: I <1 hour: 0 times; I, [1, n]: I-1 times; I> N: n times. Time complexity of compaction: O (n ).

2) search by value: the Algorithm Execution is related to the parameter key. The average time complexity is O (n)

Elemtype * locate_node (lnode * l, elemtype key) /* Find the first node with the key value in the single-chain table with the L header node */{lnode * P = L-> next; while (P! = NULL & P-> data! = Key) P = p-> next; If (p-> DATA = Key) return P; else {printf ("the node to be searched does not exist !! \ N "); retutn (null );}}

Insert: (the algorithm time mainly consumes the moving pointer P, so the time complexity is O (n )).

Status insert_lnode (linklist & L, int I, elemtype E) /* Insert a node with a value of E at the I position of a single-chain table with the L header node */{Int J = 0; lnode * P, * q; P = L-> next; while (P & J <i-1) {P = p-> next; j ++;} If (! P | j> i-1) {printf ("I is too large or I is 0 !! \ N "); Return Error;} q = (linklist) malloc (sizeof (lnode); q-> DATA = E; q-> next = p-> next; p-> next = Q; Return OK ;}

Delete:

1) the time complexity of deleting an algorithm by serial number is O (n ).

Status delete_linklist (lnode * l, int I, elemtype & E)/* deletes the I node in the single-chain table with the L header node, the deletion value */{Int J = 1; lnode * P, * q; P = L; q = L-> next; is returned by E; while (p-> next & J <I)/* Find the I-1 node */{P = p-> next; j ++;} If (! (P-> next) | j> I) {printf ("I is too large or I is 0 !! \ N "); Return Error;} q = p-> next; P-> next = Q-> next; E = Q-> data; free (Q ); return OK ;}

2) The execution of the value-based deletion algorithm is related to the parameter key. The average time complexity is O (n)

Void delete_linklist (lnode * l, int key)/* Delete the first node whose values in a single-chain table with L as the header node are key */{lnode * P = l, * q = L-> next; while (Q & Q-> data! = Key) {P = Q; q = Q-> next;} If (Q-> DATA = Key) {P-> next = Q-> next; free (Q);} else printf ("the node to be deleted does not exist !! \ N ");}

3) delete all nodes whose values are key in a single-chain table.

Void delete_linklist_node (lnode * l, int key)/* Delete the first node whose values in the single-chain table with the L header node are the key */{lnode * P = l, * q = L-> next; while (Q! = NULL) {If (Q-> DATA = Key) {P-> next = Q-> next; free (Q); q = p-> next ;} else {P = Q; q = Q-> next ;}}}

4) delete all nodes with duplicate values in a single-chain table so that the values of all nodes are different.

Void delete_node_value (lnode * l)/* Delete all nodes with the same values in a single-chain table with the L header node */{lnode * P = L-> next, * Q, * PTR; while (P)/* Check all nodes in the linked list */{* q = P, * PTR = p-> next; /* check all the subsequent nodes of node p ptr */while (PTR) {If (PTR-> DATA = p-> data) {q-> next = PTR-> next; free (PTR); PTR = Q-> next;} else {q = PTR; PTR = PTR-> next ;}} P = p-> next ;}}

Merge a single-chain table: If the lengths of the two linked lists la and lB are M and N, the time complexity of the chain table merging is O (m + n)

Void merge_linklist (linklist & la, linklist & Lb, linklist & lc)/* merge to a non-descending ordered single-chain table la, lb to the ordered single-chain table LC */{lnode * pA, * pb, * PC, * PTR; Pa = La-> next; Pb = LB-> next; lc = pc = La; while (PA & Pb) {If (Pa-> data <= Pb-> data) {PC-> next = PA; Pc = PA; Pa = pa-> next ;} /* merge the nodes referred to by PA. Pa points to the next node */else {PC-> next = Pb; Pc = Pb; Pb = Pb-> next ;} /* merge the nodes referred to by Pb, and point Pb to the next node */} PC-Next = pa? PA: Pb; free (LB );}

Sequence Table-single-chain table

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.