Summary of partial linked list operations

Source: Internet
Author: User
Tags droplist

Summary of partial linked list operations

#include <iostream>#include <cstdlib>using namespace Std;//Definition of Nodestruct node{intVal Node*next; Node (int x): Val (x),Next(NULL) {}};//Create a linklist with n nodesnode* Create (intN) {Node*head= New Node (Rand() % -); Node*pre= head; for(inti =1; I < n; i++) {Node*newnode= New Node (Rand() % -); Pre->Next= NewNode;    Pre = NewNode; }returnHead;} Insert node with value numnode* Insert (node*head,intnum) {Node*p= head; Node*q; while(P! = NULL && p->val < num) {Q= P; p = p->Next; } Node*newnode= new Node (num);if(p = = head) {newnode->Next= head;    head = NewNode; }Else if(p = = NULL) {Q-Next= NewNode; }Else{newnode->Next=Q-Next;Q-Next= NewNode; }returnHead;} //DeleteNode with value numnode* del (node*head,intnum) {Node*p= head; Node*q; while(P! = NULL && p->val! = num) {Q= P; p = p->Next; }if(p = = head) {head = head->Next;DeleteP }Else if(p = = NULL) {cout<<"Node could not found!"<<endl; }Else{Q-Next=Q-Next-Next;DeleteP }returnHead;}int length(Node*head){intLen =0; while(head! = NULL)        {++len; Head = head->Next; }returnLen;} //SortThe linklistnode*Sort(Node*head){intn =length(head); for(inti =0; I < n-1; i++) {Node*p= head; for(intj =0; J < N-i-1; J + +) {if(P->val > p->Next->val) {inttemp = p->val; P->val = p->Next->val; P->Next->val = temp; } p = p->Next; }    }returnHead;} //ReverseThe linklist Non-recursivenode*reverse(Node*head){if(head = = NULL) {returnHead } Node*pre= head; Node*cur= head->Next; while(cur! = NULL) {pre->Next= cur->Next; Cur->Next= head;        head = cur; Cur = pre->Next; }returnHead;} //ReverseThe linklist recursivelynode* reverse2 (Node*head){if(head = = NULL | | head->Next= = NULL) {returnHead } Node*newhead= Reverse2 (head->Next); Head->Next-Next= head; Head->Next= NULL;returnNewhead;} voidPrint(Node*head){ while(head! = NULL) {cout<" "; Head = head->Next; } Cout<<endl;} void Droplist (Node*head) {Node*p; while(head! = NULL) {p = head->Next;DeleteHead    head = p; }}intMain () {Node*head= Create (Ten);Print(head);Sort(head);Print(head); Head = insert (head,101);Print(head); Head = insert (head,-1);Print(head); Head = insert (head, -);Print(head); Head = Del (head,-1);Print(head); Head = del (head,101);Print(head); Head = del (head, -);Print(head); Head =Reverse(head);Print(head); Head = Reverse2 (head);Print(head); Droplist (head);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Summary of partial linked list operations

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.