Data structure experiment

Keywords: C++ data structure

1. Experimental topic

     Graph is a widely used data structure, which is also the focus of this course. It is characterized by nonlinearity. The cross linked list storage structure of sparse matrix is also a storage structure of graph, so they are also included in this practice. This chapter continues to highlight the programming view of data structure and operation, but according to the nonlinear characteristics of these two structures, the operation is further focused on traversal operation, because traversal operation is the basis of many other operations. Traversing logical (or symbolic) structures, access actions can be any operation. This internship also hopes to be familiar with the characteristics of various storage structures and how to use tree and graph structures to solve specific problems (i.e. the combination of principle and application).

2. Demand analysis

Adjacency matrix and adjacency table are used to realize the following operations: graph creation, traversal, insertion, deletion and shortest path.

Vertex information, edge information storage, data structure type

Graph operation lookup, adjacency table to directed graph

Path output. Determine whether it is a path.

Depth traversal of graph.

3. Outline design

   Let the graph have no more than 30 nodes, and each node is represented by a number (if a graph has n nodes, their numbers are 1, 2,..., n respectively). By inputting all edges of a graph and inputting a graph, each edge is a number pair, some restrictions can be made on the input order of edges. Note that the edges of the spanning tree are directed edges, and the order of endpoints cannot be reversed.  

     (1) With the help of stack type (self-defined and implemented), the depth first traversal is realized by non recursive algorithm.

     (2) The depth first spanning tree and breadth first spanning tree are established with the adjacency table as the storage structure, and then the spanning tree is printed according to the concave table or tree shape.

4. Detailed design

 #Include < stdio. H > / / Experiment 4
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 11
#define MX 999999
int D[Max][Max];
int path[Max][Max];
typedef struct Ver{//Vertex information
    char num[5];
    char name[51];
    char instruct[101];
}Ver;
typedef struct{//adjacency matrix 
    Ver vex[Max];//Vertex table
    int arcs[Max][Max];
    int vnum,arcnum;
}AMGragh;
void menu(){
    cout<<"************Welcome************"<<endl;
    cout<<"        1,View all attractions           "<<endl;
    cout<<"        2,Scenic spot query               "<<endl;
    cout<<"        3,Ask for directions                   "<<endl;
    cout<<"        4,Modify the basic information of scenic spots       "<<endl;
    cout<<"        5,sign out                   "<<endl;
    cout<<"**********************************"<<endl;
    cout<<"Please select..."<<endl;
}
void Allprint(AMGragh G){//Output all attraction information
    cout<<"---------------Overview of campus attractions---------------"<<endl;
    cout<<"Name of scenic spot   "<<"  "<<"Code"<<"     "<<"    brief introduction"<<endl;
    for(int i=0;i<G.vnum;i++){
        cout<<G.vex[i].name<<"    "<<G.vex[i].num<<"   "<<G.vex[i].instruct<<endl;
    }
    cout<<endl;
}
void CreateUDG(AMGragh &G){//Mapping
    G.vnum=10;
    strcpy(G.vex[0].num,"01");
    strcpy(G.vex[1].num,"02");
    strcpy(G.vex[2].num,"03");
    strcpy(G.vex[3].num,"04");
    strcpy(G.vex[4].num,"05");
    strcpy(G.vex[5].num,"06");
    strcpy(G.vex[6].num,"07");
    strcpy(G.vex[7].num,"08");
    strcpy(G.vex[8].num,"09");
    strcpy(G.vex[9].num,"10");
    strcpy(G.vex[0].name,"Beiyuan Food City");
    strcpy(G.vex[1].name,"North playground    ");
    strcpy(G.vex[2].name,"Gymnasium    ");
    strcpy(G.vex[3].name,"library    ");
    strcpy(G.vex[4].name,"Square 1     ");
    strcpy(G.vex[5].name,"Square 2     ");
    strcpy(G.vex[6].name,"Wetland Park  ");
    strcpy(G.vex[7].name,"Park 2     ");
    strcpy(G.vex[8].name,"Lake 1      ");
    strcpy(G.vex[9].name,"Park 3     ");
    strcpy(G.vex[0].instruct,"There are all kinds of delicious food in Beiyuan Food City, which can make you feast");
    strcpy(G.vex[1].instruct,"North playground is not only a sports ground in Beiyuan, but also a basketball court");
    strcpy(G.vex[2].instruct,"The gymnasium has complete facilities and beautiful buildings. You can enjoy sports here!");
    strcpy(G.vex[3].instruct,"The library has a comfortable environment and a rich collection of books, which makes people feel the beauty of reading");
    strcpy(G.vex[4].instruct,"Square 1 is adjacent to the west gate of the school and is also the place where the national flag is raised");
    strcpy(G.vex[5].instruct,"Square 2 will hold some art parties and campus job fairs, which is also a paradise for roller skaters");
    strcpy(G.vex[6].instruct,"The wetland park has small stone bridges and beautiful trees, which makes people relaxed and happy");
    strcpy(G.vex[7].instruct,"The sign of Park 2 is a pair of white dolphins, which is located in the middle of the water. There are beautiful lotus flowers in the water in summer");
    strcpy(G.vex[8].instruct,"The water in Lake 1 is clear to the bottom, and you can see lively small fish");
    strcpy(G.vex[9].instruct,"The trees planted by former alumni in Park 3 are vibrant and represent their love for their alma mater");
    G.arcs[1][2]=G.arcs[2][1]=2;
    G.arcs[1][9]=G.arcs[9][1]=19;
    G.arcs[2][3]=G.arcs[3][2]=3;
    G.arcs[2][4]=G.arcs[4][2]=5;
    G.arcs[3][4]=G.arcs[4][3]=2;
    G.arcs[4][5]=G.arcs[5][4]=3;
    G.arcs[4][7]=G.arcs[7][4]=29;
    G.arcs[4][10]=G.arcs[10][4]=33;
    G.arcs[5][6]=G.arcs[6][5]=6;
    G.arcs[6][7]=G.arcs[7][6]=7;
    G.arcs[7][8]=G.arcs[8][7]=8;
    G.arcs[8][9]=G.arcs[9][8]=1;
    G.arcs[9][10]=G.arcs[10][9]=2;
    for(int i=1;i<=10;i++)//Initialization path length
        for(int j=1;j<=10;j++){
            if(G.arcs[i][j]==0&&i!=j)
                G.arcs[i][j]=MX;
        }
    G.arcnum=13;
}
void Change(AMGragh &G){//Modify information
    Allprint(G);
    cout<<"Please enter the code to modify the information:";
    char c[5];
    cin>>c;
    for(int i=0;i<G.vnum;i++){
        if(strcmp(c,G.vex[i].num)==0)//String comparison
        {
            memset(G.vex[i].name,0,sizeof(G.vex[i].name));
            memset(G.vex[i].num,0,sizeof(G.vex[i].num));
            memset(G.vex[i].instruct,0,sizeof(G.vex[i].instruct));
            char num1[5];
            char name1[51];
            char instruct1[101];
            cout<<"Please enter the modified attraction information:"<<endl;
            cout<<"Name of scenic spot:";
            scanf("%s",name1);
            cout<<"Code:";
            scanf("%s",num1);
            cout<<"Introduction:";
            scanf("%s",instruct1);
            strcpy(G.vex[i].name,name1);
            strcpy(G.vex[i].num,num1);
            strcpy(G.vex[i].instruct,instruct1);
            cout<<"Modification succeeded!"<<endl;
            break;
        }
    }
}
void Query(AMGragh G){//Query scenic spots
    cout<<"Please enter the code of the scenic spot to query:";
    char c[5];
    cin>>c;
    int i;
    for(i=0;i<G.vnum;i++)
        if(strcmp(c,G.vex[i].num)==0)
        {
            cout<<"Name of scenic spot:"<<G.vex[i].name<<"   ";
            cout<<"Code:"<<G.vex[i].num<<"   ";
            cout<<"Introduction:"<<G.vex[i].instruct<<endl;
            break;
        }
    if(i==G.vnum)
        cout<<"The code does not exist!"<<endl;
}
void Floyd(AMGragh G){//Freudian algorithm to obtain the shortest path
    int i,j,k;
    for(i=1;i<=G.vnum;++i)
        for(j=1;j<=G.vnum;j++){
            D[i][j]=G.arcs[i][j];
            if(D[i][j]<MX&&i!=j)
                path[i][j]=i;
            else
                path[i][j]=-1;
        }
    for(k=1;k<=G.vnum;k++)
        for(i=1;i<=G.vnum;++i)
            for(j=1;j<=G.vnum;j++)
                if(D[i][k]+D[k][j]<D[i][j]){
                    D[i][j]=D[i][k]+D[k][j];
                    path[i][j]=path[k][j];
                }
}
void Path(AMGragh G,int a,int b){//Get specific path
   int p[Max];
   p[0]=b;
   int i=1;
   while(a!=b){
    b=path[a][b];
    p[i]=b;
    ++i;
   }
   cout<<"route:"<<G.vex[a-1].name;
   i=i-2;
   while(i>=0){
    cout<<"--->"<<G.vex[p[i]-1].name;
    --i;
   }
}
void Ask(AMGragh G){//Ask for directions
    Allprint(G);
    cout<<"Please enter the starting point and destination(1~10´╝îWhich scenic spot,Space between):";
    int a,b;
    cin>>a>>b;
    Floyd(G);
    cout<<endl<<endl<<"from"<<G.vex[a-1].name<<"reach"<<G.vex[b-1].name<<":"<<endl<<endl<<"Shortest path length:"<<D[a][b]*10<<"rice"<<endl;
    Path(G,a,b);
    cout<<endl;
}
int main(){
    AMGragh G;
    memset(G.arcs,0,sizeof(G.arcs));
    CreateUDG(G);
    int m;
    while(m!=5){
        menu();
        cin>>m;
        switch(m){
        case 1:
            Allprint(G);
            break;
        case 2:
            Query(G);
            break;
        case 3:
            Ask(G);
            break;
        case 4:
            Change(G);
            break;
        case 5:
            cout<<"Thank you for your use!"<<endl;
            return 0;
        default:
            cout<<"This option is not available!"<<endl;
        }
        system("pause");
        system("cls");
    }
    return 0;
}

5. Commissioning analysis

     The previous problem cannot be retained repeatedly. It is not completely practical for the shortest path problem. All scenic spots are saved in the early stage without human input.

6. Instructions for use

   Select the program to be executed according to the prompt in the menu bar.

Posted by Ruiser on Mon, 08 Nov 2021 15:33:12 -0800