syneffort 2 years ago
parent 677b668c60
commit bae9fe4149
  1. 17
      .vscode/launch.json
  2. 4
      .vscode/tasks.json
  3. 14
      DataStructure/graphStructure/Program.cs
  4. 51
      DataStructure/graphStructure/Structure/Graph.cs
  5. 23
      DataStructure/graphStructure/Structure/GraphClient.cs
  6. 40
      DataStructure/graphStructure/Structure/GraphNode.cs

@ -4,6 +4,23 @@
// https://go.microsoft.com/fwlink/?linkid=830387() . // https://go.microsoft.com/fwlink/?linkid=830387() .
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": "graphStructure (Console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/DataStructure/graphStructure/bin/Debug/net7.0/graphStructure.dll",
"args": [],
"cwd": "${workspaceFolder}/DataStructure/graphStructure",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{ {
// Use IntelliSense to find out which attributes exist for C# debugging // Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes // Use hover for the description of the existing attributes

@ -7,7 +7,7 @@
"type": "process", "type": "process",
"args": [ "args": [
"build", "build",
"${workspaceFolder}/DataStructure/treeStructure/treeStructure.csproj", "${workspaceFolder}/DataStructure/graphStructure/graphStructure.csproj",
"/property:GenerateFullPaths=true", "/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary" "/consoleloggerparameters:NoSummary"
], ],
@ -33,7 +33,7 @@
"watch", "watch",
"run", "run",
"--project", "--project",
"${workspaceFolder}/DataStructure/treeStructure/treeStructure.csproj" "${workspaceFolder}/DataStructure/hashTableStructure/hashTableStructure.csproj"
], ],
"problemMatcher": "$msCompile" "problemMatcher": "$msCompile"
} }

@ -1,2 +1,12 @@
// See https://aka.ms/new-console-template for more information using MainNamespace.Structure;
Console.WriteLine("Hello, World!");
namespace MainNamespace
{
static class Program
{
static void Main(string[] args)
{
GraphClient.DoWork();
}
}
}

@ -0,0 +1,51 @@
namespace MainNamespace.Structure
{
class Graph<T>
{
private List<GraphNode<T>> _nodeList;
public Graph()
{
_nodeList = new List<GraphNode<T>>();
}
public GraphNode<T> AddNode(T data)
{
GraphNode<T> node = new GraphNode<T>(data);
_nodeList.Add(node);
return node;
}
public GraphNode<T> AddNode(GraphNode<T> node)
{
_nodeList.Add(node);
return node;
}
public void AddEdge(GraphNode<T> from, GraphNode<T> to, bool oneway = true, int weight = 0)
{
from.Neighbors.Add(to);
from.Weights.Add(weight);
if (!oneway)
{
to.Neighbors.Add(from);
to.Weights.Add(weight);
}
}
internal void DebugPrintLinks()
{
foreach (var node in _nodeList)
{
for (int i = 0; i < node.Neighbors.Count; i++)
{
var neighbor = node.Neighbors[i];
var weight = node.Weights[i];
System.Console.WriteLine($"[{node.Data}] ---{weight}--- [{neighbor.Data}]");
}
}
}
}
}

@ -0,0 +1,23 @@
namespace MainNamespace.Structure
{
class GraphClient
{
public static void DoWork()
{
Graph<int> g = new Graph<int>();
var n1 = g.AddNode(10);
var n2 = g.AddNode(20);
var n3 = g.AddNode(30);
var n4 = g.AddNode(40);
var n5 = g.AddNode(50);
g.AddEdge(n1, n3, true, 1);
g.AddEdge(n2, n4, true, 2);
g.AddEdge(n3, n4, true, 3);
g.AddEdge(n3, n5, true, 10);
g.DebugPrintLinks();
}
}
}

@ -0,0 +1,40 @@
namespace MainNamespace.Structure
{
class GraphNode<T>
{
private List<GraphNode<T>>? _neighbors;
private List<int>? _weights;
public T? Data { get; set; }
public GraphNode()
{
}
public GraphNode(T data)
{
this.Data = data;
}
public List<GraphNode<T>> Neighbors
{
get
{
_neighbors = _neighbors ?? new List<GraphNode<T>>();
return _neighbors;
}
}
public List<int> Weights
{
get
{
_weights = _weights ?? new List<int>();
return _weights;
}
}
}
}
Loading…
Cancel
Save