数据结构算法集

算法:求幂集(回溯法与树的遍历)-数据结构(17) 一、问题描述 求幂集。参见书上P149-P150,思想是这样的,为了求幂集,应对每个元素都采取两种选择,要与不要,因此形成了一棵完全二叉树,遍历这棵完全二叉树便是求出幂集的解决方法。 二、算法与数据结构 1、顺序列表 //顺序表 2017-04-23 // c2-1.h 线性表的动态分配顺序存储结构。在教科书第22页 #define LIST_INIT_SIZE 30 // 线性表存储空间 阅读详情
/////////////////////////// 
//    // 
//   堆栈数据结构   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); 
}/////////////////////////// 
//    // 
//   堆栈数据结构   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); 
}
数据结构算法题集及解答1800题 本文还有配套的精品资源,点击获取 简介:本资源提供了一套全面的数据结构练习题,包括1800道题目及其完整答案,覆盖了数组、链表、栈、队列、树、图、哈希表、排序和查找等数据结构主题。资源适合学习者和程序员使用,以加强理论知识和实际编程能力。题目不仅涉及基础操作,还包含了各种数据结构的高级应用,如深度优先搜索、广度优先搜索和最短路径算法等。同时,题目可能是用Java语言编写,需... 阅读详情

相关推荐

数据结构与算法代码实战集

本文还有配套的精品资源,点击获取 简介:数据结构是计算机科学中的基础,涉及数据的有效组织和管理以优化操作效率。正确的数据结构选择对算法性能至关重要。本资源提供了一套常用数据结构如数组、链表、栈、队列、树、图、散列表(哈希表)、堆等的C++实现代码,以及排序和查找算法的实现。通过实际代码学习,你将深刻理解数据结构的工作原理和应用,提高问题解决能力,并利用Visual C++环...

weixin_34438187的博客 1202

数据结构与算法题目集

准备考研复试中,正在重新学习数据结构,pta上的数据结构与算法题目集,由于复试要求用c语言,所以全文均由c语言实现,持续更新ing

保持学习 781

Demo2Reward:用人类演示动态优化VLM奖励模型提示

多模态大模型(VLM)的奖励建模正面临核心瓶颈——静态提示导致语义鸿沟与泛化失效。其本质是将人类判断标准固化为不可更新的文本模板,使奖励信号脱离真实任务意图。Demo2Reward提出范式级改进:以结构化人类演示为元信号,驱动提示权重连续优化,实现从‘结果打分’到‘过程理解’的跃迁。该方法不修改VLM主干,却显著提升RLHF收敛效率与OOD鲁棒性,在医疗、工业质检等场景验证了跨任务迁移与热修复能力。它重新定义了提示工程的技术价值:不是前端微调,而是奖励系统的认知校准基础设施。

weixin_34302798的博客 408

数据结构算法设计C++乐学答案集

数据结构算法设计C++乐学答案集 去发现同类优质开源项目:https://gitcode.com/ 此仓库包含了《数据结构算法设计C++》在北理乐学平台上的测试答案。这些答案旨在帮助学习者在学习数据结构与算法设计时,能够对照检验自己的学习成果,加深对课程内容的理解。 仓库中的资源是经过整理的测试答案,适用于使用C++语言进行数据结构与算法设计学习的读者。这些答案不仅包括问题解答,也可能涉及解题思路...

gitblog_06719的博客 254

JavaScript性能优化:全链路性能优化策略

JavaScript性能优化是提升Web应用质量的核心要素,直接影响用户体验与业务成果。研究表明,页面加载延迟1秒可能导致转化率下降7%。高效的JavaScript执行能减少主线程阻塞,避免动画卡顿或操作延迟,尤其在移动端低性能设备上更为关键。优化内存管理与减少冗余计算可降低浏览器资源消耗,防止因内存泄漏导致的崩溃,提升应用稳定性。在竞争激烈的互联网环境中,性能优势已成为产品差异化的关键因素,直接影响用户留存与商业价值。因此,性能优化应作为开发全周期的核心实践,而非事后补救措施。

weixin_49707375的博客 1270

OpenClaw安装方式那么多,但建议大家还是用云厂商吧

OpenCloud的安装方式多样,包括One-liner、npm、pnpm和Hackable等。One-liner提供高效的单行命令安装;npm和pnpm作为Node.js包管理工具,分别提供常规和高性能的安装选项;Hackable则支持定制化安装。不同系统(macOS、Windows)和终端(PowerShell、CMD)的操作步骤各有特点,用户需根据自身技术背景选择合适的安装方式。官方推荐使用闲置计算资源或云部署方案以获得更佳体验。

涛涛讲AI 1399

数据结构算法设计C++乐学答案集:提升编程能力的利器

数据结构算法设计C++乐学答案集:提升编程能力的利器 去发现同类优质开源项目:https://gitcode.com/ 项目核心功能/场景 为《数据结构算法设计C++》学习提供测试答案及解题分析。 项目介绍 在当今信息时代的浪潮中,掌握数据结构和算法设计是计算机科学领域的基础和核心。为此,北理乐学平台推出的《数据结构算法设计C++》课程,旨在帮助学习者系统地掌握C++语言在数据结构中的应用。而“数...

gitblog_06769的博客 581

【COMSOL超材料和超表面仿真设计】

超材料和超表面的设计通常需要自定义材料属性。在COMSOL中,可以通过定义介电常数和磁导率的张量来模拟超材料的特性。对于超表面,通常需要定义表面阻抗或等效电路模型。

weixin_51101456的博客 1661

浙大PTA数据结构与算法题目集(中文)题解集复习用

7-1 最大子列和问题 (20分)(dp或贪心) 给定K个整数组成的序列{ N1, N2, …, N**K },“连续子列”被定义为{ N**i, N**i+1, …, N**j },其中 1≤i≤j≤K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。 本题旨在测试各种不同的算法在各种数据情况下的表现。各组测试数据特点如下: 数据1

qq_41342326的博客 1万+

Android汽车服务篇(一) CarService

本文主要说明了CarService这一系统服务的启动流程,以及一些关键成员的创建和初始化流程.同时简单介绍了一下客户端是如何获取和使用CarService对象, 如上文介绍的, CarService对象只是获取各个Manager的媒介,它本身并不承担管理传感器.空调等具体的任务.如果要获取车辆的相关信息,如车速, 车内空调温度,诊断信息, 或对车辆进行相关的控制,如升高和降低空调温度,控制座椅和车窗, 调节音量等,就要通过具体的Manager中的API和对应的服务来实现了.

u012514113的专栏 8145

Openclaw错误分析与解决方案之:All models are temporarily rate-limited. Please try again in a few minutes

Openclaw错误分析与解决方案之: All models are temporarily rate-limited. Please try again in a few minutes.

多媒体编程、网络编程、系统编程、网络安全编程、驱动编程 1618

java数据结构和算法 北风 下载_Java] 北风Java20集+44集版数据结构算法基础教程

20集版本第一讲数组.rar第二讲简单排序.A危i第三讲栈和队列.A危i第四讲链表.A危i第五讲双端链表和双向链表.A危i第六讲递归的应用.A危i第七讲递归的高级应用.A危i第八讲希尔排序.A危i第九讲快速排序.A危i第十讲二叉树的基本概念.A危i第十一讲二叉树的基本操作.A危i第十二讲遍历二叉树.A危i第十三讲删除二叉树节点.A危i第十四讲红黑树.A危i第十五讲哈希表.A危i第十六讲开放地址法....

weixin_35703208的博客 178

基于深度学习的ECG心电信号异常分类:从数据集到Transformer实战

心电图(Electrocardiogram, ECG)是诊断心血管疾病最常用、最无创的工具。随着可穿戴设备的普及,海量的心电数据需要自动、快速且准确地分析。人工智能,尤其是深度学习,在心电信号分类任务中表现出了超越传统规则算法的能力。本文将深入探讨ECG异常分类的技术细节,从心电数据的生理基础,到数据预处理,再到使用1D-CNN、LSTM、以及前沿的Transformer模型进行实战演练。文章末尾提供了完整的代码以及多个国际公开数据集的获取方式。

YOLO 188

数据结构与算法题目集PTA

PTA数据结构算法题集-个人解法仅供参考

小白 1214

【报错】(pycharm运行程序)OSError: [WinError 1114] 动态链接库(DLL)初始化例程失败。 Error loading

1 报错 OSError: [WinError 1114] 动态链接库(DLL)初始化例程失败。 Error loading 2 分析原因 通过 activate Distance 进行自己创建的 Distance 虚拟环境中,输入python会显示环境中python的版本信息,再依次import,显然不会报错。 这是因为在Anaconda的环境下有支持 import torch 的 dll,而在PyCharm中使用的是自己创建的新环境,还缺少相应支持的dll,仅需在PyCharm中配置环境变量。.

静谧、淡雅 8621

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

PTA习题:数据结构与算法题目集1

PTA习题:数据结构与算法题目集1 去发现同类优质开源项目:https://gitcode.com/ 资源描述 本仓库提供了一个名为“PTA习题:数据结构与算法题目集1”的资源文件下载。该资源文件包含了数据结构与算法相关的练习题目,适合正在学习或复习数据结构与算法的学生和开发者使用。 资源内容 资源文件中包含以下题目: 题目1:最大子列和问题 题目描述: 给定K个整数组成的序列{ N1, N2, ...

gitblog_06645的博客 408

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

数据结构题集-第二章-线性表-算法设计题三四

实现和测试了两个算法:比较两个顺序表的大小,定位数据在链表中第一次出现的位置,从动态数组到静态链表,各自取长补短、相辅相成。

georangel 1202

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

C++编程,数据结构,算法类面试题集

1. 在linked list中找倒数第N个结点 2. 倒转linked list 3. 二叉树的结点有指向parent的指针,求最近公共祖先 4. 给一个数组,如何打印该数组成员构成集合的全部子集合. 5. 有两个字符串,一个是text,一个是command, Command有四种:    ‘+’: 在text中前进一位    ‘-’: 在text中后退一位    ‘a’: 在当前

rongwenbin的专栏 1万+

数据结构与算法之位运算: LeetCode 2166. 设计位集 (Ts版)

1 ) 方案1:利用数组实现。2 ) 方案2:另一种方式。

Wang的专栏 467

CC数据结构排序算法资源集:助您轻松掌握八大排序算法

CC数据结构排序算法资源集:助您轻松掌握八大排序算法 去发现同类优质开源项目:https://gitcode.com/ 项目介绍 在现代计算机科学中,排序算法是数据结构学习的基础,更是程序设计不可或缺的一部分。今天,我们要为您推荐的是一个专门针对C/C++语言开发的资源集——CC数据结构排序算法资源集。这个项目集合了八大经典排序算法,旨在帮助开发者和学习者快速掌握这些算法的实现和应用。 项目技术分...

gitblog_06734的博客 335

JS数据结构与算法 —— 集合,并集,交集,补集

概念:集合是由一组无序且唯一(每个元素只出现一次)的项组成的一组数据。其与数学里的集合是同一个概念。在ES6里已经引入了集合的数据结构概念——Set类。 分类:常见的有空集,并集,交集,差集。 应用场景:1)数据去重;2)用于存储一些独一无二的数据。 js实现一个集合 集合的特性类似于JavaScript数据类型里的Object,Object对象里的每个键都是唯一。下面用一个名为item的...

知行合一,跬步致远 1898

PTA-数据结构与算法题目集(中文)— 编程题

PTA题目集题解

weixin_74229477的博客 1106

数据结构算法集--C++语言实现

/**//////////////////////////// //    // //  基本功能函数 BaseFun.h   // //    // /**//////////////////////////// void GRAPH(); void LIST(); void STACK(); void QUEUE(); void COMPOSITOR(); void BINTREE(); /*

阿丹的彩蛋 1056

C/C++数据结构排序算法资源集

C/C++数据结构排序算法资源集 去发现同类优质开源项目:https://gitcode.com/ 此资源文件包含了一个压缩包,内含8大排序算法的源代码,适用于C/C++数据结构学习。资源包括但不限于以下排序算法的实现: 冒泡排序 选择排序 插入排序 快速排序 归并排序 希尔排序 堆排序 计数排序 压缩包中的源代码允许您通过更改MAX值来调整随机数的数量,从而满足不同教学或实验的需求。此代码集...

gitblog_06702的博客 350

数据结构与算法[求A,b集合的并集操作]

package test; import java.util.ArrayList; import java.util.List; /** * 求A,b集合的并集操作 */ public class UnionL { public List getNewList(List a,List b){ // int size = a.size(); //idea自动补全返回...

weixin_39209728的博客 3467

数据结构题集-第二章-线性表-算法设计题二

完整解决了将一个数插入顺序存储的有序线性表,并保持表的有序性,提供自动生成的测试数据,并调用了C标准中的qsort算法。

georangel 1299
上一篇: 快速排序算法和二分搜索算法
下一篇: 揭开私有继承的面纱
mal327
博客等级 码龄17年 155粉丝 87原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值