Linked List (4) -- one-way linked list with table Header

Source: Internet
Author: User
1. One-way linked list with table Header
(1) A one-way linked list without a table header must differentiate the header knot and other nodes during insertion and deletion.
(2) benefits of using a one-way linked list with a table header: you do not need to consider the separate processing of the header node.
Header node: The data field has no value, and the pointer field points to the first node with values in the data field in the one-way linked list.

2. Basic operations on the one-way linked list of the header

# Include <stdio. h> # include <malloc. h> # define null0typedef struct node {int data; struct node * Next;} elemsn; elemsn * creat_link (int ms ); // create a void print_link (elemsn * head) linked list with table headers; // output one-way linked list void delete_node (elemsn * head, int X ); // Delete the node void insert_node (elemsn * head, int X); // Insert the node void clear_link (elemsn * head); // Delete the int main () {elemsn * head; int MS, X; printf ("Please input node number:"); scanf ("% d", & MS); H EAD = creat_link (MS); // create a linked list print_link (head); printf ("Please input delete node:"); scanf ("% d", & X ); delete_node (Head, x); // Delete the node print_link (head); printf ("Please input insert node:"); scanf ("% d", & X ); insert_node (Head, x); print_link (head); clear_link (head); print_link (head);} elemsn * creat_link (int ms) // The one-way linked list with table headers has the same creation method except the rest of the header nodes. Therefore, {elemsn * H = NULL, * P; int I, X does not need to be reverse created; H = P = (elemsn *) malloc (sizeof (elems N); for (I = 0; I <MS; I ++) {P-> next = (elemsn *) malloc (sizeof (elemsn )); // create a new node printf ("Please input node data:"); scanf ("% d", & X); P-> next-> DATA = x; // new node initialization p-> next = NULL; // new node initialization P = p-> next;} return h;} void print_link (elemsn * head) {If (null = head-> next) {printf ("link is null. ") ;}for (Head = head-> next; head = head-> next) {printf (" % d ", head-> data );} printf ("\ n");} void delete_node (elemsn * H EAD, int X) {elemsn * P, * q; For (P = head, q = head-> next; Q & Q-> data! = X; P = Q, q = Q-> next) {} If (Q! = NULL) {P-> next = Q-> next; free (q) ;}} void insert_node (elemsn * head, int X) {elemsn * P, * q; if (null = head) return; If (null = head-> next) // when the linked list is empty {head-> next = (elemsn *) malloc (sizeof (elemsn); head-> next = NULL; head-> next-> DATA = x; return;} p = head; Q = head-> next; If (null = Q-> next) // when there is only one element in the linked list {P-> next = (elemsn *) malloc (sizeof (elemsn); P-> next-> DATA = x; P-> next = Q; return ;} if (Q-> DATA> = Q-> next-> data) // decrease the linked list {for (; Q & Q-> DATA> = x; P = Q, Q = Q-> next) {}} else // incremental linked list {for (; Q & Q-> data <= x; P = Q, Q = Q-> next) {}} if (null = q) // Insert at the end of the linked list {q = (elemsn *) malloc (sizeof (elemsn )); q-> DATA = x; q-> next = NULL; P-> next = Q;} else if (q = head-> next) // insert at the beginning of the table {q = (elemsn *) malloc (sizeof (elemsn); q-> DATA = x; q-> next = head-> next; head-> next = Q;} else // insert in the middle position {P-> next = (elemsn *) malloc (sizeof (elemsn )); p-> next = Q; P-> next-> DATA = x ;}} void clear_link (elemsn * head) {elemsn * P; while (Head-> next) {P = head-> next; head-> next = p-> next; free (p );}}


Linked List (4) -- one-way linked list with table Header

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.