Implement means code; Use C++ Part 1: Implement the stack traversal for the pseu
ID: 3684600 • Letter: I
Question
Implement means code; Use C++
Part 1: Implement the stack traversal for the pseudocode template given below, and produce identical output. USE NODE C as the ROOT.
Part 2: Modify your implementation to traverse GRAPH 2 shown below. USE NODE A as the ROOT.
Part 4: Modify your implementation to traverse the graph in the posted file “BiggieGraph.txt”. In addition to the default output, determine and print the LONGEST BRANCH in the resulting tree. USE NODE A as the ROOT.
Full Prompt File: http://wserver.flc.losrios.edu/~ross/CISP430S16.SPLWQKFGXWKTGZHS.BOB/HW11-S16-GraphStack.pdf
Program Diagram: http://wserver.flc.losrios.edu/~ross/CISP430S16.SPLWQKFGXWKTGZHS.BOB/GraphStack.pdf
BiggieGraph.txt: http://wserver.flc.losrios.edu/~ross/CISP430S16.SPLWQKFGXWKTGZHS.BOB/BiggieGraph.txt
Explanation / Answer
#include <iostream.h> // input output stream header file
#include <stdio.h> // standard input output header file
#include <stdlib.h> // standard library header file
//#include <string.h> // header file with string function
// #include <conio.h> // console input output header file
// the graph adjacency matrix
/*
static int[,] graph = {
{0,1,1,0,0,0,0,0}, //A
{1,0,1,0,0,0,0,0}, //B
{1,1,0,1,0,1,0,0}, //C
{0,0,1,0,1,0,0,0}, //D
{0,0,0,1,0,1,0,0}, //E
{0,0,1,0,1,0,1,1}, //F
{0,0,0,0,0,1,0,0}, //G
{0,0,0,0,0,1,0,0}, //H
//A B C D E F G H
};
*/
// where
// where I've been
//static bool[] visited = {false, false, false, false, false, false,false, false};
// the resulting tree. Each node's parent is stored
//static int[] tree = {-1, -1, -1, -1, -1, -1, -1, -1};
struct nd { // nd = node = vertex
char data;
struct nd *next;
} *sp; // sp = stack pointer that points to top of the stack
class stackOfNodes {
public:
nd *pushInToTheStack(nd *, int);
nd *popOutOfTheStack(nd *);
void traverseTheStack(nd *);
stackOfNodes() {
sp = NULL;
} // end constructor
};
nd *stackOfNodes::pushInToTheStack(nd *sp, char dataIn) {
nd *locator
locator = new ( struct nd);
locator->data = dataIn;
locator->next = sp;
sp = locator;
return sp;
} // end func push
void *stackOfNodes::traverseTheStack(node *sp) {
// mark node as visited
nd *tracker;
tracker = sp;
cout << "The contents of the stack list are:";
while( tracker != NULL) {
cout << tracker->data << " , ";
tracker = tracker->next;
} // end while
} // end traverse func
void Main()
{
char data;
stackOfNodes stk1;
data = 'C';
sp = stk1.pushInToTheStack(sp, data);
data = 'A';
sp = stk1.pushInToTheStack(sp, data);
data = 'B';
sp = stk1.pushInToTheStack(sp, data);
data = 'D';
sp = stk1.pushInToTheStack(sp, data);
data = 'E';
sp = stk1.pushInToTheStack(sp, data);
data = 'F';
sp = stk1.pushInToTheStack(sp, data);
data = 'G';
sp = stk1.pushInToTheStack(sp, data);
data = 'H';
sp = stk1.pushInToTheStack(sp, data);
stk1.traverseTheStack(sp); // traversal includes printing of node's data
//stk1.pop(sp);
// "Push" C
// traverse(2);
// printtree();
} //end main
/* print(node)
init target to 0(A)
while(not at end of node’s list)
{
if(target is a neighbor and target is unvisited)
{
// who’s target’s daddy?
traverse(target) // push
}
next target
}
return // pop
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.