Dqueue template-c++
来源:互联网 发布:模拟人生网络猫咪 编辑:程序博客网 时间:2024/05/11 02:47
complied ok with dev-c and vc 6.0
#include <cstdlib>
#include <iostream>
#include "DoubleLink.h"
using namespace std;
int main(int argc, char *argv[])
{
cout<<"DoubleLink class template demo.../n";
//1.constrator DoubleLink() test ok
DoubleLink<int> int1,int2,int3; //DoubleLink()
int1.InsertNode(10);
int1.InsertNode(20);
int1.InsertNode(30);
int1.InsertNode(40);
//2.copy constrator,=,destructor test ok
DoubleLink<int> copy1(int1); //copy constrator
int2=copy1; //=
int2.Print(); //print()
//3.IsEmpty(),Length() test ok
cout<<"copy1.IsEmpty()="<<copy1.IsEmpty()<<endl; //IsEmpty()
cout<<"copy1.Length()="<<copy1.Length()<<endl; //Length()
//4.FindItem(item),Front(),Back()
cout<<"int1.FindItem(20)="<<int1.FindItem(20)<<endl;//int1.FindItem(item)
cout<<"int1.Front()="<<int1.Front()<<endl;//int1.Front()
cout<<"int1.Back()="<<int1.Back()<<endl;//int1.Back()
//5.InsertNode(item),DeleteNode(item) test ok
int1.InsertNode(99); //InsertNode(item)
int1.Print();
int1.DeleteNode(99); //DeleteNode(item)
int1.Print();
//6.copylist(DoubleLink&),Destrory() test ok
int3.copylist(int2); //copylist
int3.Destrory(); //Destrory
int3.Print();
//7.ReversePrint(),Print(),<<
int1.Print(); //Print()
int1.ReversePrint(); //ReversePrint()
cout<<int1; //<<
system("Pause");
return 0;
}
/*
*DoubleLink.h
*this is a DoubleLink template by pointer
*DoubleLink ADT
*writer:chinanetboy ,QQ:44633197
*blog http://chinanetboy.bokee.com
*date:07/01/2008
*/
#ifndef H_DoubleLink
#define H_DoubleLink
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
using namespace std;
//DoubleLink ADT list
template <class T>
struct NodeType{
T info;
NodeType<T> *next;
NodeType<T> *back;
};
template <class T>
class DoubleLink
{
public:
//1.constrator DoubleLink()
DoubleLink();
//2.copy constrator,=,destructor
DoubleLink(const DoubleLink <T> & O);
const DoubleLink <T>& operator=(const DoubleLink<T>& O);
~DoubleLink();
//3.IsEmpty(),Length(),
bool IsEmpty();
int Length();
//4.FindItem(item),Front(),Back()
bool FindItem(const T & item);
const T Front()const;
const T Back()const;
//5.InsertNode(item),DeleteNode(item),
void InsertNode(const T& item);
void DeleteNode(const T& item);
//6.copylist(DoubleLink&),Destrory()
void copylist(const DoubleLink<T>& O);
void Destrory();
//7.ReversePrint(),Pint(),<<
void ReversePrint();
void Print();
friend ostream& operator<<(ostream& O,const DoubleLink <T> & list){
NodeType<T> *current;
current=list.Last;
while(current!=NULL){
cout<<current->info<<" ";
current=current->back;
}
cout<<endl;
return O;
}
//private:
protected:
int count;
NodeType<T> *First;
NodeType<T> *Last;
};
//1.constrator SingleLink()
template <class T>
DoubleLink<T>::DoubleLink(){
count=0;
First=NULL;
Last=NULL;
}
//2.copy constrator,=,destructor
template <class T>
DoubleLink<T>::DoubleLink(const DoubleLink <T> & O){
First=NULL;
copylist(O);
}
template <class T>
const DoubleLink <T>& DoubleLink<T>::operator=(const DoubleLink<T>& O){
if (this!=&O)
copylist(O);
return *this;
}
template <class T>
DoubleLink<T>::~DoubleLink(){
Destrory();
}
//3.IsEmpty(),Length(),
template <class T>
bool DoubleLink<T>::IsEmpty(){
return (First==NULL);
}
template <class T>
int DoubleLink<T>::Length(){
return count;
}
//4.FindItem(item),Front(),Back()
template <class T>
bool DoubleLink<T>::FindItem(const T & item){
NodeType<T> *current;
bool found=false;
current=First;
while(current!=NULL && !found){
if (current->info==item)
found=true;
else
current=current->next;
}//end while
return found;
}
template <class T>
const T DoubleLink<T>::Front()const{
assert(First!=NULL);
return First->info;
}
template <class T>
const T DoubleLink<T>::Back()const{
assert(Last!=NULL);
return Last->info;
}
//5.InsertNode(item),DeleteNode(item)
template <class T>
void DoubleLink<T>::InsertNode(const T& item){
NodeType<T> *current,*traillcurrent,*newnode;
bool found;
newnode=new NodeType<T>;
assert(newnode!=NULL);
newnode->info=item;
newnode->next=NULL;
newnode->back=NULL;
if (First==NULL){
First=newnode;
Last=newnode;
count ;
}
else
{
found=false;
current=First;
while(current!=NULL && !found)
if(current->info==item)
found=true;
else
{
traillcurrent=current;
current=current->next;
}//end else
if (current=First){
First->back=newnode;
newnode->next=First;
First=newnode;
count ;
}
else
{
if (current!=NULL){
traillcurrent->next=newnode;
newnode->back=traillcurrent;
newnode->next=current;
current->back=newnode;
}
else
{
traillcurrent->next=newnode;
newnode->back=traillcurrent;
Last=newnode;
}
count ;
}//end else
}//end while
}
template <class T>
void DoubleLink<T>::DeleteNode(const T& item){
NodeType<T> *current,*trailcurrent;
bool found;
if (First!=NULL)
{
if(First->info==item){
current=First;
First=First->next;
if (First!=NULL)
First->back=NULL;
else
Last=NULL;
count--;
delete current;
}
else
{
found=false;
current=First;
while(current!=NULL && !found)
if(current->info==item)
found=true;
else
found=false;
if(current==NULL)
cout<<"not be found in list."<<endl;
else
if(current->info==item){
trailcurrent=current->back;
trailcurrent->next=current->next;
if(current->next!=NULL)
current->next=current->next;
if(current==Last)
Last=trailcurrent;
count--;
delete current;
}
else
cout<<"not be found in list."<<endl;
}//end else
}
}//end deletenode
//6.copylist(DoubleLink&),Destrory()
template <class T>
void DoubleLink<T>::Destrory(){
NodeType<T> *temp;
while(First!=NULL){
temp=First;
First=First->next;
delete temp;
}
Last=NULL;
count=0;
}
template <class T>
void DoubleLink<T>::copylist(const DoubleLink<T>& O){
NodeType<T> *newnode;
NodeType<T> *current;
if(First!=NULL)
Destrory();
if (O.First==NULL){
First=NULL;
Last=NULL;
count=0;
}
else
{
current= O.First;
count= O.count;
First=new NodeType<T>;
assert(First!=NULL);
First->info=current->info;
First->next=NULL;
Last=First;
current=current->next;
while(current!=NULL){
newnode= new NodeType<T>;
assert(newnode!=NULL);
newnode->info=current->info;
newnode->next=NULL;
newnode->back=current->back;
Last->next=newnode;
Last=newnode;
current=current->next;
}//endwhile
}//end else
}
//7.Print(),ReversePrint()
template <class T>
void DoubleLink<T>::Print(){
NodeType<T> *current;
current=Last;
while(current!=NULL){
cout<<current->info<<" ";
current=current->back;
}//end while
cout<<endl;
}
template <class T>
void DoubleLink<T>::ReversePrint(){
NodeType<T> *current;
current=First;
while(current!=NULL){
cout<<current->info<<" ";
current=current->next;
}//end while
cout<<endl;
}
#endif
- Dqueue template-c++
- C++template
- C template
- C++Template
- list c++template
- Queue-C++template
- Stack-c++template
- C++Template之Priority_queue
- [C++] Template练习
- eclipse-c-template
- eclipse-c-template
- C++template ;模板学习
- list c++template
- c plus template
- Template模版实例(C++)
- C++template--函数模版
- 练习笔记 C++Template
- [C++]Stack with Template
- 一次性批量上传百张图片并保存到数据库中
- java数据结构
- Linked List-C++
- 程序的静态测试之源程序静态分析
- pku 3682 King Arthur's Birthday Celebration(负二项分布、帕斯卡分布以及期望方差公式的应用)
- Dqueue template-c++
- 话说软件测试中的“易用性”测试
- 对代码覆盖率价值的初步认识
- NS2中对一些类型的定义
- binary tree template-c++
- 测试领域中有待解决的难题们
- CAS是什么
- list-java
- C语言基础