# 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;
char name;
char instruct;
}Ver;
Ver vex[Max];//Vertex table
int arcs[Max][Max];
int vnum,arcnum;
}AMGragh;
cout<<"************Welcome************"<<endl;
cout<<"        1,View all attractions           "<<endl;
cout<<"        2,Scenic spot query               "<<endl;
cout<<"        4,Modify the basic information of scenic spots       "<<endl;
cout<<"        5,sign out                   "<<endl;
cout<<"**********************************"<<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.num,"01");
strcpy(G.vex.num,"02");
strcpy(G.vex.num,"03");
strcpy(G.vex.num,"04");
strcpy(G.vex.num,"05");
strcpy(G.vex.num,"06");
strcpy(G.vex.num,"07");
strcpy(G.vex.num,"08");
strcpy(G.vex.num,"09");
strcpy(G.vex.num,"10");
strcpy(G.vex.name,"Beiyuan Food City");
strcpy(G.vex.name,"North playground    ");
strcpy(G.vex.name,"Gymnasium    ");
strcpy(G.vex.name,"library    ");
strcpy(G.vex.name,"Square 1     ");
strcpy(G.vex.name,"Square 2     ");
strcpy(G.vex.name,"Wetland Park  ");
strcpy(G.vex.name,"Park 2     ");
strcpy(G.vex.name,"Lake 1      ");
strcpy(G.vex.name,"Park 3     ");
strcpy(G.vex.instruct,"There are all kinds of delicious food in Beiyuan Food City, which can make you feast");
strcpy(G.vex.instruct,"North playground is not only a sports ground in Beiyuan, but also a basketball court");
strcpy(G.vex.instruct,"The gymnasium has complete facilities and beautiful buildings. You can enjoy sports here!");
strcpy(G.vex.instruct,"The library has a comfortable environment and a rich collection of books, which makes people feel the beauty of reading");
strcpy(G.vex.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.instruct,"Square 2 will hold some art parties and campus job fairs, which is also a paradise for roller skaters");
strcpy(G.vex.instruct,"The wetland park has small stone bridges and beautiful trees, which makes people relaxed and happy");
strcpy(G.vex.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.instruct,"The water in Lake 1 is clear to the bottom, and you can see lively small fish");
strcpy(G.vex.instruct,"The trees planted by former alumni in Park 3 are vibrant and represent their love for their alma mater");
G.arcs=G.arcs=2;
G.arcs=G.arcs=19;
G.arcs=G.arcs=3;
G.arcs=G.arcs=5;
G.arcs=G.arcs=2;
G.arcs=G.arcs=3;
G.arcs=G.arcs=29;
G.arcs=G.arcs=33;
G.arcs=G.arcs=6;
G.arcs=G.arcs=7;
G.arcs=G.arcs=8;
G.arcs=G.arcs=1;
G.arcs=G.arcs=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;
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;
char name1;
char instruct1;
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;
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=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;
}
}
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){
cin>>m;
switch(m){
case 1:
Allprint(G);
break;
case 2:
Query(G);
break;
case 3:
break;
case 4:
Change(G);
break;
case 5:
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