Queue list implementation and consolidation of ordered tables

Source: Internet
Author: User

#include <iostream>#include<algorithm>using namespaceStd;typedefstructnode{intdata; Node*Next;} Lnode,*linklist;voidCreat (Linklist &L) {L=NewNode; L->next =NULL; cout<<"you have successfully created a queue"<<Endl;}voidInsert (linklist &l,intN) {cout<<"Please enter"<< N <<"an element that needs to be inserted"<<Endl; Lnode*p, *cur; Cur=L;  for(inti =0; I < n; i++) {p=NewLnode; CIN>> p->data; P->next =NULL; Cur->next =p; Cur=p; } cout<<"Create End"<<Endl;}voidPop (Linklist &m) {    if(L->next = =NULL) {cout<<"pop-up error"<<Endl; return; } Lnode*p, *Q; P= l->Next; Q= p->Next; L->next =Q; cout<<"the top element of the team is"<< P->data <<Endl; Delete p;}intMain () {linklist L;    Creat (L); cout<<"Input Insertion Length"<<Endl; intN; CIN>>N;    Insert (L, N);  while(l->next)    {Pop (L); }     return 0;}

#include <iostream>using namespaceStd;typedefstructLnode//defining a single-linked list{    intdata; structLnode *Next;} Lnode,*linklist;voidinitlist_l (lnode* &l)//Create a single linked list{L=NewLnode; L->next=NULL;}voidInput (linklist &l,intN//enter data into single-linked list L{    inti;    Linklist p,r; R=m; cout<<"Please enter the elements of the table (in non-descending order):";  for(i=0; i<n;i++) {p=NewLnode; CIN>>p->data; P->next=NULL; R->next=p; R=p; }}voidOutput (linklist L)//output Each element of the single-link table sequentially{    intI=0; Lnode*p; P=l->Next;  while(p) {if(i) cout<<","; cout<<p->data; P=p->Next; I=1; }}voidmergelist_l (linklist &la,linklist &lb,linklist &AMP;LC)//algorithm 2.16 Consolidation of chained ordered tables{    //elements of the single-linked list LA and LB are known to be non-descending by value//merge LA and lb to get new single-linked list LC,LC elements are also non-descending by value//fill in the blanks belowLnode *p, *q, *cur, *temp; P= la->Next; Q= lb->Next; Cur=LC;  while(P &&q) {        if(P->data <= q->data) {Temp=NewLnode; Temp->data = p->data; Temp->next =NULL; Cur->next =temp; Cur=temp; P= p->Next; }        Else{Temp=NewLnode; Temp->data = q->data; Temp->next =NULL; Cur->next =temp; Cur=temp; Q= q->Next; }    }    if(p) {cur->next =p; }    if(q) {cur->next =Q; }}    //mergelist_l ()intMain () {linklist la,lb,lc; intNum_a,num_b; cout<<"Please enter the number of non-descending single-linked list a n:"; CIN>>Num_a;                                                    initlist_l (La); //creation of a la tableInput (LA,NUM_A);//enter data into single-linked table Lacout<<"Please enter the number N of non-descending single-linked list B:"; CIN>>Num_b;                                                    initlist_l (LB); //creation of LB tablesInput (Lb,num_b);//enter data into single-linked table Lainitlist_l (LC);                                            mergelist_l (LA,LB,LC); //combine single-linked list LA and lbcout<<"Non -descending single-linked list A, a combined non-descending single-linked list C is: \ n";//output combined single-linked list LCoutput (LC); cout<<Endl; return 0;}

Queue list implementation and consolidation of ordered tables

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.