C ++ Program Design Practice Guidance 1.13 realization of requirements for modifying natural data sets, and programming practice 1.13

Source: Internet
Author: User

C ++ Program Design Practice Guidance 1.13 realization of requirements for modifying natural data sets, and programming practice 1.13

Rewrite requirement 1: using a single-chain table

Rewrite requirement 2: delete the linked list node in sequence in the destructor

#include <cstdlib>#include <iostream>using namespace std;struct LinkNode{       int data;       LinkNode* next;};class NOPRIME{      friend struct LinkNode;      LinkNode* Head;      int n;      public:            NOPRIME(int n1)            {               n=n1;            }              void creat();            int yes(int x)            {                for(int i=2;i<x/2;i++)                   if(x%i==0)                      return 1;                return 0;            }            void Search();            void print()            {                 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);               }            }};void NOPRIME::creat(){   Head=new LinkNode;   Head->next=NULL;   LinkNode* p=Head;   int i=n;   while(i)   {           LinkNode* newLinkNode=new LinkNode;           newLinkNode->next=NULL;           p->next=newLinkNode;           p=newLinkNode;           i--;   }}void NOPRIME::Search(){     LinkNode* p;     int i,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;    }} int main(int argc, char *argv[]){    NOPRIME num(10);    num.creat();    num.Search();    num.print();    return EXIT_SUCCESS;}

 

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.