#include<iostream>
#include<iomanip>
#include<math.h>
#include<conio.h>
#include<String.h>
#include<ctype.h>
#include <stdlib.h>
#include<windows.h>
using namespace std;
// , :
struct Node
{
char data; //
Node* next; //
};
int main()
{
if(SetConsoleCP(1251)==0)
{
cerr<<"Fialed to set codepage!"<<endl;
}
if(SetConsoleOutputCP(1251)==0)
{
cerr<<"Failed to set OUTPUT page!"<<endl;
}
// , :
Node *firstPt=NULL, *token=NULL, *prev=NULL;
cout<<" \n";
char ch, flag=1;
// :
do
{
ch = _getche(); //
token = new Node; //
token->data = ch; //
token->next = NULL; // 0
if (flag) //
{
firstPt = token; flag = 0; //
}
else prev -> next = token;
//
prev = token; //
} while (ch!= '\r'); // <Enter>
cout<<endl;
//:
cout <<":\n";
token = firstPt; //
while (token) // 0
{
cout<<token -> data; //
token = token->next; //
}
cout <<endl;
_getch();
return 0;
}
, m- , .
#include<iostream>
#include<iomanip>
#include<conio.h>
#include<String.h>
#include<ctype.h>
#include<stdlib.h>
#include<windows.h>
using namespace std;
// , :
struct Node
{
int num;
Node* ptr;
};
// , 1 :
void Add(Node*& pLast, int i)
/* , , , */
{
Node* pNext = new Node; //
if (pLast) pLast -> ptr = pNext;
// ,
pNext -> num = i; //
pNext -> ptr = NULL;
// ,
pLast = pNext; //
}
// -----------------------------------------------------------
// , m:
int Del(Node*& pToken, int m)
|
|
//
{
int n; //
if (pToken->ptr!= pToken) // -
{
// :
for (int i = 1; i < m; i++)
pToken = pToken->ptr; //
// :
Node* tmp = pToken->ptr;
n = tmp->num; //
pToken->ptr = tmp->ptr;
// ,
delete tmp; //
}
else // ,
{
n = pToken -> num;
delete pToken;
pToken = NULL;
}
return n;
}
//------------------------------------------------------------
int main()
{
if(SetConsoleCP(1251)==0)
{
cerr<<"Fialed to set codepage!"<<endl;
}
if(SetConsoleOutputCP(1251)==0)
{
cerr<<"Failed to set OUTPUT page!"<<endl;
}
do
{
int n, m;
do
{
cout << " >=1 --> ";
cin >> n;
} while (n < 1);
do
{
cout << " 1 "<< n << " --> ";
cin >> m;
} while (m < 1 || m > n);
// :
Node *pBegin = NULL, pLast = NULL;
// :
for (int i = 1; i <= n; i++)
{
Add(pLast, i);
if (i == 1) pBegin = pLast;
}
// :
pLast->ptr = pBegin;
// :
Node* pToken = pLast;
// :
do
{
cout<<"" << Del(pToken, m) << ' ';
} while (pToken);
cout << endl;
cout << "\n \n";
} while (_getch()!=27);
return 0;
}