Beispiel #1
0
func (graph *Graph) DepthFirstSearch(startVertex *Vertex) {
	if graph.Vertices == nil || len(graph.Vertices) == 0 {
		panic("Graph has no vertex.")
	}
	fmt.Printf("%s ", startVertex.Label)
	startVertex.isVisited = true
	stack := &stack.LinkedStack{}
	stack.Push(startVertex)
	for stack.Size() > 0 { // Visit the the vertices by edges that hasn't been visited, until the path ends.
		vertex := convertToVertex(stack.Peek())
		hasAddedNewVertex := false
		for _, edge := range vertex.Edges {
			if !edge.ToVertex.isVisited {
				fmt.Printf("%s ", edge.ToVertex.Label)
				edge.ToVertex.isVisited = true
				hasAddedNewVertex = true
				stack.Push(edge.ToVertex)
			}
		}
		if !hasAddedNewVertex {
			stack.Pop()
		}
	}
	graph.clearVerticesVisitHistory()
}
func (hashMap *LinkedHashMap) Clear() {
	hashMap.checkTable()
	for _, e := range hashMap.table {
		if e != nil {
			stack := &stack.LinkedStack{}
			stack.Push(e)
			nextEntry := e.next
			for nextEntry != nil {
				stack.Push(nextEntry)
				nextEntry = nextEntry.next
			}
			for stack.Size() > 0 {
				e := convertToEntry(stack.Peek())
				e.key = 0
				e.value = nil
				e.next = nil
				e = nil
				stack.Pop()
			}
		}
	}
}