Data structure (C implementation) ------- single-chain table

Source: Internet
Author: User

In a single-chain table, each node contains two parts: the data domain that stores the information of each data element and the pointer domain that stores its direct subsequent storage location.

Type description of a Single-linked table node:

typedef int ElemType;typedef struct node{ElemType data;struct node *next;}LNode,*LinkList;

The access to a single-chain table must start with a single-chain pointer. Therefore, the header pointer is usually used to identify a single-chain table. If the header pointer is null, the linked list is empty. Sometimes, a node is attached to the first node of a single-chain table, which is called a header node.

Single-chain table operations:

1. init_linklist () initialization of a single-chain table ()

The initialization of a single-chain table is to create an empty linked list of the leading node.

// Initialize the linklist init_linklist () {linklist L; L = (linklist) malloc (sizeof (lnode); L-> next = NULL; return l ;}

2. Find the length of the single-chain table length_linklist (linklist L)

// Calculate the long run of the table int length_linklist (linklist L) {linklist P; int length = 0; P = L-> next; // P points to the first node while (P) {length ++; P = p-> next;} return length ;}

3. Search for a single-chain table locate_linklist_byseq (linklist L, int I) by serial number)

Starting from the head pointer of the linked list, scan one by one until the I node is scanned.

// Locate the linklist locate_linklist_byseq (linklist L, int I) {linklist P = L; Int J = 0; while (j <I & P-> next) {P = p-> next; j ++;} If (j = I) return P; elsereturn NULL ;}

4. Search for a single-chain table locate_linklist_byvalue (linklist L, elemtype e) by value)

Starting from node I of a single-chain table, scan the single-chain table sequentially and compare the node value with node e until a node equal to node e is found. The pointer of the node is returned; otherwise, if no such node is found in the entire linked list, a null pointer is returned.

// Locate the linklist locate_linklist_byvalue (linklist L, elemtype e) {linklist P = L-> next; while (P! = NULL & P-> data! = E) {P = p-> next;} return P ;}

5. The insertafter (linklist P, elemtype e) operation after inserting the new node * s into the node * P)

// After the new node is inserted to the specified node, void insertafter (linklist P, elemtype e) {linklist s; S = (linklist) malloc (sizeof (lnode )); s-> DATA = E; s-> next = p-> next; P-> next = s ;}

6. Insert the new node * s into the node * P before insertbefore (linklist L, linklist P, elemtype E)

// Void insertbefore (linklist L, linklist P, elemtype e) {linklist S, Q; S = (linklist) malloc (sizeof (lnode) before the new node is inserted to the specified node )); s-> DATA = E; q = L; // locate the P precursor node while (Q-> next! = P) q = Q-> next; s-> next = P; q-> next = s ;}

7. Insert a new node before the I node of a single-chain table

// Void insertbefore_seqi (linklist L, int I, elemtype e) {linklist P, S; // locate the I-1 node P = locate_linklist_byseq (L, I-1); If (P = NULL) printf ("I location invalid"); else {S = (linklist) malloc (sizeof (lnode); s-> DATA = E; s-> next = p-> next; P-> next = s ;}}

8. Insert a new node after the I node of a single-chain table

// Insert the new node to the I-th node and then void insertafter_seqi (linklist L, int I, elemtype e) {linklist P, S; // locate the I-th node P = locate_linklist_byseq (L, I); If (P = NULL) printf ("I location illegal"); else {S = (linklist) malloc (sizeof (lnode); s-> DATA = E; s-> next = p-> next; P-> next = s ;}}

9. Delete the successor node deleteafter_nodep (linklist P) of * P)

// Delete * P node's successor node void deleteafter_nodep (linklist p) {linklist s; If (p-> next = NULL) printf ("the successor node of the current node is blank \ n"); else {S = p-> next; P-> next = s-> next; free (s );}}

10. Delete * P node deletenodep (linklist L, linklist P)

// Delete the specified node void deletenodep (linklist L, linklist p) {linklist Q; q = L; // find the * P precursor node while (Q-> next! = P) q = Q-> next; q-> next = p-> next; free (p );}

11. Delete the I node delete_nodei (linklist L, int I) of the single-chain table)

// Delete the I node void delete_nodei (linklist L, int I) {linklist Q, P; // find the I node q = locate_linklist_byseq (L, i-1); If (q = NULL) printf ("The I-1 node does not exist \ n"); else {P = Q-> next; q-> next = p-> next; free (p );}}

12. Delete all nodes with values of E in the single-chain table and return the number of nodes with values of E. delete_node_valuee (linklist L, elemtype E)

// Delete all nodes with values of E in the single-chain table and return the number of nodes with values of E. Int delete_node_valuee (linklist L, elemtype e) {linklist Q, P; int COUNT = 0; Q = L; while (Q-> next! = NULL) {P = Q-> next; If (p-> DATA = e) {q-> next = p-> next; free (p );} elseq = P;} return count ;}

13. output single-chain table print_linklist (linklist L)

// Print the linked list void print_linklist (linklist L) {linklist P = L-> next; while (p) {printf ("% d \ t", p-> data ); P = p-> next;} printf ("\ n ");}

14. Create a single-chain table create_linklistf (int n) using the header Insertion Method)

// Create the linklist create_linklistf (int n) {linklist L, s; int I, X; L = (linklist) malloc (sizeof (lnode )); l-> next = NULL; for (I = N; I> 0; I --) {scanf ("% d", & X); s = (linklist) malloc (sizeof (lnode); s-> DATA = x; s-> next = L-> next; L-> next = s;} return l ;}

15. Create a single-chain table create_linklistr (int n) by using the end-Insertion Method)

// Create the linklist create_linklistr (int n) {linklist L, S, P; int I, X; L = (linklist) malloc (sizeof (lnode )); P = L; for (I = N; I> 0; I --) {scanf ("% d", & X); s = (linklist) malloc (sizeof (lnode); s-> DATA = x; P-> next = s; P = s;} p-> next = NULL; return l ;}












 


 


 

 


 

Data structure (C implementation) ------- single-chain table

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.