Write a program that outputs the nodes of a graph in a breadth first traversal. in c ++  10 0 1 3 -999 1 4 -999 2 5 -999 3 2 -999 4 -999 5 7 8 -999 6 4 7 -999 7 -999 8 -999 9 7 8 -999 #include #include #include #include using namespace std; template struct nodeType { Type info; nodeType *link; }; template class linkedListIterator { public: linkedListIterator() { current = nullptr; } linkedListIterator(nodeType *ptr) { current = ptr; } Type operator*() { return current->info; } linkedListIterator operator++() { current = current->link; return *this; } bool operator==(const linkedListIterator& right) const { return (current == right.current); } bool operator!=(const linkedListIterator& right) const { return (current != right.current); } private: nodeType *current; }; template class linkedListType { public: const linkedListType& operator=(const linkedListType&) { if (this != &otherList) { copyList(otherList); } return *this; } void initializeList() { destroyList(); } bool isEmptyList() const { return (first == nullptr); } void print() const { nodeType *current; current = first; while (current != nullptr) { cout << current->info << " "; current = current->link; } } int length() const { return count; } void destroyList() { nodeType *temp; while (first != nullptr) { temp = first; first = first->link; delete temp; } last = nullptr; count = 0; } Type front() const { assert(first != nullptr); return first->info; } Type back() const { assert(last != nullptr); return last->info; } virtual bool search(const Type& searchItem) const = 0; virtual void insertFirst(const Type& newItem) = 0; virtual void insertLast(const Type& newItem) = 0; virtual void deleteNode(const Type& deleteItem) = 0; linkedListIterator begin() { linkedListIterator temp(first); return temp; } linkedListIterator end() { linkedListIterator temp(nullptr); return temp; } linkedListType() { first = nullptr; last = nullptr; count = 0; } linkedListType(const linkedListType& otherList) { first = nullptr; copyList(otherList); } ~linkedListType() { destroyList(); } protected: int count; nodeType *first; nodeType *last; private: void copyList(const linkedListType& otherList) { nodeType *newNode; nodeType *current; if (first != nullptr) destroyList(); if (otherList.first == nullptr) { first = nullptr; last = nullptr; count = 0; } else { current = otherList.first; count = otherList.count; first = new nodeType; first->info = current->info; first->link = nullptr; #include #include using namespace std; class Graph { int V; list *adj; void DFSUtil(int v, bool visited[]); public: Graph(int V); void addEdge(int v, int w); void DFS(int v); }; Graph::Graph(int V) { this->V = V; adj = new list[V]; } void Graph::addEdge(int v, int w) { adj[v].push_back(w); } void Graph::DFSUtil(int v, bool visited[]) { visited[v] = true; cout << v << " "; list::iterator i; for (i = adj[v].begin(); i != adj[v].end(); ++i) { if (!visited[*i]) { DFSUtil(*i, visited); } } } void Graph::DFS(int v) { bool *visited = new bool[V]; for (int i = 0; i < V; i++) { visited[i] = false; } DFSUtil(v, visited); } int main() { Graph g(10); g.addEdge(0, 1); g.addEdge(0, 3); g.addEdge(1, 4); g.addEdge(3, 2); g.addEdge(2, 5); g.addEdge(5, 7); g.addEdge(5, 8); cout << "Following is Depth First Traversal \n"; g.DFS(0); cout << endl; system("pause"); } please help me figure out how to do it

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Write a program that outputs the nodes of a graph in a breadth first traversal. in c ++ 
10
0 1 3 -999
1 4 -999
2 5 -999
3 2 -999
4 -999
5 7 8 -999
6 4 7 -999
7 -999
8 -999
9 7 8 -999
#include <iostream> #include <fstream> #include <iomanip> #include <cassert> using namespace std; template <class Type> struct nodeType { Type info; nodeType<Type> *link; }; template <class Type> class linkedListIterator { public: linkedListIterator() { current = nullptr; } linkedListIterator(nodeType<Type> *ptr) { current = ptr; } Type operator*() { return current->info; } linkedListIterator<Type> operator++() { current = current->link; return *this; } bool operator==(const linkedListIterator<Type>& right) const { return (current == right.current); } bool operator!=(const linkedListIterator<Type>& right) const { return (current != right.current); } private: nodeType<Type> *current; }; template <class Type> class linkedListType { public: const linkedListType<Type>& operator=(const linkedListType<Type>&) { if (this != &otherList) { copyList(otherList); } return *this; } void initializeList() { destroyList(); } bool isEmptyList() const { return (first == nullptr); } void print() const { nodeType<Type> *current; current = first; while (current != nullptr) { cout << current->info << " "; current = current->link; } } int length() const { return count; } void destroyList() { nodeType<Type> *temp; while (first != nullptr) { temp = first; first = first->link; delete temp; } last = nullptr; count = 0; } Type front() const { assert(first != nullptr); return first->info; } Type back() const { assert(last != nullptr); return last->info; } virtual bool search(const Type& searchItem) const = 0; virtual void insertFirst(const Type& newItem) = 0; virtual void insertLast(const Type& newItem) = 0; virtual void deleteNode(const Type& deleteItem) = 0; linkedListIterator<Type> begin() { linkedListIterator<Type> temp(first); return temp; } linkedListIterator<Type> end() { linkedListIterator<Type> temp(nullptr); return temp; } linkedListType() { first = nullptr; last = nullptr; count = 0; } linkedListType(const linkedListType<Type>& otherList) { first = nullptr; copyList(otherList); } ~linkedListType() { destroyList(); } protected: int count; nodeType<Type> *first; nodeType<Type> *last; private: void copyList(const linkedListType<Type>& otherList) { nodeType<Type> *newNode; nodeType<Type> *current; if (first != nullptr) destroyList(); if (otherList.first == nullptr) { first = nullptr; last = nullptr; count = 0; } else { current = otherList.first; count = otherList.count; first = new nodeType<Type>; first->info = current->info; first->link = nullptr;

#include<iostream>
#include<list>
using namespace std;

class Graph
{
int V;
list<int> *adj;
void DFSUtil(int v, bool visited[]);

public:
Graph(int V);
void addEdge(int v, int w);
void DFS(int v);
};

Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
}

void Graph::DFSUtil(int v, bool visited[])
{
visited[v] = true;
cout << v << " ";
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
{
if (!visited[*i])
{
DFSUtil(*i, visited);
}
}
}

void Graph::DFS(int v)
{
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
{
visited[i] = false;
}
DFSUtil(v, visited);
}

int main()
{
Graph g(10);


g.addEdge(0, 1);
g.addEdge(0, 3);
g.addEdge(1, 4);
g.addEdge(3, 2);
g.addEdge(2, 5);
g.addEdge(5, 7);
g.addEdge(5, 8);

cout << "Following is Depth First Traversal \n";
g.DFS(0);
cout << endl;
system("pause");
}

please help me figure out how to do it
 
 
 
 
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Merge Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education