7、c++版,在大学学的编程基础知识

1、各种排序

#include<iostream>
using namespace std;
//-------直接插入排序
void InsertSort(ElemType A[],int n)
{
    ElemType x;
    int i,j;
    for(i=1;i<n;i++)
    {
        x=A[i];
        for(j=i-1;j>=0;j--)
            if(x.stn<A[j].stn) A[j+1]=A[j];
            else break;
        A[j+1]=x;
    }
}
//----------------------
//-------希尔排序-----
void ShellSort(ElemType A[],int n)
{
    ElemType x;
    int i,j,d;
    for(d=n/2;d>=1;d/=2)
    {
        for(i=d;i<n;i++)
        {
            x=A[i];
            for(j=i-d;j>=0;j-=d)
            {
                if(x.stn<A[j].stn) A[j+d]=A[j];
                else break;
            }
            A[j+d]=x;
        }
    }
}
///-----直接选择排序--
void SelectSort(ElemType A[],int n)
{
    ElemType x;
    int i,j,k;
    for(i=1;i<=n-1;i++)
    {
        k=i-1;
        for(j=i;j<=n-1;j++)
        {
            if(A[j].stn<A[k].stn) k=j;
        }
        if(k!=i-1)
        {
            x=A[i-1];A[i-1]=A[k];A[k]=x;
        }
    }
}
//---------冒泡排序----------
void BubbleSort(ElemType A[],int n)
{
    ElemType x;
    int i,j,flag;
    for(i=1;i<=n-1;i++)
    {
        flag=0;
        for(j=n-1;j>=i;j--)
            if(A[j].stn<A[j-1].stn)
            {
                x=A[j-1];A[j-1]=A[j];A[j]=x;
                flag=1;
            }
        if(flag==0) return;
    }
}
int main()
{
    
    return 0;
    }
 

2、邻接矩阵,外加克鲁斯卡尔,弗洛伊德,普里姆等算法,进行图的遍历,和最小生成树的处理

#include<iostream.h>
#include<stdlib.h>
#include<strstrea.h>
typedef int VertexType;
typedef int WeightType;
const int MaxEdgeNum=10;
const int MaxVertexNum=10;
const WeightType MaxValue=1000;
typedef VertexType vexlist[MaxVertexNum];
typedef int adjmatrix[MaxVertexNum][MaxVertexNum];
struct edgenode
{
    int adjvex;
    WeightType weight;
    edgenode*next;
};
typedef edgenode*adjlist[MaxVertexNum];
struct edge
{
    int fromvex;
    int endvex;
    WeightType  weight;
};
typedef edge edgeset[MaxEdgeNum];
//#include"4_01栈的顺序存储.cpp"
void PATH(edgenode*path[],int m,int j)
{
    edgenode *p,*q,*s;
    p=path[j];
    while(p!=NULL)
    {
        path[j]=p->next;
        delete p;
        p=path[j];
    }
    p=path[m];
    while(p!=NULL)
    {
        q=new edgenode;
        q->adjvex=p->adjvex;
        if(path[j]==NULL) path[j]=q;
        else s->next=q;
        s=q;
        p=p->next;
    }
    q=new edgenode;
    q->adjvex=j;
    q->next=NULL;
    s->next=q;
}
void Dijkstra(adjmatrix GA, int dist[],edgenode* path[], int i, int n)
{
    int j,k,w,m;
    bool* s=new bool[n];
    //分别给s,dist和path数组赋初值
    for(j=0; j<n; j++) {
        if(j==i) s[j]=true; else s[j]=false;
        dist[j]=GA[i][j];
        if(dist[j]<MaxValue && j!=i) {
            edgenode* p1=new edgenode;
            edgenode* p2=new edgenode;
            p1->adjvex=i; p2->adjvex=j; p2->next=NULL;
            p1->next=p2; path[j]=p1;
        }
        else
            path[j]=NULL;
    }
    for(k=1; k<=n-2; k++)
    {
        //求出第k个终点m
        w=MaxValue; m=i;
        for(j=0; j<n; j++)
            if(s[j]==false && dist[j]<w) {
                w=dist[j]; m=j;
            }
            if(m!=i) s[m]=true;
            else break;
            for(j=0; j<n; j++)
                if(s[j]==false && dist[m]+GA[m][j]<dist[j]) {
                    dist[j]=dist[m]+GA[m][j];
                    PATH(path, m, j);
                }
    }
}

void Floyed(adjmatrix GA,adjmatrix A,int n)
{
    int i,j,k;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            A[i][j]=GA[i][j];
    for(k=0;k<n;k++)
        for(i=0;i<n;i++)
            for(j=0;j<n;j++){
                if(i==k || j==k || i==j) continue;
                if(
                    A[i][k]+A[k][j]<A[i][j])
                    A[i][j]=A[i][k]+A[k][j];
            }
}

void Prim(adjmatrix GA,edgeset CT,int n)
{
    int i,j,k,min,t,m,w;
    for(i=0;i<n-1;i++)
    {
        CT[i].fromvex=0;
        CT[i].endvex=i+1;
        CT[i].weight=GA[0][i+1];
    }
    for(k=1;k<n;k++)
    {
        min=MaxValue;
        m=k-1;
        for(j=k-1;j<n-1;j++)
            if(CT[j].weight<min)
            {
                min=CT[j].weight;
                m=j;
            }
            edge temp=CT[k-1];
            CT[k-1]=CT[m];
            CT[m]=temp;
            j=CT[k-1].endvex;
            for(i=k;i<n-1;i++)
            {
                t=CT[i].endvex;
                w=GA[j][t];
                if(w<CT[i].weight)
                {
                    CT[i].weight=w;
                    CT[i].fromvex=j;
                }
            }
    }
}
void Kruskal(edgeset GE,edgeset CT,int n)
{
    int i,j;
    bool**s=new bool*[n];
    for(i=0;i<n;i++)
        s[i]=new bool[n];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            if(i==j) s[i][j]=true;
            else s[i][j]=false;
    }
    int k=1;
    int d=0;
    int m1,m2;
    while(k<n)
    {
        for(i=0;i<n;i++)
        {
            if(s[i][GE[d].fromvex]==true) m1=i;
            if(s[i][GE[d].endvex]==true) m2=i;
        }
        if(m1!=m2)
        {
            CT[k-1]=GE[d];
            k++;
            for(j=0;j<n;j++)
            {
                s[m1][j]=s[m1][j]||s[m2][j];
                s[m2][j]=false;
            }
        }
        d++;
    }
    for(i=0;i<n;i++)
        delete[]s[i];
    delete[]s;
}
//边集数组

void InitArray(edgeset GE)
{
    for(int i=0;i<MaxEdgeNum;i++)
    {
        GE[i].fromvex=GE[i].endvex=-1;
        GE[i].weight=MaxValue;
    }
}
void CreateArray(edgeset GE,int n,char*s,int k)
{
    istrstream sin(s);
    char c1,c2,c3;
    int i,j,c=0;
    WeightType w;
    sin>>c1;
    if(k==0){
        do{
            sin>>c1>>i>>c2>>j>>c3;
            GE[c].fromvex=i;
            GE[c].endvex=j;
            GE[c].weight=1;
            c++;
            sin>>c1;
        }while(c1==',');
    }
    else{
        do{
            sin>>c1>>i>>c2>>j>>c3>>w;
            GE[c].fromvex=i;
            GE[c].endvex=j;
            GE[c].weight=w;
            c++;
            sin>>c1;
        }while(c1==',');
    }
}
void Arry(edgeset GE,int i,int n,bool*visited)
{
    int j=i,k=0;
    while(k!=n)
    {
        for(int t=0;GE[t].fromvex!=-1;t++)
        {
            if(GE[t].fromvex==j&&!visited[j])
            {
                cout<<j<<" ";
                visited[j]=true;
                j=GE[t].endvex;
                k++;
                break;
            }
        }
        if(GE[t].fromvex==-1)
        {
            cout<<j<<" ";
            k++;
        }
    }
}
void Arry2(edgeset GE,int i,int n,bool*visited)
{
    int j=i,k=1;
    int q[10];
    int front=0,rear=0;
    cout<<j<<" ";
    while(k!=n)
    {
        for(int t=0;GE[t].fromvex!=-1;t++)
        {
            if(GE[t].fromvex=j&&!visited[j])
            {
                cout<<GE[t].endvex<<" ";
                k++;
                q[rear]=GE[t].endvex;
                rear=(rear+1)%10;
            }
            else 
            {
                visited[j]=true;
                j=q[front];
                front=(front+1)%10;
                break;
            }
        }
    }
}


void InitAdjoin(adjlist GL)
{
    for(int i=0;i<MaxVertexNum;i++)
        GL[i]=NULL;
}
void CreateAdjoin(adjlist GL,int n,char*s,int k1,int k2)
{
    istrstream sin(s);
    char c1,c2,c3;
    int i,j;
    WeightType w;
    edgenode*p;
    sin>>c1;
    if(k2==0){
        do{
            sin>>c1>>i>>c2>>j>>c3;
            p=new edgenode;
        
            p->adjvex=j;p->weight=1;
            p->next=GL[i];
            GL[i]=p;
            if(k1==0){
                p=new edgenode;
                p->adjvex=i;p->weight=1;
                p->next=GL[j];
                GL[j]=p;
            }
            sin>>c1;
        }while(c1==',');
    }
    else{
        do{
            sin>>c1>>i>>c2>>j>>c3>>w;
            p=new edgenode;
            p->adjvex=j;p->weight=w;
            p->next=GL[i];
            GL[i]=p;
            if(k1==0)
            {
                p=new edgenode;
                p->adjvex=i;p->weight=w;
                p->next=GL[j];
                GL[j]=p;
            }
            sin>>c1;
        }while(c1==',');
    }
}

void InitMatrix(adjmatrix GA,int k)
{
    int i,j;
    for(i=0;i<MaxVertexNum;i++)
        for(j=0;j<MaxVertexNum;j++)
            if(i==j) GA[i][j]=0;
            else if(k) GA[i][j]=MaxValue;
            else GA[i][j]=0;
}
void CreateMatrix(adjmatrix GA,int n,char*s,int k1,int k2)
{
    istrstream sin(s);
    char c1,c2,c3;
    int i,j;
    WeightType w;
    sin>>c1;
    if(k1==0&&k2==0)
        do{
            sin>>c1>>i>>c2>>j>>c3;
            GA[i][j]=GA[j][i]=1;
            sin>>c1;
            if(c1==')')break;
        }while(1);
    else if(k1==0&&k2!=0)
        do{
            sin>>c1>>i>>c2>>j>>c3>>w;
            GA[i][j]=GA[j][i]=w;
            sin>>c1;
            if(c1==')')break;
        }while(1);
        else if(k1!=0 && k2==0)
            do{
                sin>>c1>>i>>c2>>j>>c3;
                GA[i][j]=1;
                sin>>c1;
                if(c1==')')break;
            }while(1);
            else if(k1!=0 && k2!=0)
                do{
                    sin>>c1>>i>>c2>>j>>c3>>w;
                    GA[i][j]=w;
                    sin>>c1;
                    if(c1==')')break;
                }while(1);
}
void dfsMatrix(adjmatrix GA,int i,int n,bool*visited)
{
    Stack M;
    InitStack(M);
    int j=i;
    visited[i]=true;
    cout<<j<<" ";
    Push(M,j);
    while(j!=n||M.top!=0)
    {
        if(j==n+1)
        {
            j=M.stack[M.top];
            M.top--;
        }
        if(GA[M.stack[M.top]][j]!=0&&!visited[j])
        {
            visited[j]=true;
            Push(M,j);
            cout<<j<<" ";
            j=0;
        }
        else
            j++;
    }
}

/*void dfsAdjoin(adjlist GL,int i,int n,bool*visited)
{
    cout<<i<<" ";
    visited[i]=true;
    edgenode*p=GL[i];
    while(p!=NULL)
    {
        int j=p->adjvex;
        if(!visited[j])
            dfsAdjoin(GL,j,n,visited);
        p=p->next;
    }
}*/
void dfsAdjoin(adjlist GL,int i,int n,bool*visited)
{
    Stack M;
    InitStack(M);
    cout<<i<<" ";
    Push(M,i);
    visited[i]=true;
    edgenode*p=GL[i];
    while(p!=NULL||M.top!=0)
    {
        if(p==NULL)
        {
            M.top--;
            p=GL[M.stack[M.top]];
        }
        int j=p->adjvex;
        
        
        if(!visited[j])
        {
            visited[j]=true;
            cout<<j<<" ";
            Push(M,j);
            p=GL[M.stack[M.top]];
        }
        else
        {
            p=p->next;
        }
    }
}
            




int main()
{
    int i,n,k1,k2;
    cout<<"输入待处理图的顶点数:";
    cin>>n;
    cout<<"输入图的有向和有无权选择(0为无,非0为有):";
    cin>>k1>>k2;
    bool*visited=new bool[n];
    adjmatrix ga;
    InitMatrix(ga,k2);
    cout<<"输入图的边集;";
    char*a=new char[100];
    cin>>a;
    CreateMatrix(ga,n,a,k1,k2);
    cout<<"按图的邻接矩阵得到的深度优先遍历序列:"<<endl;
    for(i=0;i<n;i++) 
        visited[i]=false;
    dfsMatrix(ga,0,n,visited);
    cout<<endl;
    cout<<"按图的邻接表得到的深度优先遍历序列:"<<endl;
    adjlist ka;
    InitAdjoin(ka);
    CreateAdjoin(ka,n,a,k1,k2);
    for(i=0;i<n;i++) 
        visited[i]=false;
    dfsAdjoin(ka,0,n,visited);
    cout<<endl;
    for(i=0;i<n;i++) 
        visited[i]=false;
    edgeset ke;
    InitArray(ke);
    CreateArray(ke,n,a,0);
    //Arry(ke,0,n,visited);
    //cout<<endl;
    //for(i=0;i<n;i++) 
        visited[i]=false;
    //Arry2(ke,0,n,visited);


    return 0;
}

3、栈的顺序存储

//typedef int ElemType;
/*struct Stack
{
    ElemType *stack;
    int top;
    int MaxSize;
};*/

//1. 初始化栈S为空
void InitStack(Stack& S)
{
    //初始设置栈空间大小为10
    S.MaxSize=100;
    //动态存储空间分配
    S.stack=new ElemType[S.MaxSize];
    if(!S.stack){
        cout<<"动态存储分配失败!"<<endl;
        exit(1);
    }
    //初始置栈为空
    S.top=-1;
}
//2. 元素item进栈,即插入到栈顶
void Push(Stack& S, ElemType item)
{
    //若栈空间用完则自动扩大2倍空间,原有栈内容不变
    if(S.top==S.MaxSize-1) {
        int k=sizeof(ElemType);  //计算每个元素存储空间的长度
        S.stack=(ElemType*)realloc(S.stack, 2*S.MaxSize*k);
        S.MaxSize=2*S.MaxSize;   //把栈空间大小修改为新的长度
    }
    //栈顶指针后移一个位置
    S.top++;
    //将新元素插入到栈顶
    S.stack[S.top]=item;
}
//3. 删除栈顶元素并返回
ElemType Pop(Stack& S)
{
    //若栈空则退出运行
    if(S.top==-1) {
        cout<<"栈空,退出运行!"<<endl; 
        exit(1);
    }
    //栈顶指针减1表示退栈
    S.top--;
    //返回原栈顶元素的值
    return S.stack[S.top+1];
}
//4. 读取栈顶元素的值
ElemType Peek(Stack& S)
{
    //若栈空则退出运行
    if(S.top==-1) {
        cout<<"Stack is empty!"<<endl; 
        exit(1);
    }
    //返回栈顶元素的值
    return S.stack[S.top];
}
//5. 判断栈是否为空
bool EmptyStack(Stack& S)
{
    return S.top==-1;
}
//6. 清除栈中的所有元素并释放动态存储空间
void ClearStack(Stack& S)
{
    if(S.stack) {
        delete []S.stack;
        S.stack=0;
    }
    S.top=-1;
    S.MaxSize=0;
}

4、边集数组深度优先搜索

void Arry(edgeset GE,int i,int n,bool*visited)
{
    int j=i,k=0;
    while(k!=n)
    {
        for(int t=0;GE[t].fromvex!=-1;t++)
        {
            if(GE[t].fromvex==j&&!visited[j])
            {
                cout<<j<<" ";
                visited[j]=true;
                j=GE[t].endvex;
                k++;
                break;
            }
        }
        if(GE[t].fromvex==-1)
        {
            cout<<j<<" ";
            k++;
        }
    }
}

5、非递归邻接表

void dfsAdjoin(adjlist GL,int i,int n,bool*visited)
{
    Stack M;
    InitStack(M);
    cout<<i<<" ";
    Push(M,i);
    visited[i]=true;
    edgenode*p=GL[i];
    while(p!=NULL||M.top!=0)
    {
        if(p==NULL)
        {
            M.top--;
            p=GL[M.stack[M.top]];
        }
        int j=p->adjvex;
        
        
        if(!visited[j])
        {
            visited[j]=true;
            cout<<j<<" ";
            Push(M,j);
            p=GL[M.stack[M.top]];
        }
        else
        {
            p=p->next;
        }
    }
}

6、非递归链接矩阵

void dfsMatrix(adjmatrix GA,int i,int n,bool*visited)
{
    Stack M;
    InitStack(M);
    int j=i;
    visited[i]=true;
    cout<<j<<" ";
    Push(M,j);
    while(j!=n||M.top!=0)
    {
        if(j==n+1)
        {
            j=M.stack[M.top];
            M.top--;
        }
        if(GA[M.stack[M.top]][j]!=0&&!visited[j])
        {
            visited[j]=true;
            Push(M,j);
            cout<<j<<" ";
            j=0;
        }
        else
            j++;
    }
}

7、哈夫曼编码

#include<iostream>
using namespace std;
#include<stdlib.h>
#include<fstream>
#include<string>
typedef int ElemType;
struct save
{
    char a2[10];
    int lenth;
};
save minma[26];
struct BTreeNode
{
    ElemType data;
    BTreeNode*left;
    BTreeNode*right;
};
int count=0;
BTreeNode*CreateHuffman(ElemType a[],int n)
{
    BTreeNode**b,*q;
    b=new BTreeNode*[n];
    int i,j;
    for(i=0;i<n;i++)
    {
        b[i]=new BTreeNode;
        b[i]->data=a[i];b[i]->left=b[i]->right=NULL;
    }
    for(i=1;i<n;i++)
    {
        int k1=-1,k2;
        for(j=0;j<n;j++)
        {
            if(b[j]!=NULL&&k1==-1) {k1=j;continue;}
            if(b[j]!=NULL) {k2=j;break;}
        }
        for(j=k2;j<n;j++)
        {
            if(b[j]!=NULL)
            {
                if(b[j]->data<b[k1]->data) {k2=k1;k1=j;}
                else if(b[j]->data<b[k2]->data) k2=j;
            }
        }
        q=new BTreeNode;
        q->data=b[k1]->data+b[k2]->data;
        q->left=b[k1];q->right=b[k2];
        b[k1]=q;b[k2]=NULL;
    }
    delete []b;
    return q;
}
void HuffManCoding(BTreeNode*FBT,int len)
{
    static char a[10];
    if(FBT!=NULL)
    {
        if(FBT->left==NULL&&FBT->right==NULL)
        {
            for(int i=0;i<len;i++)
            {
                minma[count].a2[i]=a[i];
            }
            minma[count].lenth=len;
            count++;
        }
        else{
            a[len]=0;HuffManCoding(FBT->left,len+1);
            a[len]=1;HuffManCoding(FBT->right,len+1);
        }
    }
}
int main()
{
    BTreeNode *m;
    char a1[26];
    for(int i=0;i<26;i++)
    {
        a1[i]=65+i;
    }
    int a3[26];
    ifstream f1("新建文本文档.txt",ios::out);
    int j;
    i=0;
    while(!f1.eof())
        if(f1>>j) a3[i++]=j;
    m=CreateHuffman(a3,26);
    HuffManCoding(m,0);
    int total=0;
    cout<<"请输入一串字符"<<endl;
    char s1[100];
    cin>>s1;
    cout<<"它们所对应的密文分别是"<<endl;
    char *q=s1;
    while(q[total]!='\0')
        total++;
    for(i=0;i<total;i++)
    {
        for(j=0;j<26;j++)
        {
            if(s1[i]==a1[j]) 
            {
                for(int k=0;k<minma[j].lenth;k++)
                    cout<<(int)minma[j].a2[k]<<" ";
                cout<<endl;
            }
        }

    }
    
    return 0;
}
"新建文本文档.txt"
23 45 12 87 103 54 99 46 97 82 98 65 38 84 56 55 78 77 47 73 91 86 4 5 40 3

8、邻接矩阵与邻接表
#include<iostream.h>
#include<stdlib.h>
#include<strstrea.h>
typedef int VertexType;
typedef int WeightType;
const int MaxEdgeNum=10;
const int MaxVertexNum=10;
const WeightType MaxValue=1000;
typedef VertexType vexlist[MaxVertexNum];
typedef int adjmatrix[MaxVertexNum][MaxVertexNum];
#include"4_01栈的顺序存储.cpp"
void Prim(adjmatrix GA,edgest CT,int n)
{
    int i,j,k,min,t,m,w;
    for(i=0;i<n-1;i++)
    {
        CT[i].fromvex=0;
        CT[i].endvex=i+1;
        CT[i].weight=GA[0][i+1];
    }
    for(k=1;k<n;k++)
    {
        min=MaxValue;
        m=k-1;
        for(j=k-1;j<n-1;j++)
            if(CT[j].weight<min)
            {
                min=CT[j].weight;
                m=j;
            }
            edge temp=CT[k-1];
            CT[k-1]=CT[m];
            CT[m]=temp;
            j=CT[k-1].endvex;
            for(i=k;i<n-1;i++)
            {
                t=CT[i].endvex;
                w=GA[j][t];
                if(w<CT[i].weight)
                {
                    CT[i].weight=w;
                    CT[i].fromvex=j;
                }
            }
    }
}
void Kruskal(edgeset GE,edgeset CT,int n)
{
    int i,j;
    bool**s=new bool*[n];
    for(i=0;i<n;i++)
        s[i]=new bool[n];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            if(i==j) s[i][j]=true;
            else s[i][j]=false;
    }
    int k=1;
    int d=0;
    int m1,m2;
    while(k<n)
    {
        for(i=0;i<n;i++)
        {
            if(s[i][GE[d].fromvex]==true) m1=i;
            if(s[i][GE[d].endvex]==true) m3=i;
        }
        if(m1!=m2)
        {
            CT[k-1]=GE[d];
            k++;
            for(j=0;j<n;j++)
            {
                s[m1][j]=s[m1][j]||s[m2][j];
                s[m2][j]=false;
            }
        }
        d++;
    }
    for(i=0;i<n;i++)
        delete[]s[i];
    delete[]s;
}
//边集数组
struct edge
{
    int fromvex;
    int endvex;
    WeightType  weight;
};
typedef edge edgeset[MaxEdgeNum];
void InitArray(edgeset GE)
{
    for(int i=0;i<MaxEdgeNum;i++)
    {
        GE[i].fromvex=GE[i].endvex=-1;
        GE[i].weight=MaxValue;
    }
}
void CreateArray(edgeset GE,int n,char*s,int k)
{
    istrstream sin(s);
    char c1,c2,c3;
    int i,j,c=0;
    WeightType w;
    sin>>c1;
    if(k==0){
        do{
            sin>>c1>>i>>c2>>j>>c3;
            GE[c].fromvex=i;
            GE[c].endvex=j;
            GE[c].weight=1;
            c++;
            sin>>c1;
        }while(c1==',');
    }
    else{
        do{
            sin>>c1>>i>>c2>>j>>c3>>w;
            GE[c].fromvex=i;
            GE[c].endvex=j;
            GE[c].weight=w;
            c++;
            sin>>c1;
        }while(c1==',');
    }
}
void Arry(edgeset GE,int i,int n,bool*visited)
{
    int j=i,k=0;
    while(k!=n)
    {
        for(int t=0;GE[t].fromvex!=-1;t++)
        {
            if(GE[t].fromvex==j&&!visited[j])
            {
                cout<<j<<" ";
                visited[j]=true;
                j=GE[t].endvex;
                k++;
                break;
            }
        }
        if(GE[t].fromvex==-1)
        {
            cout<<j<<" ";
            k++;
        }
    }
}
void Arry2(edgeset GE,int i,int n,bool*visited)
{
    int j=i,k=1;
    int q[10];
    int front=0,rear=0;
    cout<<j<<" ";
    while(k!=n)
    {
        for(int t=0;GE[t].fromvex!=-1;t++)
        {
            if(GE[t].fromvex=j&&!visited[j])
            {
                cout<<GE[t].endvex<<" ";
                k++;
                q[rear]=GE[t].endvex;
                rear=(rear+1)%10;
            }
            else 
            {
                visited[j]=true;
                j=q[front];
                front=(front+1)%10;
                break;
            }
        }
    }
}

struct edgenode
{
    int adjvex;
    WeightType weight;
    edgenode*next;
};
typedef edgenode*adjlist[MaxVertexNum];
void InitAdjoin(adjlist GL)
{
    for(int i=0;i<MaxVertexNum;i++)
        GL[i]=NULL;
}
void CreateAdjoin(adjlist GL,int n,char*s,int k1,int k2)
{
    istrstream sin(s);
    char c1,c2,c3;
    int i,j;
    WeightType w;
    edgenode*p;
    sin>>c1;
    if(k2==0){
        do{
            sin>>c1>>i>>c2>>j>>c3;
            p=new edgenode;
        
            p->adjvex=j;p->weight=1;
            p->next=GL[i];
            GL[i]=p;
            if(k1==0){
                p=new edgenode;
                p->adjvex=i;p->weight=1;
                p->next=GL[j];
                GL[j]=p;
            }
            sin>>c1;
        }while(c1==',');
    }
    else{
        do{
            sin>>c1>>i>>c2>>j>>c3>>w;
            p=new edgenode;
            p->adjvex=j;p->weight=w;
            p->next=GL[i];
            GL[i]=p;
            if(k1==0)
            {
                p=new edgenode;
                p->adjvex=i;p->weight=w;
                p->next=GL[j];
                GL[j]=p;
            }
            sin>>c1;
        }while(c1==',');
    }
}

void InitMatrix(adjmatrix GA,int k)
{
    int i,j;
    for(i=0;i<MaxVertexNum;i++)
        for(j=0;j<MaxVertexNum;j++)
            if(i==j) GA[i][j]=0;
            else if(k) GA[i][j]=MaxValue;
            else GA[i][j]=0;
}
void CreateMatrix(adjmatrix GA,int n,char*s,int k1,int k2)
{
    istrstream sin(s);
    char c1,c2,c3;
    int i,j;
    WeightType w;
    sin>>c1;
    if(k1==0&&k2==0)
        do{
            sin>>c1>>i>>c2>>j>>c3;
            GA[i][j]=GA[j][i]=1;
            sin>>c1;
            if(c1==')')break;
        }while(1);
    else if(k1==0&&k2!=0)
        do{
            sin>>c1>>i>>c2>>j>>c3>>w;
            GA[i][j]=GA[j][i]=w;
            sin>>c1;
            if(c1==')')break;
        }while(1);
        else if(k1!=0 && k2==0)
            do{
                sin>>c1>>i>>c2>>j>>c3;
                GA[i][j]=1;
                sin>>c1;
                if(c1==')')break;
            }while(1);
            else if(k1!=0 && k2!=0)
                do{
                    sin>>c1>>i>>c2>>j>>c3>>w;
                    GA[i][j]=w;
                    sin>>c1;
                    if(c1==')')break;
                }while(1);
}
void dfsMatrix(adjmatrix GA,int i,int n,bool*visited)
{
    Stack M;
    InitStack(M);
    int j=i;
    visited[i]=true;
    cout<<j<<" ";
    Push(M,j);
    while(j!=n||M.top!=0)
    {
        if(j==n+1)
        {
            j=M.stack[M.top];
            M.top--;
        }
        if(GA[M.stack[M.top]][j]!=0&&!visited[j])
        {
            visited[j]=true;
            Push(M,j);
            cout<<j<<" ";
            j=0;
        }
        else
            j++;
    }
}

/*void dfsAdjoin(adjlist GL,int i,int n,bool*visited)
{
    cout<<i<<" ";
    visited[i]=true;
    edgenode*p=GL[i];
    while(p!=NULL)
    {
        int j=p->adjvex;
        if(!visited[j])
            dfsAdjoin(GL,j,n,visited);
        p=p->next;
    }
}*/
void dfsAdjoin(adjlist GL,int i,int n,bool*visited)
{
    Stack M;
    InitStack(M);
    cout<<i<<" ";
    Push(M,i);
    visited[i]=true;
    edgenode*p=GL[i];
    while(p!=NULL||M.top!=0)
    {
        if(p==NULL)
        {
            M.top--;
            p=GL[M.stack[M.top]];
        }
        int j=p->adjvex;
        
        
        if(!visited[j])
        {
            visited[j]=true;
            cout<<j<<" ";
            Push(M,j);
            p=GL[M.stack[M.top]];
        }
        else
        {
            p=p->next;
        }
    }
}
            




int main()
{
    int i,n,k1,k2;
    cout<<"输入待处理图的顶点数:";
    cin>>n;
    cout<<"输入图的有向和有无权选择(0为无,非0为有):";
    cin>>k1>>k2;
    bool*visited=new bool[n];
    adjmatrix ga;
    InitMatrix(ga,k2);
    cout<<"输入图的边集;";
    char*a=new char[100];
    cin>>a;
    CreateMatrix(ga,n,a,k1,k2);
    cout<<"按图的邻接矩阵得到的深度优先遍历序列:"<<endl;
    for(i=0;i<n;i++) 
        visited[i]=false;
    dfsMatrix(ga,0,n,visited);
    cout<<endl;
    cout<<"按图的邻接表得到的深度优先遍历序列:"<<endl;
    adjlist ka;
    InitAdjoin(ka);
    CreateAdjoin(ka,n,a,k1,k2);
    for(i=0;i<n;i++) 
        visited[i]=false;
    dfsAdjoin(ka,0,n,visited);
    cout<<endl;
    for(i=0;i<n;i++) 
        visited[i]=false;
    edgeset ke;
    InitArray(ke);
    CreateArray(ke,n,a,0);
    Arry(ke,0,n,visited);
    cout<<endl;
    for(i=0;i<n;i++) 
        visited[i]=false;
    Arry2(ke,0,n,visited);


    return 0;
}

9、迷宫程序

#include<iostream>
#include <windows.h>
#include<ctime>
#include<cstdlib>
#include<string>
using namespace std;
const int m=8,n=8;
char maze[m+2][n+2];
char mark[m+2][n+2];
int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
struct position
{
    int a;
    int b;
};
typedef position ElemType;
struct Stack
{
    ElemType *stack;
    int top;
    int MaxSize;
};
#include"4_01栈的顺序存储.cpp"
void output(ElemType &t)
{
    cout<<"("<<t.a<<" "<<t.b<<")"<<endl;
}
bool SeekPath(int x,int y)
{
    Stack M;
    InitStack(M);
    int i,j,k=0,t;
    ElemType item1,item2;
    int g=x,h=y;
    item1.a=g;
    item1.b=h;
    Push(M,item1);
    mark[g][h]=1;
    while((g!=m)||(h!=n))
    {
        item1.a=g;
        item1.b=h;
        k=0;
        for(i=0;i<4;i++)
        {
            g=g+move[i][0];h=h+move[i][1];
            if((maze[g][h]==0)&&(mark[g][h]==0))
            {
                maze[g][h]=2;
                for(i=0;i<m+2;i++)
                {
                    for(j=0;j<n+2;j++)
                    {
                        cout<<maze[i][j]<<" ";
                    }
                    cout<<endl;
                }
                Sleep(1000);
                system("cls");
                Sleep(50);
                mark[g][h]=1;
                item2.a=g;
                item2.b=h;
                Push(M,item2);break;
            }
            else
            {
                k++;
                g=item1.a;
                h=item1.b;
            }
        }
        
        if((g==1)&&(h==1)&&(k==4))
        {
            cout<<"失败"<<endl;
            t=0;
            break;
        }
        if(k==4)
        {
            g=M.stack[M.top-1].a;
            h=M.stack[M.top-1].b;
            M.top--;
        }
    }
    if(t!=0)
    {    
        while(M.top!=-1)
        {
            output(M.stack[M.top]);
            M.top--;
        }
        return true;
    }
    else
        return false;
}        
int main()
{
    int i,j;
    for(i=0;i<m+2;i++)
        maze[0][i]=1;
    for(j=0;j<n+2;j++)
        maze[j][i-1]=1;
    for(i=0;i<m+2;i++)
        maze[j-1][i]=1;
    for(j=1;j<m+1;j++)
        maze[j][0]=1;
    srand(unsigned(time(0)));
    for(i=1;i<m+1;i++)
    {
        for(j=1;j<n+1;j++)
        {
            double some=(rand()%10)/10.0;
            if(some>0.25)
                maze[i][j]=0;
            else
                maze[i][j]=1;
        }
    }
    maze[m][n]=0;
    for(i=0;i<m+2;i++)
    {
        for(j=0;j<n+2;j++)
            mark[i][j]=0;
    }
    mark[1][1]=1;
    for(i=0;i<m+2;i++)
    {
        for(j=0;j<n+2;j++)
        {
            cout<<maze[i][j]<<" ";
        }
        cout<<endl;
    }
    SeekPath(1,1);
    return 0;
}

10、图实现教学楼的寻找

#include<iostream>
#include<strstrea.h>
#include<fstream>
#include<string>
using namespace std;
typedef int VertexType;
typedef int WeightType; 
const int MaxVertexNum=8;
const int MaxEdgeNum=24;
const WeightType MaxValue=100;
typedef VertexType vexlist[MaxVertexNum];
typedef int adjmatrix[MaxVertexNum][MaxVertexNum];

void InitMatrix(adjmatrix GA,int k)
{
    int i,j;
    for(i=0;i<MaxVertexNum;i++)
        for(j=0;j<MaxVertexNum;j++)
            if(i==j) GA[i][j]=0;
            else if(k) GA[i][j]=MaxValue;
            else GA[i][j]=0;
}
void CreateMatrix(adjmatrix GA,int n,char*s,int k1,int k2)
{
    istrstream sin(s);
    char c1,c2,c3;
    int i,j;
    WeightType w;
    sin>>c1;
    if(k1==0&&k2==0)
        do{
            sin>>c1>>i>>c2>>j>>c3;
            GA[i][j]=GA[j][i]=1;
            sin>>c1;
            if(c1==')') break;
        }while(1);
    else if(k1==0&&k2!=0)
        do{
            sin>>c1>>i>>c2>>j>>c3>>w;
                GA[i][j]=GA[j][i]=w;
                sin>>c1;
                if(c1==')') break;
        }while(1);
    else if(k1!=0&&k2==0)
        do{
            sin>>c1>>i>>c2>>j>>c3;
            GA[i][j]=1;
            sin>>c1;
            if(c1==')') break;
        }while(1);
    else if(k1!=0 &&k2!=0)
        do{
            sin>>c1>>i>>c2>>j>>c3>>w;
            GA[i][j]=w;
            sin>>c1;
            if(c1==')') break;
        }while(1);
}
void dfsMatrix(adjmatrix GA,int i,int n,bool*visited,string a[])
{
    std::cout<<a[i]<<" ";
    visited[i]=true;
    for(int j=0;j<n;j++)
        if(GA[i][j]!=0 && GA[i][j]!=MaxValue && !visited[j])
            dfsMatrix(GA,j,n,visited,a);
}
void Prim(adjmatrix GA,edgeset CT,int n)
{
    int i,j,k,min,t,m,w;
    for(i=0;i<n-1;i++)
    {
        CT[i].fromvex=0;
        CT[i].endvex=i+1;
        CT[i].weight=GA[0][i+1];
    }
    for(k=1;k<n;k++}
    {
        min=MaxValue;
        m=k-1;
        for(j=k-1;j<m-1;j++)
        {
            if(CT[j].weight<min)
            {
                min=CT[j].weight;
                m=j;
            }
        edge temp=CT[k-1];
        CT[k-1]=CT[m];
        CT[m]=temp;
        j=CT[k-1].endvex;
        for(i=k;i<n-1;i++)
        {
            t=CT[i].endvex;
            w=GA[j][t];
            if(w<CT[i].weight)
            {
                CT[i].weight=w;
                CT[i].fromvex=j;
            }
        }
    }
}




int main()
{
    
    string a[8];
    int i=0;
    adjmatrix A;
    ifstream f1("新建文本文档 (2).txt",ios::out);
    if(!f1)
    {
        std::cout<<"新建文本文档 (2).txt do not found";
        exit(1);
    }
    string s;
    while(!f1.eof())
        if(f1>>s) a[i++]=s;
    InitMatrix(A,1);
    char *s1;
    s1=new char [100];
    ifstream f2("新建文本文档 (3).txt",ios::out);
    if(!f2)
    {
        std::cout<<"新建文本文档 (3).txt do not found";
        exit(1);
    }
    char m;
    i=0;
    while(!f2.eof())
        if(f2>>m) s1[i++]=m;
    /*for(i=0;i<62;i++)
        std::cout<<s1[i];*/
    CreateMatrix(A,8,s1,0,1);
    bool *visited=new bool [8];
    for(i=0;i<8;i++)
        visited[i]=false;
    dfsMatrix(A,0,8,visited,a);
    


    return 0;
}

新建文本文档(2)

主楼 博雅楼 知行楼 三舍 图书馆 致远楼 新华楼 中和楼 

新建文本文档(3)

( ( 0 , 6 ) 2 ,( 0 , 2 ) 4 ,( 0 , 5 ) 1 ,( 0 , 3 ) 1 ,( 1 , 5 ) 2 ,( 1 , 7 ) 1 ,( 2 , 4 ) 3 ,( 3 , 6 ) 2 ,( 6 , 7 ) 3 )

 

11、拓补排序

void Toposort(adjlilst GL,int n)
{
    int i,j,k,top,m=0;
    edgenode*p;
    int *d=new int[n];
    for(i=0;i<n;i++)
        d[i]=0;
    for(i=0;i<n;i++)
    {
        p=GL[i];
        while(p!=NULL)
        {
            j=p->adjvex;d[j]++;p=p->next;
        }
    }
    top=-1;
    for(i=0;i<n;i++)
        if(d[i]==0) {d[i]=top;top=i;}
    while(top!=-1)
    {
        j=top;
        top=d[top];
        cout<<j<<" ";
        m++;
        p=GL[j];
        while(p!=NULL)
        {
            k=p->adjvex;
            d[k]--;
            if(d[k]==0)
            {
                d[k]=top;top=k;
            }
            p=p->next;
        }
    }
    cout<<endl;
    if(m<n) cout<<"The network has a cycle!"<<endl;
    delete []d;
}

12、稀疏矩阵的转置乘法与加法

#include<iostream>
using namespace std;
typedef int ElemType;
struct Triple
{
    int row,col;
    ElemType val;
};
struct SMatrix
{
    int m,n,t;
    Triple sm[100];
};
void InitMatrix(SMatrix & M)
{
    M.m=0;
    M.n=0;
    M.t=0;
}
void InputMatrix(SMatrix &M,int m,int n)
{
    M.m=m;
    M.n=n;
    int row,col,val;
    int k=0;
    cin>>row>>col>>val;
    while(row!=0)
    {
        k++;
        M.sm[k].row=row;
        M.sm[k].col=col;
        M.sm[k].val=val;
        cin>>row>>col>>val;
    }
    M.t=k;
}
SMatrix FastTranspose(SMatrix &M)
{
    SMatrix S;
    InitMatrix(S);
    S.m=M.n;  S.n=M.m;  S.t=M.t;
    if(S.t==0) return S;
    int* num=new int[M.n+1];
    int* pot=new int[M.n+1];
    int col,i;
    for(col=1;col<=M.n;col++)
        num[col]=0;
    for(i=1;i<=M.t;i++)
    {
        int j=M.sm[i].col;
        num[j]++;
    }
    pot[1]=1;
    for(col=2;col<=M.n;col++)
        pot[col]=pot[col-1]+num[col-1];
    for(i=1;i<=M.t;i++)
    {
        int j=M.sm[i].col;
        int k=pot[j];
        S.sm[k].row=j;
        S.sm[k].col=M.sm[i].row;
        S.sm[k].val=M.sm[i].val;
        pot[j]++;
    }
    delete[] num;
    delete[] pot;
    return S;
}
void output(SMatrix &M)
{
    int k=1;
    for(int i=1;i<=M.m;i++)
    {
        for(int j=1;j<=M.n;j++)
        {
            if((M.sm[k].row==i)&&(M.sm[k].col==j))
            {
                cout<<M.sm[k].val<<"    ";
                    k++;
            }
            else
                cout<<"0"<<"    ";
        }
        cout<<endl;
    }
}
SMatrix Add(SMatrix& M1,SMatrix& M2)
{
    SMatrix M;
    InitMatrix(M);
    if((M1.m!=M2.m)||(M1.n!=M2.n))
    {
        cout<<"两矩阵规格不一样"<<endl;
        exit(1);
    }
    M.m=M1.m;M.n=M1.n;
    if((M1.t==0)&&(M2.t==0))
        return M;
    int k,t=1;
    for(k=1;k<=M1.t;k++)
    {
        if((M1.sm[k].row==M2.sm[k].row)&&(M1.sm[k].col=M2.sm[k].col))
        {
            M.sm[t].row=M1.sm[k].row;
            M.sm[t].col=M1.sm[k].col;
            M.sm[t].val=M1.sm[k].val+M2.sm[k].val;
            t++;
        }
        else 
        {
            if(M1.sm[k].val==0)
            {
                M.sm[t].row=M1.sm[k].row;
                M.sm[t].col=M1.sm[k].col;
                M.sm[t].val=M2.sm[k].val;
            }
            else
            {
                if(M2.sm[k].val==0)
                {
                    M.sm[t].row=M2.sm[k].row;
                    M.sm[t].col=M2.sm[k].col;
                    M.sm[t].val=M1.sm[k].val;
                }
            }
            t++;
        }
        M.t=t;
    }
    return M;
}
SMatrix chengfa(SMatrix &M1,SMatrix &M2)
{
    SMatrix M3;
    InitMatrix(M3);
    M3.n=M1.m;
    M3.m=M2.n;
    if(M1.n!=M2.m)
        cout<<"两矩阵不能相乘"<<endl;
    else
    {
        int h=0;
        for(int i=1;i<=M1.m;i++)
        {
            for(int j=1;j<=M2.n;j++)
            {
                int sum=0;
                for(int t=1;t<=M1.t;t++)
                {
                    for(int k=1;k<=M2.t;k++)
                    {
                        if((M1.sm[t].row==i)&&(M2.sm[k].col==j))
                            if(M2.sm[k].row==M1.sm[t].col)
                                sum+=M1.sm[t].val*M2.sm[k].val;
                            else
                                sum+=0;
                        else
                            sum+=0;
                    }
                }
                h++;
                M3.sm[h].val=sum;
                M3.sm[h].row=i;
                M3.sm[h].col=j;
            }    
        }
        M3.t=h;        
    }
    return M3;
}
int main()
{
    cout<<"请选择操作序号"<<endl;
    cout<<"1 转质 2 加法 3 乘法"<<endl;
    char a;
    cin>>a;
    switch(a)
    {
    case '1':
        {
            SMatrix p;
            InitMatrix(p);
            int m,n;
            cout<<"请输入稀疏矩阵的行数和列数"<<endl;
            cin>>m>>n;
            cout<<"请输入非零元素所在行数 列数 元素值 输入完后以 0 0 0结束"<<endl;
            InputMatrix(p,m,n);
            output(FastTranspose(p));
        };break;
    case '2':
        {
            SMatrix a,b;
            InitMatrix(a);
            cout<<"请输入稀疏矩阵1的行数和列数"<<endl;
            int m1,n1;
            cin>>m1>>n1;
            cout<<"请输入稀疏矩阵1非零元素所在的行 列 值  输入完后以 0 0 0结束"<<endl;
            InputMatrix(a,m1,n1);
            InitMatrix(b);
            cout<<"请输入稀疏矩阵2的行数和阶数"<<endl;
            int m2,n2;
            cin>>m2>>n2;
            cout<<"请输入稀疏矩阵2中非零元素的行 列 值  输入完后以 0 0 0结束 "<<endl;
            InputMatrix(b,m2,n2);
            output(Add(a,b));
        };break;
    case '3':
        {
            SMatrix a,b;
            InitMatrix(a);
            cout<<"请输入稀疏矩阵1的行数和列数"<<endl;
            int m1,n1;
            cin>>m1>>n1;
            cout<<"请输入稀疏矩阵1非零元素所在的行 列 值  输入完后以 0 0 0结束"<<endl;
            InputMatrix(a,m1,n1);
            InitMatrix(b);
            cout<<"请输入稀疏矩阵2的行数和阶数"<<endl;
            int m2,n2;
            cin>>m2>>n2;
            cout<<"请输入稀疏矩阵2中非零元素的行 列 值   输入完后以 0 0 0结束"<<endl;
            InputMatrix(b,m2,n2);
            output(chengfa(a,b));
        };break;
    }
    return 0;
}

最后说一句,以上都是扯淡,都是扯淡,能挣着钱才是王道呀!!!!!!

孩子我还是太幼稚

 

posted @ 2016-08-30 22:26  香港胖仔  阅读(401)  评论(0编辑  收藏  举报