Doubly linked list (non-circular) related operations: Create, destructor, delete, bubble sort

Source: Internet
Author: User

structdulnode{intVal; Dulnode*Pre; Dulnode*next;};//This is not a double-loop linked list.dulnode* Create_dulnode (intN) {    if(N <=0)returnNULL; inti =0; Dulnode*head, *P1, *P2; //Build head NodeHead =NewDulnode; Head->val = rand ()%Rand_max; Head->pre = NULL;//the head node precursor node is emptyP1 =Head;  for(i =1; I < n; i++) {P2=NewDulnode; P2->val = rand ()%Rand_max; P2->pre =P1; P1->next =P2; P1=P2; } p1->next =NULL; returnhead;}voidDel_dulnode (dulnode* &head,Const intnum) {    //The linked list is empty    if(NULL = = head)return; Dulnode*temp, *p; //Delete head node     while(head! = Null&&num = = head->val) {Temp=Head; Head= head->Next; Head->pre =NULL; Deletetemp; cout<<"To Delete a head node:"<< Num <<Endl; } P=Head;  while(P! =NULL) {        if(num = = p->val) {Temp=p; P->pre->next = temp->Next; P= temp->Next; Deletetemp; cout<<"To Delete a node:"<< Num <<Endl; }        Else{p= p->Next; }    }}voidErase_dulnode (dulnode*head) {    if(NULL = = head)return; Dulnode*temp, *p =Head;  while(p) {Temp=p; P= p->Next; Deletetemp; }}voidPrint_dulnode (dulnode*head) {    if(NULL = = head)return; inti =0; Dulnode*p =Head;  while(p) {cout<<" "<< P->val <<","; P= p->Next; if(5== ++i) {i=0; cout<<Endl; }} cout<<Endl;}intGetdulnodelistlen (dulnode*head) {    if(NULL = = head)return 0; intn =0; Dulnode* p=Head;  while(p) {++N; P= p->Next; }    returnN;}voidSortdulnodelist (dulnode*head) {    //empty double-linked list or single-node doubly linked list does not operate    if(NULL = = Head | | NULL = = Head->next)return; BOOLSign =false; intI, j, len =Getdulnodelistlen (head), temp; Dulnode*p;  for(i = len-1; i >0; i--) { sign=false; P=Head;  for(j =0; J < I; J + +)        {            if(P->val > p->next->val) {Temp= p->Val; P->val = p->next->Val; P->next->val =temp; Sign=true; } P= p->Next; }        if(!sign) Break;//This loop has no value exchange and the sorting is complete    }}

Doubly linked list (non-circular) related operations: Create, destructor, delete, bubble sort

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.