// 此代码仅供个人学习之用
SingleLinkedList
// SingleLinkedList.h
#ifndef SINGLELINKEDLIST_H_H
#define SINGLELINKEDLIST_H_H
template
template
{
public:
List() : _first(0),_back(0)
{}
~List();
bool insert(T,int); // 安值和位置插入
bool push_back(T); // 接在链表队尾之后
bool push_front(T);// 插在链表头之前
bool remove(int index); // 按索引删除值
void remove_front(); // 从头部移出
void print_list(); // 输出链表
bool is_empty()
{ return _first==0; }
void reverse(); // 反转链表
int get_length();
List& operator + (List& );
private:
ListNode
bool is_index(int index); // 索引是否合法
};
template
{
friend class List
public:
ListNode( T t) : data(t), next(0)
{}
private:
T data;
ListNode * next; // 下一结点的指针
};
#endif
// SingleLinkedList.cpp
#ifndef SINGLELINKLIST_CPP
#define SINGLELINKLIST_CPP
#include "SingleLinkedList.h"
#include
using std::cout;
using std::endl;
template
{
ListNode
if( !temp )
return false;
if( is_empty() )
{
_first = _back = temp;
return true;
}
_back->next = temp;
_back = temp;
return true;
}
template
{
ListNode
if( !temp )
return false;
if( is_empty() )
{
_first = _back = temp;
return true;
}
temp->next = _first;
_first = temp;
return true;
}
template
{
if( is_empty() && index==0 )
{
push_back(t);
return true;
}
int i = get_length();
if(index<0 || index>= i )
{
cout << "下标越界" << endl;
return false;
}
if( index == 0 || index == (i-1) )
{
push_front(t);
return true;
}
ListNode
new_node= new ListNode
if( !new_node )
return false;
// 插入在temp的后面
temp = _first;
while (index--)
temp = temp->next;
new_node->next = temp->next;
temp->next = new_node;
return true;
}
template
{
int len = 0;
ListNode
while( temp != 0 )
{
++len;
temp = temp->next;
}
return len;
}
template
{
//if( is_empty() )
// return;
ListNode
_first = _back;
_back = temp;
ListNode
ft = 0; // 前结点
bc = temp ; // 后结点
while( temp != 0 )
{
temp = bc->next;
bc->next = ft;
ft = bc;
bc = temp;
}
}
template
{
if( is_empty() )
return;
ListNode
delete _first;
_first = temp;
}
template
{
if( is_index( index ) == false)
return false;
if ( 0 == index )
{
remove_front();
return true;
}
ListNode
ListNode
do
{
ft = temp;
temp = temp->next;
}while( --index);
// 删除最后一个结点
if ( index == (get_length() -1) )
{
delete _back;
_back = ft;
_back->next = 0;
return true;
}
// 删除普通结点
ft->next = temp->next;
delete temp;
return true;
}
template
{
if( ! is_empty() )
{
while( _first != 0)
{
_back = _first->next;
delete _first;
_first = _back;
}
}
}
template
{
if( is_empty() )
{
cout << "list is empty" << endl;
return ;
}
ListNode
while( temp != 0 )
{
cout << temp->data <<" " ;
temp = temp->next;
}
cout << endl;
}
template
{
if( is_empty() )
return false;
if( index<0 || (index >=get_length()) )
return false;
return true;
}
#endif
模板循环链表解决josephus问题
// CircleList.h
#ifndef CIRCLELIST_H_H
#define CIRCLELIST_H_H
template
template
{
friend class CircleList
public:
CirListNode(T d = 0) : data(d), next(0)
{}
private:
T data;
CirListNode * next;
};
template
{
public:
CircleList()
: first(0),last(0),current(0),front(0)
{}
bool is_empty()
{ return first ==0;}
void insert(T d);
void remove(); // 移出当前结点
void next() // 移动到下一结点
{ front =current;current = current->next;}
T getData()
{ return current->data; }
private:
CirListNode
};
#endif
// CircleList.cpp
#include "CircleList.h"
template
{
if( is_empty() )
{
first = last = current = new CirListNode
current->next = first;
return;
}
CirListNode
temp->next = current->next;
current->next = temp;
current = temp;
}
template
{
if ( front == 0 )
return;
front->next = current->next;
delete current;
current = front->next;
}
//main.cpp
#include
#include "CircleList.h"
#include "CircleList.cpp"
using namespace std;
template
void main ( )
{
CircleList
int n, m;
cout << "Enter the Number of Contestants?";
cin >> n >> m;
for ( int i=1; i<=n; i++ ) clist.insert (i);
//形成约瑟夫环
//clist.Josephus (n, m);
Josephus( n, m, clist);//解决约瑟夫问题
}
// n 小孩数量,m 步长
template
{
for ( int i=0; i
{//执行n-1次
for ( int j=0; j
clist.next();
cout << "Delete person "
<< clist.getData()<< endl; //数m-1个人
clist.remove();//删去
}
cout << "No."<< clist.getData() << " boy win/n";
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////