Lab Report Course: Program design and data structure Name: Yang Jindeng class: 1623 Study No.: 20162302 Experiment Name: Find and sort laboratory equipment: Lenovo Deliverer with Ideau 15ISK experimental purposes and requirements:
1. Using adjacency matrix to realize the non-direction graph
2. Using the cross-linked list to achieve the non-direction diagram
3. Implement PP19.9
Experimental content, steps and experience: experimental content:
using adjacency matrix to realize the non-
AdjMatrixGraph
direction graph
Using adjacency matrix to implement the non-direction graph (both edges and vertices to be saved), to implement the method of adding and deleting nodes, add and remove edges, size (), isEmpty (), breadth-first iterator, depth-first iterator
-
Vertex class Vertex
: The graph is made up of vertices and edges, and a matrix is used to represent the edge of another vertex pointed to by a vertex and the length of the edge in the adjacency matrix. The vertex requires a separate class to hold the relevant properties of the vertex. The
contains both the data it contains and whether it has been accessed over two basic properties. You can write a method according to the situation to change its data, access it, and give the format when it is printed. The wasvisited
can be changed directly to public without the encapsulation being considered.
public vertex<t> {private comparable data; boolean wasvisited; public vertex (Comparable data) {this . data = data; wasvisited = false ;} public void setdata (Comparable a) {data = A;} public String tostring () {return data+;}
Add Edge setline ()
and delete Edge Removeline ()
method: in adjmatrixgraph
Inside the side is in a two-dimensional array inside, so add and delete when you can directly manipulate the array. Because it is a non-directed graph, the addition and deletion of the time to handle the operation of two directions.
public void addlines (int A, int B, int weigt) {if (lines[a][b]! = 0 ) syste M.out . println ( "the line has been set!" ); LINES[A][B] = weigt; Lines[b][a] = weigt;} public void removelines (int A, int B) {if (lines[a][b] = = 0 ) System. out . println ( "the line has been remove!" ); LINES[A][B] = 0 ; Lines[b][a] = 0 ;}
Ways to add vertices setVertex()
and delete vertices removeVertex()
Methods for adding vertices setVertex()
and deleting the points remoeVertex0
: the method of adding vertices is relatively simple, just put a new vertex in the array.
The first thing to consider when deleting vertices is whether the deleted vertices are present, and then performing two actions to remove the edges that are connected to it, and to move the data behind it.
The first is the handling of the exception, when deleting the non-existent vertex can throw an exception, you can also use the F-Judge statement to avoid the execution of the exception code
if (a > count) System.out.println("We don‘t have enough data"); else {};
20162302 Experimental Report of "searching and sorting"