C + + Programming Practice Guide 1.13 Finding composite rewriting requirements in natural number set implementation

Source: Internet
Author: User

Rewrite requirement 1: Implemented with single-linked list

Overwrite Requirement 2: Delete the linked list node in turn in the destructor

#include <cstdlib>#include<iostream>using namespacestd;structlinknode{intdata; Linknode*next;};classnoprime{friendstructLinknode; Linknode*Head; intN;  Public: Noprime (intN1) {N=N1; }              voidcreat (); intYesintx) { for(intI=2; i<x/2; i++)                   if(x%i==0)                      return 1; return 0; }            voidSearch (); voidprint () {Linknode* p=head->Next;  while(p) {cout<<p->data<<'\ t'; P=p->Next; } cout<<Endl; }            ~Noprime () {Linknode* p=Head;  while(Head) {p=Head; Head=head->Next;               Delete (p); }            }};voidnoprime::creat () {Head=NewLinknode; Head->next=NULL; Linknode* p=Head; intI=N;  while(i) {Linknode* newlinknode=NewLinknode; Newlinknode->next=NULL; P->next=Newlinknode; P=Newlinknode; I--; }}voidNoprime::search () {Linknode*p; inti,j,t=1;  for(i=3;; i++) {T=1; P=head->Next;  for(j=i;j<i+n;j++) {p->data=J; P=p->Next; if(yes (j) = =0) T=0; }    if(t==1)        Break; }} intMainintargcChar*argv[]) {Noprime num (Ten);    Num.creat (); Num.    Search ();    Num.print (); returnexit_success;}

C + + Programming Practice Guide 1.13 Finding composite rewriting requirements in natural number set implementation

Related Article

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.