Sunday, February 15, 2009

Formatting Source Code For Blog Posts

Sometimes it is required to format your source code for blog posting.
Check links below:
I've selected for myself CodeHTMLer with next options:
  • Type of Style: Style Classes
  • Whitespace Options:
Then I added styles to my blog template. See results below:

Java class Node
package source; 

import java.util.LinkedList;
import java.util.List;

public class Node {
public List<Node> nodes = new LinkedList<Node>();
public int data;
public boolean isMarked;

public Node(int data) {
this.data = data;
isMarked = false;
}

public void add(Node node) {
nodes.add(node);
}
}

Java class Graph
package source; 

import java.util.LinkedList;
import java.util.Queue;

public class Graph {
public Node root;

public Graph(Node root) {
this.root = root;
}

public void dfs() {
dfs(root);
System.out.println();
}

private void dfs(Node node) {
if (node == null)
return;

System.out.print(node.data+" ");

for(Node n: node.nodes) {
dfs(n);
}
}

public void bfs() {
Queue<Node> queue = new LinkedList<Node>();

queue.add(root);

bfs(queue);
System.out.println();
}

private void bfs(Queue<Node> queue) {
while (!queue.isEmpty()) {
Node node = queue.remove();
System.out.print(node.data+" ");

for(Node n: node.nodes) {
queue.add(n);
}
}
}
}

Other useful formatter is c# code format

No comments: