Single-chain table

Source: Internet
Author: User
Struct node {int data; node * Next;}; // create a linked list and enter a number as the input. If the input is 0, the linked list ends (0 is excluded) node * creat () {node * head, * P, * s; int X, cycle = 1; head = (node *) malloc (sizeof (node); P = head; while (cycle) {cout <"Please input the data:"; CIN> X; If (X! = 0) {S = (node *) malloc (sizeof (node); s-> DATA = x; P-> next = s; P = s ;} else {cycle = 0 ;}} head = head-> next; P-> next = NULL; return head ;}


// Calculate the length of the linked list int computlength (node * head) {node * P; int n = 0; If (Head = NULL) {return 0;} p = head; while (P! = NULL) {n ++; P = p-> next;} return N ;}



// Print the data void printlist (node * head) {node * P; If (Head = NULL) {cout <"the list has no value! "<Endl; return;} p = head; while (P! = NULL) {cout <p-> data <Endl; P = p-> next ;}}

// The node whose data is num may be the first node or another node * delenode (node * head, int num) {node * P1, * P2; P1 = head; while (P1-> data! = Num & P1-> next! = NULL) {P2 = p1; P1 = p1-> next;} If (P1-> DATA = num) {If (p1 = head) // Delete the header node {head = p1-> next; Delete P1;} else // delete other nodes {P2-> next = p1-> next ;}} else cout <"not find the num! "<Endl; return head ;}



// Delete the linked table void delelist (node * head) {node * P; P = head; while (P! = NULL) {node * temp = P; P = p-> next; Delete temp;} cout <"delete list success" <Endl ;}

// Insert nodes into the linked list. In the three cases, the End Node * insertnode (node * head, int num) {node * Pre, * inser, * NEX; inser = (node *) malloc (sizeof (node); inser-> DATA = num; NEX = head; while (NEX-> data <inser-> Data & NEX-> next! = NULL) {pre = NEX; NEX = NEX-> next;} If (inser-> data <= NEX-> data) // determine whether data is inserted before the last node (including the last node) {If (NEX = head) // before the insert header {head = inser; inser-> next = NEX;} else // Insert the center {pre-> next = inser; inser-> next = NEX ;}} else // later than the last element inserted at the end {NEX-> next = inser; inser-> next = NULL;} return head ;}

 

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.