C++ 数据结构与算法

目  录
1、顺序表	1
Seqlist.h	1
Test.cpp	6
2、单链表	8
ListNode.h	8
SingleList.h	10
test.cpp	20
3、双向链表	22
NodeList.h	22
DoubleList.h	24
Test.cpp	34
4、循环链表	36
ListNode.h	36
CircularList.h	37
Test.cpp	47
5、顺序栈	49
SeqStack.h	49
Test.cpp	54
6、链式栈	55
StackNode.h	55
LinkStack.h	56
Test.cpp	60
7、顺序队列	62
SeqQueue.h	63
Test.cpp	68
8、链式队列	70
QueueNode.h	70
LinkQueue.h	71
Test.cpp	75
9、优先级队列	77
QueueNode.h	77
Compare.h	78
PriorityQueue.h	80
Test.cpp	85
10、串	88
MyString.h	88
MyString.cpp	90
test.cpp	101
11、二叉树	104
BinTreeNode.h	104
BinaryTree.h	112
Test.cpp	124
12、线索二叉树	126
ThreadNode.h	126
ThreadTree.h	128
ThreadInorderIterator.h	128
test.cpp	139
13、堆	140
MinHeap.h	140
test.cpp	147
14、哈夫曼树	149
BinTreeNode.h	149
BinaryTree.h	151
MinHeap.h	156
Huffman.h	161
Test.cpp	163
15、树	164
QueueNode.h	164
LinkQueue.h	165
TreeNode.h	169
Tree.h	170
test.cpp	187
16、B+树	189
BTreeNode.h	189
BTree.h	192
test.cpp	215
17、图	217
MinHeap.h	217
Edge.h	222
Vertex.h	223
Graph.h	224
test.cpp	246
18、排序	249
Data.h	249
QueueNode.h	255
LinkQueue.h	259
Sort.h	263
test.cpp	278
 
1、顺序表

Seqlist.h

const int DefaultSize=100;

template  class SeqList{
public:
	SeqList(int sz=DefaultSize)
		:m_nmaxsize(sz),m_ncurrentsize(-1){
		if(sz>0){
			m_elements=new Type[m_nmaxsize];
		}
	}
	~SeqList(){
		delete[] m_elements;
	}
	int Length() const{					//get the length
		return m_ncurrentsize+1;
	}
	int Find(Type x) const;				//find the position of x
	int IsElement(Type x) const;		//is it in the list
	int Insert(Type x,int i);			//insert data
	int Remove(Type x);					//delete data
	int IsEmpty(){
		return m_ncurrentsize==-1;
	}
	int IsFull(){
		return m_ncurrentsize==m_nmaxsize-1;
	}
	Type Get(int i){					//get the ith data
		return i<0||i>m_ncurrentsize?(cout<<"can't find the element"< int SeqList::Find(Type x) const{
	for(int i=0;i int SeqList::IsElement(Type x) const{
	if(Find(x)==-1)
		return 0;
	return 1;
}

template  int SeqList::Insert(Type x, int i){
	if(i<0||i>m_ncurrentsize+1||m_ncurrentsize==m_nmaxsize-1){
		cout<<"the operate is illegal"
#include "SeqList.h"

using namespace std;

int main()
{
	SeqList test(15);
	int array[15]={2,5,8,1,9,9,7,6,4,3,2,9,7,7,9};
	for(int i=0;i<15;i++){
		test.Insert(array[i],0);
}
	test.Insert(1,0);
	cout<<(test.Find(0)?"can't be found ":"Be found ")<< 0 << endl< class SingleList;

template class ListNode{
private:
	friend typename SingleList;

	ListNode():m_pnext(NULL){}
	ListNode(const Type item,ListNode *next=NULL):m_data(item),m_pnext(next){}
	~ListNode(){
		m_pnext=NULL;
	}

public:
	Type GetData();
	friend ostream& operator<< (ostream& ,ListNode&);

private:
	Type m_data;
	ListNode *m_pnext;
};

template Type ListNode::GetData(){
	return this->m_data;
}

template ostream& operator<<(ostream& os,ListNode& out){
	os< class SingleList{
public:
	SingleList():head(new ListNode()){}
	~SingleList(){
		MakeEmpty();
		delete head;
	}

public:
	void MakeEmpty();                       //make the list empty
	int Length();                           //get the length
	ListNode *Find(Type value,int n); //find thd nth data which is equal to value
	ListNode *Find(int n);            //find the nth data
	bool Insert(Type item,int n=0);         //insert the data in the nth position
	Type Remove(int n=0);                   //remove the nth data
	bool RemoveAll(Type item);              //remove all the data which is equal to item
	Type Get(int n);                        //get the nth data
	void Print();                           //print the list

private:
	ListNode *head;
};

template void SingleList::MakeEmpty(){
	ListNode *pdel;
	while(head->m_pnext!=NULL){
		pdel=head->m_pnext;
		head->m_pnext=pdel->m_pnext;
		delete pdel;
	}
}

template int SingleList::Length(){
	ListNode *pmove=head->m_pnext;
	int count=0;
	while(pmove!=NULL){
		pmove=pmove->m_pnext;
		count++;
	}
	return count;
}

template ListNode* SingleList::Find(int n){
	if(n<0){
		cout<<"The n is out of boundary"< *pmove=head->m_pnext;
	for(int i=0;im_pnext;
	}
	if(pmove==NULL){
		cout<<"The n is out of boundary"< ListNode* SingleList::Find(Type value,int n){
	if(n<1){
		cout<<"The n is illegal"< *pmove=head;
	int count=0;
	while(count!=n&&pmove){
		pmove=pmove->m_pnext;
		if(pmove->m_data==value){
			count++;
		}

	}
	if(pmove==NULL){
		cout<<"can't find the element"< bool SingleList::Insert(Type item, int n){
	if(n<0){
		cout<<"The n is illegal"< *pmove=head;
	ListNode *pnode=new ListNode(item);
	if(pnode==NULL){
		cout<<"Application error!"m_pnext;
	pmove->m_pnext=pnode;
	return 1;
}

template bool SingleList::RemoveAll(Type item){
	ListNode *pmove=head;
	ListNode *pdel=head->m_pnext;
	while(pdel!=NULL){
		if(pdel->m_data==item){
			pmove->m_pnext=pdel->m_pnext;
			delete pdel;
			pdel=pmove->m_pnext;
			continue;
		}
		pmove=pmove->m_pnext;
		pdel=pdel->m_pnext;
	}
	return 1;
}

template Type SingleList::Remove(int n){
	if(n<0){
		cout<<"can't find the element"< *pmove=head,*pdel;
	for(int i=0;im_pnext;i++){
		pmove=pmove->m_pnext;
	}
	if(pmove->m_pnext==NULL){
		cout<<"can't find the element"m_pnext=pdel->m_pnext;
	Type temp=pdel->m_data;
	delete pdel;
	return temp;
}

template Type SingleList::Get(int n){
	if(n<0){
		cout<<"The n is out of boundary"< *pmove=head->m_pnext;
	for(int i=0;im_pnext;
		if(NULL==pmove){
			cout<<"The n is out of boundary"m_pnext;
	cout<<"head";
	while(pmove){
		cout<<"--->"m_pnext;
	}
	cout<<"--->over"<
using namespace std;

#include "SingleList.h"


int main()
{
	SingleList list;
	for(int i=0;i<20;i++){
		list.Insert(i*3,i);
	}
	for(int i=0;i<5;i++){
		list.Insert(3,i*3);
	}
	cout<<"the Length of the list is "< class DoublyList;

template class ListNode{
private:
	friend class DoublyList;
	ListNode():m_pprior(NULL),m_pnext(NULL){}
	ListNode(const Type item,ListNode *prior=NULL,ListNode *next=NULL)
		:m_data(item),m_pprior(prior),m_pnext(next){}
	~ListNode(){
		m_pprior=NULL;
		m_pnext=NULL;
	}
public:
	Type GetData();
private:
	Type m_data;
	ListNode *m_pprior;
	ListNode *m_pnext;
};

template Type ListNode::GetData(){
	return this->m_data;
}

DoubleList.h

#include "ListNode.h"

template class DoublyList{
public:
	DoublyList():head(new ListNode()){    //the head node point to itself
		head->m_pprior=head;
		head->m_pnext=head;
	}
	~DoublyList(){
		MakeEmpty();
		delete head;
	}

public:
	void MakeEmpty();   //make the list empty
	int Length();       //get the length of the list
	ListNode *Find(int n=0);  //find the nth data
	ListNode * FindData(Type item);   //find the data which is equal to item
	bool Insert(Type item,int n=0);     //insert item in the nth data
	Type Remove(int n=0);   //delete the nth data
	Type Get(int n=0);      //get the nth data
	void Print();           //print the list

private:
	ListNode *head;
};

template void DoublyList::MakeEmpty(){
	ListNode *pmove=head->m_pnext,*pdel;
	while(pmove!=head){
		pdel=pmove;
		pmove=pdel->m_pnext;
		delete pdel;
	} 
	head->m_pnext=head;
	head->m_pprior=head;
}

template int DoublyList::Length(){
	ListNode *pprior=head->m_pprior,*pnext=head->m_pnext;
	int count=0;
	while(1){
		if(pprior->m_pnext==pnext){
			break;
		}
		if(pprior==pnext&&pprior!=head){
			count++;
			break;
		}
		count+=2;
		pprior=pprior->m_pprior;
		pnext=pnext->m_pnext;
	}
	return count;
}

template ListNode* DoublyList::Find(int n = 0){
	if(n<0){
		cout<<"The n is out of boundary"< *pmove=head->m_pnext;
	for(int i=0;im_pnext;
		if(pmove==head){
			cout<<"The n is out of boundary"< bool DoublyList::Insert(Type item,int n){
	if(n<0){
		cout<<"The n is out of boundary"< *newnode=new ListNode(item),*pmove=head;
	if(newnode==NULL){
		cout<<"Application Erorr!"m_pnext;
	newnode->m_pprior=pmove;
	pmove->m_pnext=newnode;
	newnode->m_pnext->m_pprior=newnode;
	return 1;
}

template Type DoublyList::Remove(int n = 0){
	if(n<0){
		cout<<"The n is out of boundary"< *pmove=head,*pdel;
	for(int i=0;im_pnext;
		if(pmove==head){
			cout<<"The n is out of boundary"m_pnext=pdel->m_pnext;
	pmove->m_pnext->m_pprior=pdel->m_pprior;
	Type temp=pdel->m_data;
	delete pdel;
	return temp;
}

template Type DoublyList::Get(int n = 0){
	if(n<0){
		cout<<"The n is out of boundary"< *pmove=head;
	for(int i=0;im_pnext;
		if(pmove==head){
			cout<<"The n is out of boundary"m_pnext;
	cout<<"head";
	while(pmove!=head){
		cout<<"--->"m_pnext;
	}
	cout<<"--->over"< ListNode* DoublyList::FindData(Type item){
	ListNode *pprior=head->m_pprior,*pnext=head->m_pnext;
	while(pprior->m_pnext!=pnext && pprior!=pnext){ //find the data in the two direction
		if(pprior->m_data==item){
			return pprior;
		}
		if(pnext->m_data==item){
			return pnext;
		}
		pprior=pprior->m_pprior;
		pnext=pnext->m_pnext;
	}
	cout<<"can't find the element"<
#include "DoublyList.h"

using namespace std;

int main()
{
	DoublyList list;
	for(int i=0;i<20;i++){
		list.Insert(i*3,i);
	}
	cout<<"the Length of the list is "m_pnext=head;	
	}
	~CircularList(){
		MakeEmpty();
		delete head;
	}
public:
	void MakeEmpty();	//clear the list
	int Length();		//get the length
	ListNode *Find(Type value,int n);	//find the nth data which is equal to value
	ListNode *Find(int n);			//find the nth data
	bool Insert(Type item,int n=0);			//insert the data into the nth data of the list
	Type Remove(int n=0);					//delete the nth data
	bool RemoveAll(Type item);				//delete all the datas which are equal to value
	Type Get(int n);	//get the nth data
	void Print();		//print the list

private:
	ListNode *head;

};

template void CircularList::MakeEmpty(){
	ListNode *pdel,*pmove=head;
	while(pmove->m_pnext!=head){
		pdel=pmove->m_pnext;
		pmove->m_pnext=pdel->m_pnext;
		delete pdel;
	}
}

template int CircularList::Length(){
	ListNode *pmove=head;
	int count=0;
	while(pmove->m_pnext!=head){
		pmove=pmove->m_pnext;
		count++;
	}
	return count;
}

template ListNode* CircularList::Find(int n){
	if(n<0){
		cout<<"The n is out of boundary"< *pmove=head->m_pnext;
	for(int i=0;im_pnext;
	}
	if(pmove==head){
		cout<<"The n is out of boundary"< ListNode* CircularList::Find(Type value,int n){
	if(n<1){
		cout<<"The n is illegal"< *pmove=head;
	int count=0;
	while(count!=n){
		pmove=pmove->m_pnext;
		if(pmove->m_data==value){
			count++;
		}
		if(pmove==head){
			cout<<"can't find the element"< bool CircularList::Insert(Type item, int n){
	if(n<0){
		cout<<"The n is out of boundary"< *pmove=head;
	ListNode *pnode=new ListNode(item);
	if(pnode==NULL){
		cout<<"Application error!"m_pnext;
	pmove->m_pnext=pnode;
	return 1;
}

template bool CircularList::RemoveAll(Type item){
	ListNode *pmove=head;
	ListNode *pdel=head->m_pnext;
	while(pdel!=head){
		if(pdel->m_data==item){
			pmove->m_pnext=pdel->m_pnext;
			delete pdel;
			pdel=pmove->m_pnext;
			continue;
		}
		pmove=pmove->m_pnext;
		pdel=pdel->m_pnext;
	}
	return 1;
}

template Type CircularList::Remove(int n){
	if(n<0){
		cout<<"can't find the element"< *pmove=head,*pdel;
	for(int i=0;im_pnext!=head;i++){
		pmove=pmove->m_pnext;
	}
	if(pmove->m_pnext==head){
		cout<<"can't find the element"m_pnext=pdel->m_pnext;
	Type temp=pdel->m_data;
	delete pdel;
	return temp;
}

template Type CircularList::Get(int n){
	if(n<0){
		cout<<"The n is out of boundary"< *pmove=head->m_pnext;
	for(int i=0;im_pnext;
		if(pmove==head){
			cout<<"The n is out of boundary"m_pnext;
	cout<<"head";
	while(pmove!=head){
		cout<<"--->"m_pnext;
	}
	cout<<"--->over"<
#include "CircularList.h"

using namespace std;

int main()
{
	CircularList list;
	for(int i=0;i<20;i++){
		list.Insert(i*3,i);
	}
	cout<<"the Length of the list is "< class SeqStack{
public:
	SeqStack(int sz):m_ntop(-1),m_nMaxSize(sz){
		m_pelements=new Type[sz];
		if(m_pelements==NULL){
			cout<<"Application Error!"< void SeqStack::Push(const Type item){
	if(IsFull()){
		cout<<"The stack is full!"< Type SeqStack::Pop(){
	if(IsEmpty()){
		cout<<"There is no element!"< Type SeqStack::GetTop() const{
	if(IsEmpty()){
		cout<<"There is no element!"< void SeqStack::Print(){
	cout<<"bottom";
	for(int i=0;i<=m_ntop;i++){
		cout<<"--->"m_pnext;
		delete pmove;
	}
}

template void LinkStack::Push(const Type item){
	m_ptop=new StackNode(item,m_ptop);
}

template Type LinkStack::GetTop() const{
	if(IsEmpty()){
		cout<<"There is no elements!"m_pnext;
	Type temp=pdel->m_data;
	delete pdel;
	return temp;
}

template void LinkStack::Print(){
	StackNode *pmove=m_ptop;
	cout<<"buttom";
	while(pmove!=NULL){
		cout<<"--->"m_pnext;
	}
	cout<<"--->top"<
using namespace std;

#include "LinkStack.h"

int main(){
	LinkStack stack;
	int init[10]={1,3,5,7,4,2,8,0,6,9};
	for(int i=0;i<10;i++){
		stack.Push(init[i]);
	}
	stack.Print();

	cout< class SeqQueue{
public:
	SeqQueue(int sz):m_nrear(0),m_nfront(0),m_ncount(0),m_nMaxSize(sz){
		m_pelements=new Type[sz];
		if(m_pelements==NULL){
			cout<<"Application Error!"< void SeqQueue::MakeEmpty(){
	this->m_ncount=0;
	this->m_nfront=0;
	this->m_nrear=0;
}

template bool SeqQueue::IsEmpty(){
	return m_ncount==0;
}

template bool SeqQueue::IsFull(){
	return m_ncount==m_nMaxSize;
}

template bool SeqQueue::Append(const Type item){
	if(IsFull()){
		cout<<"The queue is full!"< Type SeqQueue::Delete(){
	if(IsEmpty()){
		cout<<"There is no element!"< Type SeqQueue::Get(){
	if(IsEmpty()){
		cout<<"There is no element!"< void SeqQueue::Print(){
	cout<<"front";
	for(int i=0;i"m_pnext;
		delete pdel;
	}
}

template void LinkQueue::Append(const Type item){
	if(m_pfront==NULL){
		m_pfront=m_prear=new QueueNode(item);
	}
	else{
		m_prear=m_prear->m_pnext=new QueueNode(item);
	}
}

template Type LinkQueue::Delete(){
	if(IsEmpty()){
		cout<<"There is no element!"< *pdel=m_pfront;
	Type temp=m_pfront->m_data;
	m_pfront=m_pfront->m_pnext;
	delete pdel;
	return temp;
}

template Type LinkQueue::GetFront(){
	if(IsEmpty()){
		cout<<"There is no element!""m_pnext;
	}
	cout<<"--->rear"<

你可能感兴趣的:(C++学习)