.用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。

XX.h

 

#ifndef _LIST_H_
#define _LIST_H_
#include <string>
#include <iostream>
using namespace std;

struct Note
{
    string date;
    Note *next;
};

class List
{
public:
    List(void);
    ~List(void);
    void CreateNoteToList(bool flag,int count);
    void MakeList(bool flag,int num);
    void ShowNoteDate();
    void PutOutDate(int M);
private:
    Note *Head;
    Note *Tail;


};


#endif

 

 

XX.cpp

#include "StdAfx.h"
#include "List.h"

List::List(void)
:Head(NULL)
,Tail(NULL)
{
    Head = new Note();
    Head->date = "0";
    Head->next = NULL;

    Tail = Head;

}

List::~List(void)
{
}

void List::CreateNoteToList(bool flag,int num) //flag = true 头插法
{
    Note *TempNote = new Note();
    char buffer[10];
    memset(buffer,0,sizeof(buffer));
    string number = itoa(num,buffer,10);
    TempNote->date = number;
    TempNote->next = NULL;
  
    if(flag)
    {
        Note *Temp = Head->next;
        if(Temp == NULL)
        {
           Tail = TempNote;
           Head->next = TempNote;
           TempNote->next = Temp;
        }
        else
        {
            Head->next = TempNote;
            TempNote->next = Temp;
        }
         
    }
    else //尾插法
    {
        //Note *sp1;
        //sp1 = Tail;
        Tail->next = TempNote;
        Tail = TempNote;
 
    }
   
}


void List::MakeList(bool flag,int num)//flag = true 头插法
{
    Tail = Head;
    if(flag)
    {  
        for(int i=1;i<=num;i++)
        {
            CreateNoteToList(true,i);

        }
 

    }
    else
    {
        for(int i=1;i<=num;i++)
        {
            CreateNoteToList(false,i);
        }

    }
    Tail->next = Head;

}


void List::ShowNoteDate()
{
    Note *ps;
    ps = this->Head;
    while(ps != NULL && ps->next != Head)
    {  
        cout<<ps->date<<endl;
        ps = ps->next;

    }
}

void List::PutOutDate(int M)
{
    Note *cur = this->Head ;
    Note *pre = this->Tail;
    int count = M-1;
    while(cur != NULL&&cur != cur->next)
    {
        if(count)
        {
            --count;
            pre = cur;
            cur = cur->next;
        }
        else
        {
            pre->next = cur->next;
            cout<<"delect Note:"<<cur->date<<endl;
            delete cur;
            cur = pre->next;
            count = M-1;
        }
    }

    if(cur)
    {
        cout<<"delect Note:"<<cur->date<<endl;
        delete cur;
        Head = Tail = NULL;
    }


}

 

修改取余方法后:

void List::PutOutDate(int M)
{
    Note *cur = this->Head ;
    Note *pre = this->Tail;
    int count = 1;
   
    while(cur != NULL&&cur != cur->next)
    {
        pre = cur;
        cur = cur->next;
        int index = (count+1)%M;
        if(!index)
        {
            pre->next = cur->next;
            cout<<"delect Note:"<<cur->date<<endl;
            delete cur;
            cur = pre->next;
            count = 0;
           
        }
        ++count;
    }
    if(cur)
    {
        cout<<"delect Note:"<<cur->date<<endl;
        delete cur;
        Head = Tail = NULL;
    }

}

 

main.cpp

#include "stdafx.h"
#include <stdio.h>
#include "List.h"

int  main()
{
    List AppList;
    AppList.MakeList(false,10);
    AppList.ShowNoteDate();
    AppList.PutOutDate(2);

}

你可能感兴趣的:(.用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。)