"Weiss", "No. 03 chapter" Exercise 3.2

Source: Internet
Author: User

"Exercise 3.2"

Give you a list L and another linked list p, which contain integers in ascending order. The action printlots (l,p) prints the elements of L in the position specified by P.

For example, if p=1,3,4,6, then the first, third, fourth, and sixth elements of L are printed.

You should only use basic table operations, how long is the process running?

Answer:

In the same way, the actual code of the folding is placed first.

1#include <iostream>2#include <string>3#include"Linklist.cpp"4 using namespacestd;5 using namespacelinklist;6Templateclasslist<unsignedint>;7Templateclasslist<string>;8 intMainvoid)9 {Tenlist<unsignedint>Number ; One  A     //test inserted in ascending order -cout <<"/*addinorder () */"<<Endl; -Number.addinorder ( One); theNumber.addinorder ( -); -Number.addinorder (7); -Number.addinorder ( -); -Number.addinorder (3); +Number.addinorder (2); -Number.addinorder (5); + number.traverse (); Acout <<"\n/*end*/\n\n"<<Endl; at  -list<string>Word; -cout <<"/*initialize*/"<<Endl; -Word.additem (" the"); -Word.additem (" Day"); -Word.additem (" After"); inWord.additem ("tommorow"); -Word.additem (" would"); toWord.additem (" be"); +Word.additem ("a"); -Word.additem ("Sunny"); theWord.additem ("FA;"); * word.traverse (); $cout <<"\n/*end*/\n\n"<<Endl;Panax Notoginseng  -     //test Printlots, print the 2,3,5,7 element of Word thecout <<"/*printlots () */"<<Endl; + word.printlots (number); Acout <<"\n/*end*/\n\n"<<Endl; the      +System"Pause"); -}
View Code

In the previous linked list routines

Http://www.cnblogs.com/catnip/p/4328889.html

After adding the following member function (the class member function declaration needs to be added by itself), the measured code can run correctly.

Because the list of conditions is ascending, so although no writing is required, there is a routine that is inserted automatically in ascending order in the list routine.

1 //Exercise 3.2 New, sequential insert2Template <typename t>BOOLList<t>::addinorder (ConstT &Item)3 {4node<t>* pnew =NewNode<t>(item);5node<t>* Curr =Front;6node<t>* prev =nullptr;7      while(Curr! = nullptr && Curr->data <Item)8     {9Prev =Curr;TenCurr = curr->Next; One     } A     //if the element is smaller than the head element, the head pointer points to the new node -     if(prev = =nullptr) -Front =pnew; the     //Otherwise, the last node that is not more than an element is found, and the node is inserted after -     Else -Prev->next =pnew; -     //Finally, the new node's back pointer joins the first node that is larger (or empty) than the new element +Pnew->next =Curr; -++length; +     return true; A}

Then, although the topic requires two linked list elements are integers, in fact, as long as the second list element is an integer can.

If the element of the first list is not an integer, then one instance of the class template accesses another instance, and a friend declaration is added.

1 // head node and linked list body operation 2 class List 3 {4     class List; 5         // ...... 6 }

Finally, the routines that are printed as required by the topic

1 //Exercise 3.2 New, the pit Daddy function ....2Template <typename t>voidList<t>::p rintlots (Constlist<unsignedint>&inorder)3 {4     //counter, records have been queried to when the original list of the first few elements5     //Unlike arrays, counters are calculated from "first"6UnsignedintCounter =1;7node<t>* scan =Front;8      for(node<unsignedint>* iter = Inorder.front; Iter! = nullptr; ITER = iter->next)9     {Ten         //traversing the auxiliary list, iter->data means "the original list is required to output its first ITER->DATA element" One         //returns directly when data is too large A         if(Iter->data >length) -             return; -         //iterate until Counter==index and print the          for(; counter < iter->data; + +)counter) -Scan = scan->Next; -cout << Scan->data <<ends; -     } +}

"Weiss", "No. 03 chapter" Exercise 3.2

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.