using System.Collections.Generic;
public static void Main()
MyLinkedList linkedList = new MyLinkedList();
linkedList.addNode(new List<int>() { 4 });
linkedList.addNode(new List<int>() { 2,1,2 });
linkedList.addNode(new List<int>() { 2,0,3 });
linkedList.addNode(new List<int>() { 2,0,3 });
linkedList.addNode(new List<int>() { 2,1,2 });
List<int> headValue = linkedList.removeHead();
AdjacencyList Al = new AdjacencyList(headValue[0], linkedList);
Console.WriteLine(Al.totalPoints);
if (Al.listGraph.is2DimensionalGraph()) {
Console.WriteLine("Danh sach bieu dien do thi hai chieu");
Console.WriteLine("Danh sach bieu dien do thi mot chieu");
public MyLinkedList list;
public MyLinkedListGraph listGraph;
public AdjacencyList(int totalPoints, MyLinkedList list)
this.totalPoints = totalPoints;
public void generateGraph()
this.listGraph = this.list.generateGraph();
public NodeGraph preNode;
public NodeGraph nextNode;
public NodeGraph(int identity, NodeGraph nextNode, NodeGraph preNode)
this.nextNode = nextNode;
this.identity = identity;
public class MyLinkedListGraph
public List<NodeGraph> list = new List<NodeGraph>();
public MyLinkedListGraph()
public void addNode(int identity, int identity1, int identity2)
int index = list.FindIndex(a => a.identity == identity);
tmp = new NodeGraph(identity, null, null);
int index1 = list.FindIndex(a => a.identity == identity1);
tmp1 = new NodeGraph(identity1, null, null);
if (tmp.nextNode == null) {
} else if (tmp.preNode == null) {
if (tmp1.nextNode != null) {
if (tmp1.nextNode.identity == identity) {
if (tmp1.preNode != null) {
if (tmp1.preNode.identity == identity) {
if (tmp.nextNode == null) {
} else if (tmp.preNode == null) {
int index2 = list.FindIndex(a => a.identity == identity2);
tmp2 = new NodeGraph(identity2, null, null);
if (tmp.nextNode == null) {
} else if (tmp.preNode == null) {
if (tmp2.nextNode != null) {
if (tmp2.nextNode.identity == identity) {
if (tmp2.preNode != null) {
if (tmp2.preNode.identity == identity) {
if (tmp.nextNode == null) {
} else if (tmp.preNode == null) {
public bool is2DimensionalGraph()
bool is2DimensionalGraph = true;
NodeGraph tmp = this.head;
if (tmp.nextNode == null) {
is2DimensionalGraph = false;
if (tmp.nextNode.preNode == null) {
is2DimensionalGraph = false;
if (tmp.nextNode.preNode.identity != tmp.identity) {
is2DimensionalGraph = false;
if (tmp.preNode == null) {
is2DimensionalGraph = false;
if (tmp.preNode.nextNode == null) {
is2DimensionalGraph = false;
if (tmp.preNode.nextNode.identity != tmp.identity) {
is2DimensionalGraph = false;
while(tmp.nextNode.identity != this.head.identity) {
if (tmp.nextNode == null) {
is2DimensionalGraph = false;
if (tmp.nextNode.preNode == null) {
is2DimensionalGraph = false;
if (tmp.nextNode.preNode.identity != tmp.identity) {
is2DimensionalGraph = false;
if (tmp.preNode == null) {
is2DimensionalGraph = false;
if (tmp.preNode.nextNode == null) {
is2DimensionalGraph = false;
if (tmp.preNode.nextNode.identity != tmp.identity) {
is2DimensionalGraph = false;
return is2DimensionalGraph;
public Node(Node preNode, Node nextNode)
this.nextNode = nextNode;
public class MyLinkedList
public void addNode(List<int> n)
Node tmp = new Node(null, null);
this.tail.nextNode = tmp;
this.tail = this.tail.nextNode;
public List<int> removeHead()
this.head = this.head.nextNode;
public void printNodeData() {
string connectedPointsString = "";
foreach(int item in tmp.value){
connectedPointsString = connectedPointsString + item.ToString() + " ";
Console.WriteLine(connectedPointsString);
while(tmp.nextNode != null) {
string connectedPointsStringNext = "";
foreach(int item in tmp.value){
connectedPointsStringNext = connectedPointsStringNext + item.ToString() + " ";
Console.WriteLine(connectedPointsStringNext);
public MyLinkedListGraph generateGraph() {
MyLinkedListGraph listGraph = new MyLinkedListGraph();
int lineNumber = tmp.value[0];
listGraph.addNode(i, -1, -1);
int node1 = tmp.value[1];
listGraph.addNode(i, node1, -1);
int node1 = tmp.value[1];
int node2 = tmp.value[2];
listGraph.addNode(i, node1, node2);
while(tmp.nextNode != null) {
int lineNumberNext = tmp.value[0];
if (lineNumberNext == 0) {
listGraph.addNode(i, -1, -1);
if (lineNumberNext == 1) {
int node1 = tmp.value[1];
listGraph.addNode(i, node1, -1);
if (lineNumberNext == 2) {
int node1 = tmp.value[1];
int node2 = tmp.value[2];
listGraph.addNode(i, node1, node2);