Click here to Skip to main content
15,887,746 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Windows events Pin
Stuart Dootson20-Apr-09 21:05
professionalStuart Dootson20-Apr-09 21:05 
QuestionHow to Read data using IfStream in while loop? [modified] Pin
pohcb_sonic20-Apr-09 16:31
pohcb_sonic20-Apr-09 16:31 
QuestionRe: How to Read data using IfStream? Pin
David Crow20-Apr-09 17:17
David Crow20-Apr-09 17:17 
AnswerRe: How to Read data using IfStream? Pin
pohcb_sonic20-Apr-09 18:42
pohcb_sonic20-Apr-09 18:42 
QuestionRe: How to Read data using IfStream? Pin
David Crow21-Apr-09 3:12
David Crow21-Apr-09 3:12 
AnswerRe: How to Read data using IfStream? Pin
pohcb_sonic21-Apr-09 14:31
pohcb_sonic21-Apr-09 14:31 
QuestionRe: How to Read data using IfStream? Pin
David Crow22-Apr-09 2:50
David Crow22-Apr-09 2:50 
QuestionHaving trouble converting Java Code to C++ Pin
NxtGr8One20-Apr-09 16:23
NxtGr8One20-Apr-09 16:23 
I was wondering if anyone can help me convert my Graph Code I have in Java to C++. I have going at it for a week or so now and am having trouble. Any help would be appreciated. I will post all parts of the code that is involved for this code.

Also, I will post the assignment I have and if it's easier to just ignore everything I have and do it all from scratch then I will post what is needed and said in the assignment.

import java.io.*;

public class VNode {

private VNode next;
private int vertex;

public VNode() {
next = null;
vertex = 0;
}


public VNode (int a, VNode n) {
a = 0;
next = n;
}

public VNode getNext() {
return next;
}

public int getVertex() {
return vertex;
}

public void setNext(VNode ND) {
next = ND;
}

public void setItem(int IT) {
vertex = IT;
}

}

public class Graph {

private int [] ppath;
private VNode [] Vertices;
private boolean [] visited;
private boolean setVisited = false;
private VNode a;
private VNode previous;
private VNode b;
private VNode c;
private VNode prev;


//Sets up a graph with n vertices 0,1,...,n-1
public Graph(int n) {
Vertices = new VNode[n];
for (int i = 0; i < Vertices.length; i++) {
Vertices[i] = null;
}
}

//Inserts edge(i,j). Check bounds.
public void insert(int i, int j) {
if(Vertices[j] == null) {
Vertices[j] = new VNode(i, null);
}
else if(i < Vertices[j].getVertex()) {
a = Vertices[j];
Vertices[j] = new VNode(i, a);
}
else {
a = Vertices[j];
previous = null;
while(a != null) {
if(i < a.getVertex()) {
previous.setNext(new VNode(i,a));
break;
}
previous = a;
a = a.getNext();
}
if(a == null) {
previous.setNext(new VNode(i, null));
}
}
if(Vertices[i] == null) {
Vertices[i] = new VNode(j, null);
}
else if (j < Vertices[i].getVertex()) {
b = Vertices[i];
Vertices[i] = new VNode(j, b);
}
else {
c = Vertices[i];
prev = null;
while(c != null) {
if(j < c.getVertex()) {
prev.setNext(new VNode(j, c));
break;
}
prev = c;
c = c.getNext();
}
if (c == null) {
prev.setNext(new VNode(j, null));
}
}
}

//Returns a string representing a graph using set notation.
public String toString() {
String v = "V = {";
String e = "E = {";
int vertCount = 0;
//Prints out the vertices
for (int i = 0; i < Vertices.length; i++) {
v += vertCount;
if (vertCount == Vertices.length-1) {
v += "}";
}
else {
v += ", ";
}
vertCount++;
}
System.out.println(v);
//Prints out the edges
/*int edgeCount = 0;
for (VNode n = Vertices[0]; n != null; n = n.getNext()) {
while (n.getNext() != null) {
if (edgeCount == Vertices.length-1) {
e += "}";
}
else {
e += ", ";
}
}
edgeCount++;
} */
return (v + e);
}

//Returns a string representing the graph as an adjacency list.
public String getAdjList() {
String x = "";
for (int i = 0; i < Vertices.length; i++) {
VNode adj = Vertices[i];
System.out.println(adj);
x = i + ": ";
while (adj != null) {
if (adj.getNext() == null) {
x += adj.getVertex() ;
}
else {
x += ", ";
}
adj = adj.getNext();
}
}
return x;
}

//Performs a recursive depth first search starting at k.
//Print nodes in dfs order. Use least numbered vertex scheme.
public void dfs(int k) {
if(!setVisited) {
visited = new boolean[Vertices.length];
for (int i = 0; i < visited.length; i++) {
visited[i] = false;
}
setVisited = true;
}
depthFirstSearch(k);
}

//From dfs. Actually does the searching and printing.
//The dfs just initialized the visited array.
public void depthFirstSearch(int k) {
System.out.println("K = ");
visited[k] = true;
for (VNode n = Vertices[k]; n != null; n = n.getNext()) {
if(!visited[n.getVertex()]) {
visited[n.getVertex()] = true;
System.out.print(n.getVertex());
depthFirstSearch(n.getVertex());
}
}
}


//Performs a breadth first search using a queue.
//Prints nodes in bfs order. Use least numbered vertex scheme.
public void bfs(int k) {
visited = new boolean[Vertices.length];
Queue q = new Queue();
for (int i = 0; i < Vertices.length; i++) {
visited[k] = true;
}
q.enqueue(k);
while(!q.isEmpty()) {
int w = (Integer)q.dequeue();
System.out.println("The value of w = " + w);
for (VNode n = Vertices[w]; n != null; n = n.getNext()) {
if (!visited[n.getVertex()]) {
visited[n.getVertex()] = true;
q.enqueue(n.getVertex());
}
}
}
}

//Returns the shortest path between r and s.
//If no such path exists, return null.
public int[] shortestPath(int r, int s) {
visited = new boolean[Vertices.length];
Queue q = new Queue();
ppath = new int[Vertices.length];
for (int i = 0; i < Vertices.length; i++) {
visited[r] = true;
}
//Initializes all entries in ppath to -2.
for (int a = 0; a < ppath.length; a++) {
ppath[a] = -2;
}
q.enqueue(r);
ppath[r] = -1;
while(!q.isEmpty()) {
int w = (Integer)q.dequeue();
System.out.println("The value of w = " + w);
for (VNode n = Vertices[w]; n != null; n = n.getNext()) {
if (!visited[n.getVertex()]) {
visited[n.getVertex()] = true;
ppath[n.getVertex()] = w;
q.enqueue(n.getVertex());
}
}
}
int y = 0;
int z = s;
return null;
}
}//end of Graph Class

So that is the java code.

Here is the assignment asked.

//struct Edge { int v1, int v2, int weight; }
//weight of -1 = no weight

void initGraph(int n, bool directed);
//may be a constructor - intialize a graph with n verticies
//the graph will be directed if directed is set to true

void addEdge(Edge edge)
//add an edge to the graph

int getAdjacentList(int v, List& adjList){
//puts in adjlist the vertices adjacent to v
//returns # vertices in adjlist
}

int getEdgeList(List& edgeList){
//puts in edgeList the list of edges in the graph
//for undirected graph, only 1 copy of the edge is needed
//returns # edges in edgeList
}

bool isPath(int start, int end, List& path){
//returns true if there is a path from start to end
//use Depth-First search algorithm (a private method)
//puts in path the list of vertices on the path from
// start to end (if there is a path)
}


Thanks for all and any help.
AnswerRe: Having trouble converting Java Code to C++ Pin
David Crow20-Apr-09 16:51
David Crow20-Apr-09 16:51 
QuestionC++ Directory Functions Pin
gamefreak229120-Apr-09 15:37
gamefreak229120-Apr-09 15:37 
AnswerRe: C++ Directory Functions [modified] Pin
Joe Woodbury20-Apr-09 16:33
professionalJoe Woodbury20-Apr-09 16:33 
GeneralRe: C++ Directory Functions Pin
gamefreak229120-Apr-09 17:01
gamefreak229120-Apr-09 17:01 
QuestionRe: C++ Directory Functions Pin
David Crow20-Apr-09 17:37
David Crow20-Apr-09 17:37 
AnswerRe: C++ Directory Functions Pin
gamefreak229120-Apr-09 18:16
gamefreak229120-Apr-09 18:16 
AnswerRe: C++ Directory Functions Pin
krmed21-Apr-09 3:03
krmed21-Apr-09 3:03 
QuestionWhat I should Use...bitset, vector &LT bool&GT, or bit_vector? Pin
JohnnyG20-Apr-09 13:51
JohnnyG20-Apr-09 13:51 
AnswerRe: What I should Use...bitset, vector &LT bool&GT, or bit_vector? Pin
Stuart Dootson20-Apr-09 20:07
professionalStuart Dootson20-Apr-09 20:07 
GeneralRe: What I should Use...bitset, vector &LT bool&GT, or bit_vector? Pin
JohnnyG21-Apr-09 11:18
JohnnyG21-Apr-09 11:18 
Questionsprintf and rounding issues [modified] Pin
Jim Crafton20-Apr-09 8:27
Jim Crafton20-Apr-09 8:27 
AnswerRe: sprintf and rounding issues Pin
Stuart Dootson20-Apr-09 8:57
professionalStuart Dootson20-Apr-09 8:57 
AnswerRe: sprintf and rounding issues Pin
Luc Pattyn20-Apr-09 9:20
sitebuilderLuc Pattyn20-Apr-09 9:20 
AnswerRe: sprintf and rounding issues Pin
CPallini20-Apr-09 10:38
mveCPallini20-Apr-09 10:38 
QuestionText in OpenGL Pin
yousuf_227920-Apr-09 8:12
professionalyousuf_227920-Apr-09 8:12 
AnswerRe: Text in OpenGL Pin
Stuart Dootson20-Apr-09 8:24
professionalStuart Dootson20-Apr-09 8:24 
GeneralRe: Text in OpenGL Pin
yousuf_227920-Apr-09 8:31
professionalyousuf_227920-Apr-09 8:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.