Single-chain table operations

Source: Internet
Author: User

Single-chain table operations

In order to prepare for the test, I wrote several single-chain table operations, including chain creation, chain deletion, insertion of nodes, deletion of nodes, and reverse order of linked lists.

Struct node {int data; struct node * Next;}; struct node * create_list () {struct node * P, * q, * h; int I; P = H = (struct node *) malloc (sizeof (struct node); H-> DATA = 6; for (I = 0; I <5; I ++) {q = (struct node *) malloc (sizeof (struct node); q-> DATA = I; P-> next = Q; P = p-> next; Q = NULL;} return h;} void free_list (struct node * h) {struct node * P = H; struct node * q; while (p) {q = P; P = p-> next; free (q) ;}} void PRI Nt_list (const struct node * h) {struct node * P = H; while (p) {printf ("% d", p-> data ); P = p-> next;} printf ("\ n ");} /* Insert a node with the value of data at the POs node */struct node * insert_elem (struct node * H, int POs, int data) {struct node * P, * q; P = H; q = (struct node *) malloc (sizeof (struct node); q-> DATA = data; /* insert to 0th locations */If (Pos = 0) {q-> next = H; H = Q ;} else {/* insert to position after 0 */while (-- POS)/* locate to the previous node of the insertion point */P = p-> N EXT; q-> next = p-> next; P-> next = Q;} return h ;} /* Delete the second POS node */struct node * delete_pos (struct node * H, int POS) {struct node * P, * q; q = NULL; P = h; /* determine whether the linked list is empty */If (! H) return NULL;/* Delete the 0th nodes */If (Pos = 0) {H = H-> next;} else {While (pos --) {/* locate the POs node */q = P; P = p-> next;} Q-> next = p-> next;} Free (P ); return h;}/* Delete the first node with data in the linked list */struct node * delete_elem (struct node * H, int data) {struct node * P, * Q, * t; q = P = H;/* determine whether the linked list is empty */If (! H) return NULL;/* in the traversal table, locate the node with the value of data */while (p-> data! = Data & P-> next) {q = P; P = p-> next;} If (p-> DATA = data) {If (P = H) /* determine whether the node is a header node */h = H-> next; elseq-> next = p-> next; free (p);} return h ;} /* reverse list */struct node * reverse_list (struct node * h) {struct node * P, * t, * q; P = H; t = q = NULL; while (p) {T = p-> next; P-> next = Q; q = P; P = T;} return Q ;}

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.