青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

數據結構算法集---C++語言實現

數據結構算法集---C++語言實現

 

作者:蕭何 文章來源:C語言之家 點擊數: 687 更新時間:2004-11-9

這是我學數據結構編寫的算法,我把他整理出來,都是基本算法,供大家學習。我使用c++面向對象形式編寫,各種算法都封裝在各自的類里,如果想增加功能,在相應的類里增加函數即可。我對樹和圖的構造也做了一些人性化設計,輸入更加形象化,你可能看不懂,沒關系漫漫來。各種類都使用模版設計,可以對各種數據類型操作(整形,字符,浮點)


///////////////////////////
//    //
//  
堆棧數據結構   stack.h         //
//    //
//////////////////////////


#include<iostream.h>

template<class Type>class Stack;

template<class Type>
class StackNode
{
friend class Stack<Type>;
private:
Type data;
StackNode<Type> *link;
   StackNode(Type D=0,StackNode<Type> *L=NULL):link(L),data(D){}
};

template<class Type>
class Stack
{
public:
Stack():top(NULL),NumItem(0){}
void Push(Type item);
Type Pop();
Type GetTop();
void MakeEmpty();
bool ISEmpty();
int GetNum();
private:
int NumItem;
StackNode<Type> *top;
};

template<class Type>
void Stack<Type>::Push(Type item)
{
  top=new StackNode<Type>(item,top);
NumItem++;
}

template<class Type>
Type Stack<Type>::Pop()
{
StackNode<Type> *p;
Type temp;
temp=top->data;
p=top;
top=top->link;
delete p;
NumItem--;
return temp;

}

template<class Type>
Type Stack<Type>::GetTop()
{
return top->data;
}

template<class Type>
bool Stack<Type>::ISEmpty()
{
return top==NULL;
}

template<class Type>
void Stack<Type>::MakeEmpty()
{
delete top;
}

template<class Type>
int Stack<Type>::GetNum()
{
return NumItem;
}



///////////////////////////
//    //
//  
隊列數據結構       Queue.h //
//    //
//////////////////////////
#include<iostream.h>

template<class Type> class Queue;

template<class Type> class QueueNode
{
friend class Queue<Type>;
private:
Type data;
QueueNode<Type> *link;
QueueNode(Type d=0,QueueNode *l=NULL):data(d),link(l){}
};

template <class Type> class Queue
{
public:
Queue():rear(NULL),front(NULL){}
~Queue();
void EnQueue(Type item);
Type DelQueue();
Type GetFront();
void MakeEmpty();
bool ISEmpty() { return front==NULL; }
private:
QueueNode<Type> *front,*rear;
};


template<class Type>
Queue<Type>::~Queue()
{
QueueNode<Type> *p;
while(front!=NULL)
{
p=front;
front=front->link;
delete p;
}
}

template<class Type>
void Queue<Type>::EnQueue(Type item)
{
if(front==NULL)
front=rear=new QueueNode<Type> (item,NULL);
else
rear=rear->link=new QueueNode<Type> (item,NULL);
}


template<class Type>
Type Queue<Type>::DelQueue()
{
QueueNode<Type> *p=front;
Type temp=p->data;;
front=front->link;
delete p;
return temp;
}


template<class Type>
Type Queue<Type>::GetFront()
{
return front->data;
}


template<class Type>
void Queue<Type>::MakeEmpty()
{
QueueNode<Type> *p;
while(front!=NULL)
{
p=front;
front=front->link;
delete p;
}
}


///////////////////////////
//    //
//  
鏈表數據結構  list.h //
//    //
//////////////////////////


#include<iostream.h>

template<class type>
class list;

template<class type>
class listnode
{
public:
friend class list<type>;
private:
type data;
listnode<type> * next;
};


template<class type>
class list
{
public:
list();
~list();
void insertend(type); //
向鏈表尾部插入元素
bool insert(type,int); //
向鏈表任意位置插入元素
void delnode(int i);  //
刪除元素
int find(type T);   //
查找元素
void makeempty();   //
銷毀鏈表
bool print();  //
打印鏈表
int getlen();  //
得到鏈表長度
  private:
listnode<type> *first,*last;
int length;
};

template<class type>
void initlist(type &tmp);

template<class type>
void list_exit(list<type> &L,type tmp);

void initation();

template<class type>
void list_insertend(list<type> &L,type tmp);


template<class type> int list<type>::getlen()
{
return length;
}

template<class type> void list<type>::makeempty()
{
listnode<type> *p1,*p2;

p1=first->next;
first->next=NULL;
while(p1!=NULL)
  {
p2=p1;
p1=p1->next;
delete p2;
}
length=0;  
}

template<class type> void list<type>::insertend(type t)
{

listnode<type> *p;
p=new listnode<type>;
p->data=t;
p->next=NULL;
last->next=p;
last=p;

length++;
}

template<class type> bool list<type>::insert(type t,int i)
{
listnode<type> *p;
p=first;

int k=1;
while(p!=NULL&&k<i)
{
p=p->next;
k++;
}
if(p==NULL&&k!=i)
return false;
else
{
   listnode<type> *tp;
  tp=new listnode<type>;
  tp->data=t;
  tp->next=p->next;
  p->next=tp;
  length++;
  
  return true;
}
}

template<class type> void list<type>::delnode(int i)
{
int k=1;
listnode<type> *p,*t;
p=first;

while(p->next!=NULL&&k!=i)
{
p=p->next;
   k++;
}
  t=p->next;
cout<<"
你已經將數據項 "<<t->data<<"刪除"<<endl;

p->next=p->next->next;
length--;
delete t;
}

template<class type> bool list<type>::print()
{
listnode<type> *p=first->next;
if(length==0)
return false;
else
{
cout<<"
鏈表中有"<<length<<"項數據: "<<endl;
while(p)
{
  cout<<p->data<<" ";
  p=p->next;
}
}
cout<<endl;


return true;
}

template<class type> int list<type>::find(type T)
{
listnode<type> *p=first->next;
int i=1;
while(p&&p->data!=T)
{
p=p->next;
i++;
}
if(p)
return i;
else
   return 0;
}


template<class type> list<type>::~list()
{
delete first;
cout<<"
歡迎再次使用 (!^!) "<<endl;
}

template<class type> list<type>::list()
{
     listnode<type> *node=new listnode<type>;
  node->next=NULL;
  first=last=node;
  length=0;
}

///////////////////////////
//    //
//  
圖數據結構  graph.h  //
//    //
//////////////////////////


#include<iostream.h>
#include"Queue.h"

template<class NameType,class DisType>class Graph;

template<class NameType,class DisType> struct Node    
{
friend class Graph<NameType,DisType>;
int num;
DisType val;
Node<NameType,DisType> *next;
};

template<class NameType,class DisType> struct GpNode
{
friend class Graph<NameType,DisType>;
NameType data;
  Node<NameType,DisType> *link;
};

template<class NameType,class DisType>
class Graph
{
public:
void Creat();  //
創建圖
void PrintNode();    //
打印圖中的各個數據項
void DFS();    //
圖的深度優先搜索,主過程
void DFS(int v,int visited[]); //
子過程
void BFS();  //
圖的廣度優先搜索,主過程
void BFS(int v,int visited[]); //
子過程
void ShortPath();     //
求最短路徑
private:
GpNode<NameType,DisType> *table;
Node<NameType,DisType> *p;
int NumNode;        //
節點個數
};


template<class NameType,class DisType>
void Graph<NameType,DisType>::Creat()
{
do
{
cout<<"
請輸入節點個數:  ";
cin >> NumNode;
}while(NumNode<=0);

table=new GpNode<NameType,DisType>[NumNode];
cout<<"
請輸入各節點數據項"<<endl;
for(int i=0;i<NumNode;i++)
{
cin>>table[i].data;
table[i].link=NULL;
}

cout<<"
請輸入各邊的關系 (: A B)"<<endl;
i=1;
NameType nodeA,nodeB;
bool findA,findB;
char ISExit;
int m,n;
do
{
findA=findB=false;
cout<<"
請輸入第"<<i<<"對邊的關系"<<endl;
cin>>nodeA>>nodeB;
for(m=0,n=0;m<NumNode&&n<NumNode&&!(findA & findB);) //
查找邊的節點
{
  if(nodeA!=table[m].data)
  m++;
  else
  findA=true;
  if(nodeB!=table[n].data)
  n++;
  else
  findB=true;

}
if(!(findA & findB))
  cout<<"
輸入的節點數據項有錯誤"<<endl;
else
{
  p=new Node<NameType,DisType>;
  p->next=table[m].link;
  p->num=n;
  table[m].link=p;
  cout<<"
請輸入該對邊的權值: ";
  cin>>p->val;
  i++;
}
cout<<"
是否繼續輸入: y)繼續,X)任意鍵退出 ";
cin>>ISExit;
if(ISExit!='y'&&ISExit!='Y')
  break;

}while(true);
  
}

template<class NameType,class DisType>
void Graph<NameType,DisType>::PrintNode()
{
cout<<"
圖中各節點數據項 : ";
for(int i=0;i<NumNode;i++)
   cout<<table[i].data<<" ";
cout<<endl;
}


template<class NameType,class DisType>
void Graph<NameType,DisType>::DFS()
{
int *visited=new int[NumNode];
cout<<"
圖的深度優先搜索 : ";
for(int i=0;i<NumNode;i++)
visited[i]=0;
for(i=1;i<NumNode;i++) //
遍厲孤立節點
DFS(i,visited);
delete []visited;
cout<<endl;
}

template<class NameType,class DisType>
void Graph<NameType,DisType>::DFS(int v,int visited[])
{
Node<NameType,DisType> *t;
if(visited[v]==0)
   cout<<table[v].data<<" ";
visited[v]=1;
t=table[v].link;
while(t!=NULL)
{
if(visited[t->num]==0)
  DFS(t->num,visited);
t=t->next;
}
}


template<class NameType,class DisType>
void Graph<NameType,DisType>::BFS()
{
int *visited=new int[NumNode];
cout<<"
圖的廣度優先搜索 : ";
for(int i=0;i<NumNode;i++)
visited[i]=0;
for( i=0;i<NumNode;i++)
   BFS(i,visited);
}


template<class NameType,class DisType>
void Graph<NameType,DisType>::BFS(int v,int visited[])
{
Queue<int> q;
int n;
if(visited[v]==0)
{
visited[v]=1;
cout<<table[v].data<<" ";
q.EnQueue(v);
while(!q.ISEmpty())
{
  n=q.DelQueue();
  p=table[n].link;
  while(p!=NULL)
  {
  n=p->num;
  if(visited[n]==0)
  {
   cout<<table[n].data<<" ";
   visited[n]=1;

  }
  p=p->next;
  }

}
}

}


///////////////////////////
//    //
//  
排序算法數據結構 Compositor.h     //
//    //
//////////////////////////


#include<iostream.h>


template<class Type>
class Compositor
{
public:
Compositor():sort(NULL){}
void Creat();    //
創建排序數組
void Bubble(); //
冒泡排序  
void Insert(); //
插入排序
//
快速排序
void Quick();  
void QSort(int,int);
int Partition(int low,int high);
//
歸并排序
void Merge(Type SR[],Type TR[],int i,int m,int n);
void Msort(Type SR[],Type TR1[],int s,int t);
   void MergeSort();
//
選擇排序
void Select();
void Print();   //
打印排序后的結果
protected:
Type *sort;
int leng;
};

template<class Type>
void Compositor<Type>::Creat()
{
cout<<"
輸入你需要排序的數據個數: ";
cin>>leng;
while(leng<=0)
{
cout<<"
輸入數據有誤";
cin>>leng;
}
sort=new Type[leng];
cout<<"
請輸入各數據項:";
for(int i=0;i<leng;i++)
cin>>sort[i];
}  


template<class Type>
void Compositor<Type>::Insert()
{
Creat();
Type temp;
   for(int i=1;i<leng;i++)
{
  if(sort[i]<sort[i-1])
  {
  temp=sort[i];
  for(int j=i-1;temp<sort[j]&&j>=0;j--)
  {
  sort[j+1]=sort[j];
  }
  sort[j+1]=temp;
  }
}
Print();

}

template<class Type>
void Compositor<Type>::Bubble()
{
  Creat();
Type temp;
for(int i=leng-1;i>=0;i--)
{
for(int j=0;j<leng-1;j++)
{
  if(sort[j]>sort[j+1])
  {
  temp=sort[j];
  sort[j]=sort[j+1];
  sort[j+1]=temp;
  }
}
}
Print();
}

template<class Type>
void Compositor<Type>::Quick()
{
Creat();
  QSort(0,leng-1);
Print();
}

template<class Type>
void Compositor<Type>::QSort(int s,int t)
{
if(s<t-1)
{
int pivotloc=Partition(s,t);
QSort(s,pivotloc-1);
QSort(pivotloc+1,t);
}
}

template<class Type>
int Compositor<Type>::Partition(int low,int high)
{
   Type pivotkey=sort[low];
while(low < high)
{
  while(low<high&&sort[high]>=pivotkey)
  --high;
  sort[low++]=sort[high];
  while(low<high&&sort[low]<=pivotkey)
  ++low;
  sort[high--]=sort[low];
}  
sort[low]=pivotkey;
return low;
}

template<class Type>
void Compositor<Type>::MergeSort()
{
Creat();
  Msort(sort,sort,0,leng-1);
Print();
}


template<class Type>
void Compositor<Type>::Msort(Type SR[],Type TR1[],int s,int t)
{
int m;
Type *TR2=new Type[t-s];
if(s==t) TR1[s]=SR[s];
else
{
m=(t+s)/2;
Msort(SR,TR2,s,m);
Msort(SR,TR2,m+1,t);
Merge(TR2,TR1,s,m,t);
}
}

template<class Type>
void Compositor<Type>::Merge(Type SR[],Type TR[],int i,int m,int n)
{
for(int j=m+1,k=i;i<=m&&j<=n;k++)
{
if(SR[i]<=SR[j])
  TR[k]=SR[i++];
else
  TR[k]=SR[j++];
}
while(i<=m)
TR[k++]=SR[i++];
while(j<=n)
TR[k++]=SR[j++];
}


template<class Type>
void Compositor<Type>::Select()
{
Creat();
  Type temp;
int t;
for(int i=0;i<leng;i++)
{
t=i;
for(int j=i+1;j<leng;j++)
{
  if(sort[t]>sort[j])
  t=j;
}
if(t!=i)
{
  temp=sort[t];
  sort[t]=sort[i];
  sort[i]=temp;
}
}
Print();
}

template<class Type>
void Compositor<Type>::Print()
{
cout<<"
排序結果為: ";
for(int i=0;i<leng;i++)
cout<<sort[i]<<" ";
cout<<endl;
}



///////////////////////////
//    //
//  
二叉樹數據結構  BinTree.h       //
//    //
//////////////////////////


#include<iostream.h>

template<class Type>class BinTree;

template<class Type>
class TreeNode
{
  protected:
   friend class BinTree<Type>;
TreeNode():lchild(NULL),rchild(NULL){}
   Type data;
   TreeNode *lchild;  //
左,右子樹
   TreeNode *rchild;
};

template<class Type>
class BinTree
{
friend void BinTree_PRE(BinTree<Type>& BinTreeOPP);  //
友元函數
friend void BinTree_INO(BinTree<Type>& BinTreeOPP);
friend void BinTree_POS(BinTree<Type>& BinTreeOPP);
friend void BinTree_Destroy(BinTree<Type>& BinTreeOPP);
public:
BinTree():root(NULL){}
void CreatTree();               //
創建二叉樹,主過程
void CreatTree(TreeNode<Type>* child,int k); //
子過程
void PreTree(TreeNode<Type> *point);     //
先序遍歷二叉樹
void InoTree(TreeNode<Type> *point);  //
中序遍歷二叉樹
void PosTree(TreeNode<Type> *point);  //
后序遍歷二叉樹
void Destroy(TreeNode<Type> *point);     //
銷毀二叉樹
bool ISEmpty();
protected:
TreeNode<Type>* root;
};


template<class Type>
void BinTree<Type>::CreatTree()
{
CreatTree(root,1);
}

template<class Type>
void BinTree<Type>::CreatTree(TreeNode<Type>* child,int k)
{
  TreeNode<Type>* point;
point=new TreeNode<Type>;
cout<<"
輸入節點數據項 :";
  cin>>point->data;
  switch(k)
{
case 1: root=point; break;
case 2: child->lchild=point;break;
case 3: child->rchild=point;break;
}

char temp;
  cout<<"
"<<point->data<<"節點是否有左子樹 Y / 任意鍵 :";
cin>>temp;
if(temp=='y'||temp=='Y')
{
     CreatTree(point,2);
}

  cout<<"
"<<point->data<<"節點是否有右子樹 Y / 任意鍵 :";
  cin>>temp;
  if(temp=='y'||temp=='Y')
{
  CreatTree(point,3);
}
}

template<class Type>
void BinTree<Type>::PreTree(TreeNode<Type> *point)
{
if(point!=NULL)
{
cout<<" "<<point->data;
PreTree(point->lchild);
PreTree(point->rchild);
}
}


template<class Type>
void BinTree<Type>::InoTree(TreeNode<Type> *point)
{
if(point!=NULL)
{
  
   InoTree(point->lchild);
cout<<" "<<point->data;
InoTree(point->rchild);
}
}

template<class Type>
void BinTree<Type>::PosTree(TreeNode<Type> *point)
{
if(point!=NULL)
{
  
PosTree(point->lchild);
PosTree(point->rchild);
cout<<" "<<point->data;
}
}


template<class Type>
bool BinTree<Type>::ISEmpty()
{
return root==NULL;
}

template<class Type>
void BinTree<Type>::Destroy(TreeNode<Type> *point)
{
if(point!=NULL)
{
Destroy(point->lchild);
Destroy(point->rchild);
delete point;
}
}


///////////////////////////
//    //
//  
基本功能函數 BaseFun.h   //
//    //
//////////////////////////

void GRAPH();
void LIST();
void STACK();
void QUEUE();
void COMPOSITOR();
void BINTREE();

///////////////////////////
//    //
//  
堆棧功能函數   Stack.cpp/ /
//    //
//////////////////////////



#include"Stack.h"
#include"iostream.h"


const int INT =13;
const double FLOAT= 13.33;
const char CHAR ='a';




template<class Type>
void Stack_Push(Stack<Type> &StackOPP)
{
cout<<"
請輸入要插入的數據項: ";
Type item;
cin>>item;
  StackOPP.Push(item);
}

template<class Type>
void Stack_Pop(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
{
   cout<<"
出棧數據項: ";
    cout<<StackOPP.Pop()<<endl;
}
else
{
cout<<"
堆棧已經為空!"<<endl;
}
}

template<class Type>
void Stack_ISEmpty(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
   cout<<"
堆棧不空,還有"<<StackOPP.GetNum()<<"數據項!"<<endl;
else
cout<<"
堆棧為空!"<<endl;
  
}

template<class Type>
void Stack_GetTop(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
  cout<<"
棧頂元素為:"<<StackOPP.GetTop()<<endl;
else
  cout<<"
堆棧為空!"<<endl;
}

template<class Type>
void Stack_MakeEmpty(Stack<Type> &StackOPP)
{
if(!StackOPP.ISEmpty())
{
StackOPP.MakeEmpty();
cout<<"
堆棧已經銷毀!"<<endl;
}
else
{
cout<<"
銷毀失敗!"<<endl;
}
}


template<class Type>
void StackINI(Type temp)
{
Stack<Type> StackOPP;

do
{
cout<<"
堆棧的操作: "<<endl
<<" 1)
插入堆棧"<<endl
<<" 2)
出棧"<<endl
<<" 3)
堆棧是否為空"<<endl
<<" 4)
得棧頂數據項"<<endl
<<" 5)
銷毀堆棧"<<endl
<<" X)
退出堆棧操作"<<endl;
int item;
cin>>item;
switch(item)
{
case 1: Stack_Push(StackOPP); break;
case 2: Stack_Pop(StackOPP);  break;
case 3: Stack_ISEmpty(StackOPP);  break;
case 4: Stack_GetTop(StackOPP); break;
case 5: Stack_MakeEmpty(StackOPP); break;

default: return ;
}

}while(true);


}


void STACK()
{
int item;
cout<<"
清選擇數據類型: 1) 整型 2) 浮點型 3) 字符型 X) 退出: ";

cin>>item;
switch(item)
{
case 1: StackINI(INT); break; //
根據不同的用戶需要選擇數據類型
case 2: StackINI(FLOAT); break;
case 3: StackINI(CHAR); break;
  default: return ; break;
}
}



///////////////////////////
//    //
//  
隊列功能函數 Queue.h  //
//    //
//////////////////////////



#include"Queue.h"

const int INT =13;
const double FLOAT= 13.33;
const char CHAR ='a';



template<class Type>
void Queue_Enter(Queue<Type> &QueueOPP)
{
cout<<"
請輸入要插入隊列的數據: ";
Type item;
cin>>item;
QueueOPP.EnQueue(item);
}

template<class Type>
void Queue_Del(Queue<Type> &QueueOPP)
{
  if(!QueueOPP.ISEmpty())
  {
  cout<<"
出隊數據:"<<QueueOPP.DelQueue()<<endl;
  }
  else
  {
  cout<<"
隊列已為空!"<<endl;
  }
}

template<class Type>
void Queue_ISEmpty(Queue<Type> &QueueOPP)
{
if(QueueOPP.ISEmpty())
{
cout<<"
隊列已空!"<<endl;
}
else
{
cout<<"
隊列不空!"<<endl;
}
}


template<class Type>
void Queue_GetFront(Queue<Type> &QueueOPP)
{
if(!QueueOPP.ISEmpty())
{
cout<<"
隊頭元素為: "<<QueueOPP.GetFront()<<endl;
}
else
{
cout<<"
隊列已空!"<<endl;
}
}

template<class Type>
void Queue_MakeEmpty(Queue<Type> &QueueOPP)
{
QueueOPP.MakeEmpty();
cout<<"
隊列清空!"<<endl;
}

template<class Type>
void QueueINI(Type temp)
{
Queue<Type> QueueOPP;

do
{
cout<<"
隊列的操作: "<<endl
<<" 1)
插入隊列"<<endl
<<" 2)
出隊"<<endl
<<" 3)
隊列是否為空"<<endl
<<" 4)
得隊頭數據項"<<endl
<<" 5)
銷毀隊列"<<endl
<<" X)
退出隊列操作"<<endl;
int item;
cin>>item;
switch(item)
{
case 1: Queue_Enter(QueueOPP); break;
case 2: Queue_Del(QueueOPP); break;
case 3: Queue_ISEmpty(QueueOPP);  break;
case 4: Queue_GetFront(QueueOPP); break;
case 5: Queue_MakeEmpty(QueueOPP); break;

default: return ;
}

}while(true);


}


void QUEUE()  //
根據不同的用戶需要選擇數據類型
{
int item;
cout<<"
清選擇數據類型: 1) 整型 2) 浮點型 3) 字符型 X) 退出: ";


cin>>item;
switch(item)
{
case 1: QueueINI(INT); break;  
case 2: QueueINI(FLOAT); break;
case 3: QueueINI(CHAR); break;
  default: return ; break;
}
}


///////////////////////////
//    //
//  
鏈表     List.h           //
//    //
//////////////////////////


#include"list.h"
#include<iostream.h>
#include<stdlib.h>


template<class type>
void initlist(type &tmp)
{
  list<type> List;
  int n;

  while(true)
  {

  cout<<"
請選擇你要對鏈表進行的操作 "<<endl
  <<"1)
在末尾插入數據"<<endl
   <<"2)
在任意處插入數據"<<endl
  <<"3)
刪除數據項"<<endl
  <<"4)
刪除整個鏈表"<<endl
  <<"5)
打印鏈表"<<endl
  <<"6)
查找數據項"<<endl
  <<"7)
退出"<<endl;

  cout<<">\\ ";
  cin>>n;

  while(n<1||n>7)
  {
cout<<"
輸入有誤,請從新輸入!"<<endl;
   cout<<">\\ ";
   cin>>n;
}

switch(n)
{
case 1: list_insertend(List);break;
case 2: list_insert(List);break;
case 3: list_delnode(List);break;
case 4: list_makeempty(List);break;
case 5: list_print(List);break;
case 6: list_find(List);break;
case 7: return ;break;
}

  }

}

void LIST()
{
int n;
  cout<<"
請選擇你要構造的鏈表的數據類型 1)整型,2)字符型,3)浮點型"<<endl;  
cout<<">\\ ";
cin>>n;

  while(n<1||n>3)
  {
cout<<"
輸入有誤,請從新輸入!"<<endl;
   cout<<">\\ ";
   cin>>n;
}

char t_c='c';
int t_i=12;
double t_f=23.3;

switch(n)
{
case 1:initlist(t_i);break;
case 2:initlist(t_c);break;
case 3:initlist(t_f);break;
}
}

template<class type>
void list_insertend(list<type> &L)
{
type t;
cout<<"
請輸入插入數據: >\\";
cin>>t;
L.insertend(t);
}

template<class type>
void list_find(list<type> &L)
{
type T;
cout<<"
請輸入你要查找的數據項:>\\ ";
cin>>T;

int i;
if(!(i=L.find(T)))
cout<<"
你要查找的數據項不存在!"<<endl;
else
cout<<"
你要查找的數據項在第"<<i<<"個位置"<<endl;
}

template<class type>
void list_insert(list<type> &L)
{

type t;
cout<<"
請輸入插入數據: >\\";
cin>>t;

int n;
cout<<"
請輸入插入位置: >\\";
cin>>n;
if(L.insert(t,n))
cout<<"
插入成功! "<<n<<"位置 插入"<<t<<endl;
else
cout<<"
插入失敗! 插入位置不正確!"<<endl;

}

template<class type>
void list_delnode(list<type>& L)
{
int i;
  cout<<"
請輸入要刪除數據項的位置: >\\";
cin>>i;


while(i<1||i>L.getlen())
{
    cout<<"
輸入有誤,可能大與鏈表長度,請從新輸入!"<<endl;
    cout<<">\\ ";
  cin>>i;
}

L.delnode(i);
}
template<class type>
void list_makeempty(list<type> &L)
{
L.makeempty();
}

template<class type>
void list_print(list<type> &L)
{
if(!L.print())
cout<<"
鏈表為空!"<<endl;
}


///////////////////////////
//    //
//  
圖功能函數  Graph.h    //
//    //
//////////////////////////


#include"Graph.h"

template<class NameType,class DisType>
void Graph_Creat(Graph<NameType,DisType> &GraphOPP)
{
GraphOPP.Creat();
}

template<class NameType,class DisType>
void Graph_DFS(Graph<NameType,DisType> &GraphOPP)
{
GraphOPP.DFS();
}

template<class NameType,class DisType>
void Graph_BFS(Graph<NameType,DisType> &GraphOPP)
{
GraphOPP.BFS();
}

template<class NameType,class DisType>
void Graph_PRINT(Graph<NameType,DisType> &GraphOPP)
{
GraphOPP.PrintNode();
}


void GRAPH()
{
Graph<char,int> GraphOPP;
do
{
cout<<"
圖的操作: "<<endl
<<" 1)
建立圖"<<endl
<<" 2)
圖的深度優先搜索"<<endl
<<" 3)
圖的廣度優先搜索"<<endl
<<" 4)
打印圖中各結點"<<endl
<<" X)
退出排序操作"<<endl;
int item;
cin>>item;
switch(item)
{
case 1: Graph_Creat(GraphOPP); break;
case 2: Graph_DFS(GraphOPP);  break;
case 3: Graph_BFS(GraphOPP);  break;
case 4: Graph_PRINT(GraphOPP);  break;
default: return ;
}

}while(true);


}

///////////////////////////
//    //
//  
排序算法功能函數    Compositor.cpp //
//    //
//////////////////////////



#include"Compositor.h"


const int INT =13;
const double FLOAT= 13.33;
const char CHAR ='a';

template<class type>
void CompositorINI(type temp)
{
Compositor<type> CompositorOPP;

do
{
cout<<"
排序的操作: "<<endl
<<" 1)
插入排序"<<endl
<<" 2)
快速排序"<<endl
<<" 3)
歸并排序"<<endl
<<" 4)
冒泡排序"<<endl
<<" 5)
選擇排序"<<endl
<<" X)
退出排序操作"<<endl
<<"
請選擇相應的操作: ";
int item;
cin>>item;
switch(item)
{
case 1: Compositor_Insert(CompositorOPP); break;
case 2: Compositor_Quick(CompositorOPP);  break;
case 3: Compositor_Merge(CompositorOPP);  break;
case 4: Compositor_Bubble(CompositorOPP); break;
case 5: Compositor_Select(CompositorOPP); break;

default: return ;
}

}while(true);


}

void COMPOSITOR()//
根據不同的用戶需要選擇數據類型
{
int item;
cout<<"
清選擇數據類型: 1) 整型 2) 浮點型 3) 字符型 X) 退出: ";


cin>>item;
switch(item)
{
case 1: CompositorINI(INT); break;  
case 2: CompositorINI(FLOAT); break;
case 3: CompositorINI(CHAR); break;
  default: return ; break;
}
}

template<class type>
void Compositor_Insert(Compositor<type> CompositorOPP)
{
CompositorOPP.Insert();
}

template<class type>
void Compositor_Quick(Compositor<type> CompositorOPP)
{
CompositorOPP.Quick();
}

template<class type>
void Compositor_Select(Compositor<type> CompositorOPP)
{
CompositorOPP.Select();
}

template<class type>
void Compositor_Merge(Compositor<type> CompositorOPP)
{
CompositorOPP.MergeSort();
}


template<class type>
void Compositor_Bubble(Compositor<type> CompositorOPP)
{
CompositorOPP.Bubble();
}

///////////////////////////
//    //
//  
二叉樹功能函數 BinTree.cpp//
//    //
//////////////////////////


#include<iostream.h>
#include"BinTree.h"

const int INT =13;
const double FLOAT= 13.33;
const char CHAR ='a';



template<class Type>
void BinTree_CREAT(BinTree<Type>& BinTreeOPP)
{
    BinTreeOPP. CreatTree();
}

template<class Type>
void BinTree_PRE(BinTree<Type>& BinTreeOPP)
{
if(!BinTreeOPP.ISEmpty())
{
   cout<<"
先序遍歷二叉樹 : ";
   BinTreeOPP. PreTree(BinTreeOPP.root);
}
else
{
cout<<"
二叉樹已經為空!"<<endl;
}
}

template<class Type>
void BinTree_INO(BinTree<Type>& BinTreeOPP)
{
if(!BinTreeOPP.ISEmpty())
{
   cout<<"
中序遍歷二叉樹 : ";
BinTreeOPP. InoTree(BinTreeOPP.root);
}
else
{
cout<<"
二叉樹已經為空!"<<endl;
}

}

template<class Type>
void BinTree_POS(BinTree<Type>& BinTreeOPP)
{
if(!BinTreeOPP.ISEmpty())
{
cout<<"
后序遍歷二叉樹 : ";
BinTreeOPP. PosTree(BinTreeOPP.root);
}
else
{
cout<<"
二叉樹已經為空!"<<endl;
}
}

template<class Type>
void BinTree_Destroy(BinTree<Type>& BinTreeOPP)
{
BinTreeOPP.Destroy(BinTreeOPP.root);
BinTreeOPP.root=NULL;
cout<<"
二叉樹已經銷毀!"<<endl;
}

template<class Type>
void BinTree_THREAD(BinTree<Type>& BinTreeOPP)
{
if(BinTreeOPP.ISThread())
{
cout<<"
該二叉樹已經線索化!!"<<endl;
}
else
{
BinTreeOPP.ThreadTree();
}
}

template<class Type>
void BinTree_THROUGH(BinTree<Type>& BinTreeOPP)
{
BinTreeOPP.Through();
}


template<class Type>
void BinTreeINI(Type temp)
{
BinTree<Type> BinTreeOPP;

do
{
cout<<"
樹的操作: "<<endl
<<" 1)
構造二叉數"<<endl
<<" 2)
先序遍歷二叉樹"<<endl
<<" 3)
中序遍歷二叉樹"<<endl
<<" 4)
后序遍歷二叉樹"<<endl
<<" 5)
銷毀二叉樹  "<<endl
<<" X)
退出二叉樹操作"<<endl;
int item;
cin>>item;
switch(item)
{
case 1: BinTree_CREAT(BinTreeOPP); break; //
構造二叉數
case 2: BinTree_PRE(BinTreeOPP);  break; //
先序遍歷二叉樹
case 3: BinTree_INO(BinTreeOPP); break;  //
中序遍歷二叉樹
  case 4: BinTree_POS(BinTreeOPP); break;   //
后序遍歷二叉樹
  case 5: BinTree_Destroy(BinTreeOPP);break;   //
求樹的深度
default: return ;
}

}while(true);


}

void BINTREE()
{
int item;
cout<<"
清選擇數據類型: 1) 整型 2) 浮點型 3) 字符型 X) 退出: ";


cin>>item;
switch(item)
{
case 1: BinTreeINI(INT); break; //
根據不同的用戶需要選擇數據類型
case 2: BinTreeINI(FLOAT); break;
case 3: BinTreeINI(CHAR); break;
  default: return ; break;
}
}


///////////////////////////
//    //
//  
主函數 index.cpp  用戶菜單 //
//    //
//////////////////////////


#include <iostream.h>
#include"BaseFun.h"

void main()
{
//
功能菜單
do
{
cout<<"
歡迎使用數據結構算法集"<<endl
<<"1)
線性表 "<<endl
<<"2)
堆棧 "<<endl
<<"3)
隊列 "<<endl
<<"4)
二叉樹 "<<endl
<<"5)
"<<endl
<<"6)
排序算法 "<<endl
<<"7)
字符串 "<<endl
<<"X)
按任意鍵退出 "<<endl;
  cout<<"
請您選擇何種數據結構的操作:"<<endl;
int kind;
cin>>kind;
switch(kind)
{
case 1: LIST(); break;
case 2: STACK(); break;
case 3: QUEUE(); break;
case 4: BINTREE(); break;
case 5: GRAPH(); break;
case 6: COMPOSITOR(); break;
default: return;
}
}while(true);

}

posted on 2005-12-24 19:22 夢在天涯 閱讀(2356) 評論(1)  編輯 收藏 引用 所屬分類: CPlusPlusData Arithmetic

評論

# re: 數據結構算法集---C++語言實現 2006-07-10 19:49 question

hi
i want a threadtree algorithem whould u mind help me?
this is my yahoo id --->best_dream_12@yahoo.com
tanx  回復  更多評論   

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1814982
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              国产精品久久久一区麻豆最新章节| 午夜激情一区| 这里只有视频精品| 在线视频欧美精品| 欧美在线视频观看| 欧美成人精品一区二区| 亚洲国产精品黑人久久久| 亚洲人成在线观看一区二区| 这里只有精品丝袜| 久久精品国产欧美亚洲人人爽| 男男成人高潮片免费网站| 欧美日韩和欧美的一区二区| 国产精品一区=区| 亚洲激情电影在线| 欧美在线不卡视频| 欧美激情精品久久久六区热门| 日韩午夜中文字幕| 久久精品免费观看| 国产精品久久久久久久久免费| 一区二区三区在线看| 夜夜嗨av一区二区三区四季av| 性色一区二区三区| 亚洲国产天堂久久国产91| 亚洲欧美日韩一区在线| 欧美另类亚洲| 亚洲欧洲一区二区三区| 欧美一级片久久久久久久| 亚洲国产美女| 久久亚洲欧美| 国产午夜精品美女视频明星a级| 9久草视频在线视频精品| 免费成人av| 久久精品99国产精品日本| 国产精品久久久久三级| 在线亚洲美日韩| 欧美激情中文不卡| 久久综合色一综合色88| 国内精品久久久久国产盗摄免费观看完整版 | 国产精品美女久久福利网站| 亚洲人成在线观看一区二区| 久久人人超碰| 午夜激情综合网| 国产精品成人aaaaa网站| 亚洲日本久久| 亚洲国产成人一区| 免费欧美在线| 亚洲欧洲日本一区二区三区| 久久视频精品在线| 午夜精品亚洲| 国产午夜精品美女视频明星a级| 欧美一区二区久久久| 亚洲女女做受ⅹxx高潮| 国产精品男人爽免费视频1 | 欧美精品日日鲁夜夜添| 国内外成人在线| 久久精品一本| 久久久国产91| 在线播放不卡| 欧美福利视频| 欧美精品成人在线| 一区二区三区精品国产| 日韩午夜剧场| 国产精品一卡二卡| 久久精品中文| 狂野欧美激情性xxxx欧美| 亚洲国产欧美日韩另类综合| 亚洲欧洲视频在线| 欧美日韩久久不卡| 亚洲欧美日韩综合一区| 亚洲欧美在线一区二区| 激情综合五月天| 亚洲福利国产| 国产精品成人一区二区艾草| 欧美在线视频一区二区| 久久综合色88| 亚洲一区二区三区欧美| 久久精品国产99精品国产亚洲性色| 国模私拍一区二区三区| 亚洲高清视频在线观看| 国产精品嫩草影院一区二区| 久久亚洲综合色| 欧美激情偷拍| 欧美一区二区三区在线播放| 久久另类ts人妖一区二区| 亚洲最黄网站| 久久成人精品视频| 99热在线精品观看| 亚洲欧美日韩国产综合在线| 亚洲黄色一区| 亚洲欧美日韩精品久久| 亚洲美女精品成人在线视频| 午夜国产精品影院在线观看| 亚洲精品国精品久久99热| 亚洲资源在线观看| 亚洲精品美女免费| 久久超碰97中文字幕| 亚洲私人影院| 美女视频一区免费观看| 欧美一站二站| 欧美视频中文字幕在线| 欧美a级片一区| 国产欧美一区二区三区在线老狼| 亚洲国产欧美日韩另类综合| 国产一区日韩二区欧美三区| 一二三区精品| 日韩视频免费观看高清完整版| 久久精品国产91精品亚洲| 亚洲免费视频在线观看| 欧美成人高清视频| 久久免费观看视频| 国产精品无码永久免费888| 亚洲黄色av一区| 亚洲国产精品v| 久久精品国产96久久久香蕉| 欧美一区二区日韩| 欧美性色aⅴ视频一区日韩精品| 亚洲第一黄网| 亚洲福利小视频| 亚洲国产一区二区三区高清| 国产日韩在线播放| 亚洲欧美日韩系列| 亚洲欧美中日韩| 欧美日韩一区在线| 亚洲国产欧洲综合997久久| 亚洲第一中文字幕| 久久九九免费视频| 久久综合久久综合九色| 国内成+人亚洲| 欧美在线精品一区| 久久久久九九九| 狠狠色香婷婷久久亚洲精品| 欧美专区在线| 欧美www视频在线观看| 在线播放中文一区| 快播亚洲色图| 亚洲精品久久| 亚洲午夜免费视频| 国产精品性做久久久久久| 亚洲性视频网址| 欧美在线视频导航| 在线成人中文字幕| 欧美黄色一区| 亚洲美女视频在线观看| 亚洲天堂av在线免费| 国产精品丝袜白浆摸在线| 亚洲自拍另类| 久热精品在线| 亚洲精品美女在线| 欧美视频在线观看| 欧美亚洲视频| 亚洲国产合集| 亚洲一区在线观看视频| 国产欧美午夜| 麻豆av福利av久久av| 亚洲精品久久在线| 欧美一区二区三区婷婷月色| 在线电影院国产精品| 欧美日韩人人澡狠狠躁视频| 亚洲专区在线视频| 欧美国产日韩视频| 亚洲欧美日韩在线| 国内精品伊人久久久久av影院 | 国产精品久久久一本精品| 欧美一区二区观看视频| 亚洲高清中文字幕| 午夜精品久久久久久久白皮肤| 国内精品久久久久影院色| 欧美第十八页| 欧美亚洲一区在线| 亚洲精品免费网站| 久久久久久伊人| 亚洲视频一区二区在线观看| 极品av少妇一区二区| 欧美午夜电影在线| 老巨人导航500精品| 亚洲一二三区精品| 欧美国产另类| 欧美在线视频不卡| 一区二区激情视频| 亚洲国产精品久久久久久女王| 国产精品美女久久久| 欧美成人国产| 久久久噜噜噜久久中文字幕色伊伊 | 亚洲自拍电影| 亚洲精品中文在线| 极品尤物av久久免费看 | 欧美国产第二页| 欧美在线视频播放| 亚洲视频自拍偷拍| 亚洲麻豆av| 亚洲国产精品视频| 国产一区二区福利| 国产精品久久久99| 欧美精品久久久久久久免费观看 | 亚洲人成在线观看网站高清| 国语自产精品视频在线看8查询8| 欧美美女bb生活片| 欧美成人福利视频| 浪潮色综合久久天堂| 久久久久久**毛片大全|