//=============================================================================================
//定义名为swap的函数模板用于交换两个变量中的数据
template void swap(T &x, T &y)
{
T temp;
temp = x;
x = y;
y = temp;
}
//==============================================================================================
//选择法对数组排序的函数模板
template void sort(T arr[],int size)
{
T temp;
int i,j;
for (i=0;i for (j=i+1;j if (arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
//=============================================================================================
//二分查找法的函数模板
template int binary_search(T array[], T value, int size)
{
int found = 0;
int high = size, low = 0, mid;
mid = (high + low) / 2;
cout<<"Looking for "< while ((! found) && (high >= low))
{
if (value == array[mid])
found = 1;
else
if (value < array[mid])
high = mid - 1;
else
low = mid + 1;
mid = (high + low) / 2;
}
return((found) ? mid: -1);
}
//=============================================================================================
//定义名为ex_class的类模板
template class ex_class
{
T value;
public:
ex_class(T v) { value=v; }
void set_value(T v) { value=v; }
T get_value(void) {return value;}
};
//定义名为ex_class的类模板
template class ex_class
{
T1 value1;
T2 value2;
public:
ex_class(T1 v1,T2 v2) {
value1=v1;
value2=v2;
}
void set_value(T1 v1,T2 v2) {
value1=v1;
value2=v2;
}
void put_value(void) {
cout<<"valu1="< cout<<"valu2="< }
};
//测试int和double类型数据
ex_class a(5,1.5);
cout<<"ex_class a:"< a.put_value();
a.set_value(100,3.14);
a.put_value();
//=============================================================================================
//定义处理栈的类模板接口
template class stack
{
T stck[SIZE];
int tos;
public:
stack(void) {
tos = 0;
cout << "Stack Initialized." << endl;
}
~stack(void) {
cout << "Stack Destroyed." << endl;
}
void push(T);
T pop(void);
};
//定义栈的成员函数
template void stack::push(T i)
{
if(tos==SIZE)
{
cout << "Stack is full." << endl;
return;
}
stck[tos++] = i;
}
template T stack::pop(void)
{
if(tos==0)
{
cout << "Stack underflow." << endl;
return 0;
}
return stck[--tos];
}
//================================================================================================
//使用当前的系统时间初始化随机数种子
srand( (unsigned)time( NULL ) );
a=rand(0,20);
//定义产生[n1,n2]范围int随机数的函数
int rand(int n1,int n2)
{
if (n1>n2) return -1;
if (n1==n2) return 0;
int temp=n1+int((n2-n1)*double(rand())/RAND_MAX);
return temp;
}
//================================================================================================
//定义时间延迟函数
void Dtime(double dt) {
time_t current_time;
time_t start_time;
//得到开始时间
time(&start_time);
//延迟处理
do
{
time(¤t_time);
}
while (difftime(current_time,start_time)
}
Dtime(3);
//==================================================================================================
#include
//定义节点(数据对象)的接口
class Node
{
//声明list类为本类的友元类
friend class list;
//私有成员
private:
int Data; //节点数据
Node *previous; //前趋指针
Node *next; //后继指针
};
//定义双向链表list的接口声明
class list
{
//私有成员
private:
Node *Head; //链表头指针
Node *Tail; //链表尾指针
//定义接口函数
public:
//构造函数
list();
//析构函数
~list();
//从链表尾后添加数据
void Build_HT(int Data);
//从链表前头添加数据
void Build_TH(int Data);
//从头到尾显示数据
void list::Display_HT();
//从尾到头显示数据
void list::Display_TH();
//清除链表的全部数据
void Clear();
};
//main()函数测试双向链表
int main(void)
{
list list1;
int i;
//从尾添加数据
cout<<"Add to the back of the list1:"<
for (i=1;i<=20;i=i+2) {
list1.Build_HT(i);
cout< }
cout<
//从头添加数据
cout<<"Add to the front of the list1:"<
for (i=0;i<=20;i=i+2) {
list1.Build_TH(i);
cout< }
cout<
//显示链表
list1.Display_HT();
list1.Display_TH();
return 0;
}
//list类函数的定义
//构造函数的定义
list::list()
{
//初值
Head=0;
Tail=0;
}
//析构函数的定义
list::~list()
{
Clear();
}
//从链表尾后添加数据
void list::Build_HT(int Data)
{
Node *Buffer;
Buffer=new Node;
Buffer->Data=Data;
if(Head==0)
{
Head=Buffer;
Head->next=0;
Head->previous=0;
Tail=Head;
}
else
{
Tail->next=Buffer;
Buffer->previous=Tail;
Buffer->next=0;
Tail=Buffer;
}
}
//从链表前头添加数据
void list::Build_TH(int Data)
{
Node *NewNode;
NewNode=new Node;
NewNode->Data=Data;
if(Tail==0)
{
Tail=NewNode;
Tail->next=0;
Tail->previous=0;
Head=Tail;
}
else
{
NewNode->previous=0;
NewNode->next=Head;
Head->previous=NewNode;
Head=NewNode;
}
}
//从头到尾显示数据
void list::Display_HT()
{
Node *TEMP;
TEMP=Head;
cout<<"Display the list from Head to Tail:"< while(TEMP!=0)
{
cout<Data<<" ";
TEMP=TEMP->next;
}
cout<}
//从尾到头显示数据
void list::Display_TH()
{
Node *TEMP;
TEMP=Tail;
cout<<"Display the list from Tail to Head:"< while(TEMP!=0)
{
cout<Data<<" ";
TEMP=TEMP->previous;
}
cout<}
//清除链表的全部数据
void list::Clear()
{
Node *Temp_head=Head;
if (Temp_head==0) return;
do
{
Node *TEMP_NODE=Temp_head;
Temp_head=Temp_head->next;
delete TEMP_NODE;
}
while (Temp_head!=0);
}
//===============================================================================================
阅读(1237) | 评论(1) | 转发(0) |