#ifndef MYITERATOR_H
#define MYITERATOR_H
template <class T> struct _list_iterator;
template <class T> struct _list_const_iterator;
template <class T>
struct _list_node
{
//typedef void* void_pointer;
_list_node* pre;
_list_node* next;
T data;
};
template <class T>
struct _list_iterator
{
//typedef
typedef _list_iterator iterator;
typedef _list_const_iterator const_iterator;
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef _list_node<T>* link_type;
// typedef ptrdiff_t difference_type;
//
link_type node;
//
_list_iterator(link_type x):node(x){}
_list_iterator(){}
//
bool operator==(const iterator& it){return node==it.node;}
bool operator!=(const iterator& it){return node!=it.node;}
//
reference operator *() const{return (*node).data;}
//
iterator& operator ++()
{
node=(link_type)(node->next);
return *this;
}
iterator& operator ++(int)
{
iterator tmp=*this;
++*this; //调用上面定义的前自增
return tmp;
}
iterator& operator --()
{
node=(link_type)(node->pre);
return *this;
}
iterator& operator --(int)
{
iterator tmp=*this;
--*this;
return tmp;
}
};
template <class T>
struct _list_const_iterator
{
//typedef
typedef _list_iterator iterator;
typedef _list_const_iterator const_iterator;
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef _list_node<T>* link_type;
// typedef ptrdiff_t difference_type;
//
link_type node;
//
_list_const_iterator(link_type x):node(x){}
_list_const_iterator(){}
//
bool operator==(const const_iterator& it){return node==it.node;}
bool operator!=(const const_iterator& it){return node!=it.node;}
//
const_reference operator *() const{return (*node).data}
//
const_iterator& operator ++()
{
node=(link_type)(node->next);
return *this;
}
const_iterator& operator ++(int)
{
iterator tmp=*this;
++*this; //调用上面定义的前自增
return tmp;
}
const_iterator& operator --()
{
node=(link_type)(node->pre);
return *this;
}
const_iterator& operator --(int)
{
iterator tmp=*this;
--*this;
return tmp;
}
};
#endif
#include"myIterator.h"
#include<memory>
#include<iostream>
using namespace std;
template <class T>
class list
{
protected:
typedef void* void_pointer;
typedef _list_node<T> list_node;
public:
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef list_node* link_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _list_iterator<T> iterator;
typedef _list_const_iterator<T> const_iterator;
protected:
link_type create_node()
{
link_type tmp=(link_type)malloc(sizeof(list_node));
tmp->pre=tmp;
tmp->next=tmp;
return tmp;
}
link_type create_node(value_type x)
{
link_type tmp=(link_type)malloc(sizeof(list_node));
tmp->data=x;
tmp->pre=tmp;
tmp->next=tmp;
return tmp;
}
void destroy_node(link_type p)
{
free(p);
p=NULL;
}
protected:
link_type node;
size_type length;
public:
list():length(0)
{
node=create_node();
}
list(value_type x,size_type len):length(0)
{
node=create_node();
push_back(x);
}
list(iterator first,iterator last):length(0)
{
node=create_node();
insert(end(),first,last);
}
list(list<T>& x):length(0)
{
node=create_node();
link_type tmp=x.node->next;
while(tmp!=x.node)
{
push_back(tmp->data);
tmp=tmp->next;
}
}
~list()
{
//for(link_type p=node->next;p!=node;p=p->next)
// free(p);
if(node!=NULL){
link_type p=node->next,q=NULL;
while(p!=node)
{
q=p->next;
free(p);
p=q;
}
p=NULL;
q=NULL;
free(node);
node=NULL;//free()过后,指针赋NULL,free只是将node指向的内存内存置为垃圾
length=0;
}
}
public:
iterator begin(){return (link_type)(node->next);}
// const_iterator begin(){return (link_type)(node->next);}
iterator end(){return node;}
// const_iterator end(){return node;}
bool empty(){return 0==length;}
void reverse_node(link_type ptr)
{
link_type tmp=NULL;
tmp=ptr->next;
ptr->next=ptr->pre;
ptr->pre=tmp;
tmp=NULL;
}
size_type get_length(){return length;}
size_type distance(iterator first,iterator last)
{
size_type n=1;
for(iterator it=first;it!=last;++it)
++n;
return n;
}
/*link_type find(const value_type& x)
{
link_type tmp=node->next;
while(x!=tmp->data&&node!=tmp)
tmp=tmp->next;
if(node==tmp)
return NULL;
return tmp;
}*/
void clear()
{
}
/* void insert(value_type x,int pos)
{
if(pos<=0||pos>length+1)
return;
link_type tmp=(link_type)malloc(sizeof(list_node));
tmp->data=x;
tmp->pre=tmp;
tmp->next=tmp;
if(node==node->pre)
{
node->next=tmp;
node->pre=tmp;
tmp->next=node;
tmp->pre=node;
//return;
}
else
{
int index=0;
link_type pos_pointer=node;
while(index<pos)
{
index++;
pos_pointer=pos_pointer->next;
}
pos_pointer->pre->next=tmp;
tmp->pre=pos_pointer->pre;
tmp->next=pos_pointer;
pos_pointer->pre=tmp;
}
length++;
}
void push_back(value_type x)
{
insert(x,length+1);
}*/
iterator insert(iterator pos,value_type x)
{
link_type tmp=(link_type)malloc(sizeof(list_node));
tmp->data=x;
tmp->pre=tmp;
tmp->next=tmp;
/* if(node->next==node)
{
node->pre=tmp;
node->next=tmp;
tmp->pre=node;
tmp->next=node;
}
else*/
{
pos.node->pre->next=tmp;
tmp->pre=pos.node->pre;
pos.node->pre=tmp;
tmp->next=pos.node;
}
++length;
return tmp;
}
iterator insert(iterator pos,iterator it)
{
it.node->pre=pos.node->pre;
it.node->next=it.node;
pos.node->pre->next=it.node;
pos.node->pre=it.node;
++length;
return it;
}
void insert(iterator pos,iterator first,iterator last)//深拷贝插入
{
// first.node->pre=pos.node->pre;
// last.node->next=pos.node;
// pos.node->pre->next=first.node;
// pos.node->pre=last.node;
iterator tmp=first;
while(tmp!=last)
{
insert(pos,*tmp);
//++length;
++tmp;
}
insert(pos,*tmp);
// ++length;
}
void push_back(value_type x)
{
insert(end(),x);
}
void pop_back()
{
erase(--end());
}
/* void push_back(value_type x)
{
link_type tmp=(link_type)malloc(sizeof(list_node));
tmp->data=x;
//link_type p=node->pre;
//p->next=tmp;
node->pre->next=tmp;
tmp->pre=node->pre;
node->pre=tmp;
tmp->next=node;
length++;
}*/
void erase(iterator it)
{
it.node->pre->next=it.node->next;
it.node->next->pre=it.node->pre;
destroy_node(it.node);
--length;
}
void reverse()
{
link_type tmp=node;
do
{
reverse_node(tmp);
tmp=tmp->pre;
} while(tmp!=node);
}
void transfer(iterator position,iterator first,iterator last)
{
if(first!=last)
{
position.node->pre->next=first.node;
first.node->pre->next=last.node;
last.node->pre->next=position.node;
link_type tmp=position.node->pre;
position.node->pre=last.node->pre;
last.node->pre=first.node->pre;
first.node->pre=tmp;
}
}
void splice(iterator position,list<T> l)
{
if(!empty())
{
transfer(position,l.begin(),l.end());
length+=l.length;
l.length=0;
}
}
void splice(iterator position,list<T> l,iterator i)
{
iterator j=i;
if(i==position||++j==position) return;
transfer(position,i,j);
++length;
--l.length;
}
void splice(iterator position,list<T> &l,iterator first,iterator last)
{
}
void remove(const value_type& x)
{
iterator first=begin();
iterator last=end();
while(first!=last)
{
iterator next=first;//erase()操作前先保存迭代器
++next;
if(x==first.node->data)
erase(first);
first=next;
}
}
void unique()
{
iterator first=begin();
iterator last=end();
for(;first!=last;++first)
{
iterator it=first;
++it;
//for(;it!=last;it++)
while(it!=last)//之前的for语句,it++跟下面的next重复了,段错误。。找了N久
{
bool bequal=(it!=last);
{iterator next=it;
++next;
if(first.node->data==it.node->data)
erase(it);
it=next;
}
}
}
}
void merge(list<T> &l)
{
iterator first1=begin();
iterator last1=end();
iterator first2=l.begin();
iterator last2=l.end();
while(first1!=last1&&first2!=last2)
{
if(*first2<*first1)
{
iterator next=first2;
transfer(first1,first2,++next);
first2=next;
}
else
++first1;
}
if(first1==last1)
transfer(last1,first2,last2);
}
//l.destroy_node(l.node);
void swap(link_type ptr1,link_type ptr2)
{
value_type temp=ptr1->data;
ptr1->data=ptr2->data;
ptr2->data=temp;
// link_type tmp=ptr1;
// ptr1=ptr2;
// ptr2=tmp;
// tmp=NULL;
}
void quick_sort(link_type first,link_type last)
{
//link_type tmp=create_node();
//tmp->data=first->data;
if(first==last) return;
link_type pivot=first;
link_type l=first->next,r=last;
do
{
while(r->data>pivot->data&&l!=r)
r=r->pre;
while(l->data<pivot->data&&l!=r)
l=l->next;
if(l!=r)
{
swap(l,r);
l=l->next;
r=r->pre;
}
}while(l!=r&&l!=r->next);
if(r->data<pivot->data)
{
swap(pivot,r);
pivot=r;
}
else
{
swap(pivot,r->pre);
pivot=r->pre;
}
//quick_sort(first,pivot->pre);
if(first!=pivot)
quick_sort(first,pivot->pre);
if(last!=pivot)
quick_sort(pivot->next,last);
}
void sort()
{
quick_sort(node->next,node->pre);
}
};
//#include"list"
//#include"iterator"
#include"myList.h"
//#include"myIterator.h"
#include"iostream"
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
list<int> ring0;
for(int i=1;i<=n;++i)
ring0.push_back(i);
list<int> ring1(ring0);
list<int> ring(ring1.begin(),ring1.end().node->pre);
// for(int i=1;i<=n;++i)
// ring.push_back(i);
//list<int>::iterator tmp=ring.begin();
ring.pop_back();
ring.reverse();
// ring.merge(ring0);
ring.sort();
ring1.sort();
ring.unique();
ring.merge(ring1);
ring.splice(ring.begin(),ring1);
for(list<int>::iterator tmp=ring.begin();tmp!=ring.end();++tmp)
cout<<*tmp;
cout<<endl;
tmp=ring.begin();
cout<<*tmp<<endl;
list<int>::iterator it=tmp;
n=ring.get_length();
for(int index=1;index<n;++index)
{
it=tmp;
cout<<*it<<endl;
for(int j=1;j<m;++j)
{
//++a;
// if(tmp==ring.end())
cout<<*it<<" ";
++it;
++tmp;
if(tmp==ring.end())
{
//tmp=ring.begin();
++tmp;
it=tmp;
}
}
cout<<endl;
++tmp;
if(tmp==ring.end())
{
//tmp=ring.begin();
++tmp;
// it=tmp;
}
cout<<*it<<" ";
//it.erase();
ring.erase(it);
}
it=ring.begin();
cout<<*it;
return 1;
}