Data Structure in C++ Implement the commands and create the graph… #include <ios
ID: 3841091 • Letter: D
Question
Data Structure in C++
Implement the commands and create the graph…
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <algorithm>
#include <vector>
#include <functional>
#include <queue>
using namespace std;
class graph {
int V;
list<int> *adj;
public:
graph(int V) {
this->V = V;
adj = new list<int>[V];
}
void create(int val, string reg){
adj[val].push_back(reg); // Add reg
}
void print(string reg){
}
void arc(string r1, string r2, string result){
}
void biarc(string r1, string r2, string result){
}
void bfs(string r1, string r2, string result){
}
};
int main(){
using std::cin;
using std::cout;
using std::endl;
string line;
graph execute = graph();
while(true){
cout << "> ";
getline(cin,line);
string word; // Have a buffer string
std::stringstream ss(line); // Insert the string into a stream
vector<string> words; // Create vector to hold our words
while (ss >> word)
words.push_back(word);
if(words[0] == "create"){
int val = atoi(words[1].c_str()); // Change string into$
execute.create(val, words[2]);
}
else if(words[0] == "print"){
execute.print(words[1]);
}
else if(words[0] == "arc"){ execute.arc(words[1], words[2], words[3]);
}
else if(words[0] == "biarc"){
execute.biarc(words[1], words[2], words[3]);
}
else if(words[0] == "bfs"){
execute.bfs(words[1], words[2], words[3]);
}
else if(words[0] == "stop"){
return 0;
}
else{
cout << "No command with that name exists" << endl;
}
}
}
Part 2: Commands Implement the following commands Command Description create nr Create a new empty graph in register n with nnodes Print some representation of the graph in r print r arc a br n the graph in register r create an arc from node ato b biarc a br Create a bidirectional arc from ato bin r bfs abr Perform a breadth-first search from a to b in r, printing the distance Write the register commands necessary to create the following graph: and place them in a comment in your source code. What is the distance fromnode 9 to node 6?Explanation / Answer
#include<stdlib.h>
#include<iostream.h>
#include<string.h>
#include<err/sys.h>
void createGraph();
void df();
void bf();
struct node
{
int data,status;
struct node *next;
struct link *adj;
};
struct link
{
struct node *next;
struct link *adj;
};
struct node *start,*m,*n;
struct link *x,*y;
int main()
{
int choice;
clrscr();
create();
while(1)
{
cout<<"
-----------------------";
cout<<"
1: DF
2: BS
3: Exit
Enter your choice: ";
cin>>choice;
switch(choice)
{
case A:
df();
break;
case B:
bf();
break;
case C:
exit(0);
break;
default:
cout<<"
Incorrect choice!
Re-enter your choice.";
getch();
}
}
return 0;
}
void createGraph()
{
int dat,flag=0;
start=NULL;
cout<<"
Enter the nodes in the graph(0 to end): ";
while(1)
{
cin>>dat;
if(dat==0)
break;
m=new node;
m->data=dat;
m->status=0;
m->next=NULL;
m->adj=NULL;
if(flag==0)
{
start=m;
n=m;
flag++;
}
else
{
n->next=m;
n=m;
}
}
m=start;
while(m!=NULL)
{
cout<<"Enter the links to "<<m->data<<" (0 to end) : ";
flag=0;
while(1)
{
cin>>dat;
if(dat==0)
break;
k=new link;
k->adj=NULL;
if(flag==0)
{
m->adj=k;
l=k;
flag++;
}
else
{
l->adj=k;
l=k;
}
n=start;
while(q!=NULL)
{
if(n->data==dat)
k->next=n;
n=n->next;
}
}
n=m->next;
}
cout<<"
-------------------------";
return;
}
// Deapth First Search(DFS) Traversal.
void bfs()
{
int qu[20],i=1,j=0;
m=start;
while(p!=NULL)
{
m->status=0;
m=m->next;
}
m=start;
qu[0]=m->data;
m->status=1;
while(1)
{
if(qu[j]==0)
break;
m=start;
while(p!=NULL)
{
if(m->data==qu[j])
break;
m=m->next;
}
k=m->adj;
while(k!=NULL)
{
n=k->next;
if(n->status==0)
{
qu[i]=n->data;
n->status=1;
qu[i+1]=0;
i++;
}
k=k->adj;
}
j++;
}
j=0;
cout<<"
Breadth First Search Results
";
cout<<"
---------------------------
";
while(qu[j]!=0)
{
cout<<qu[j]<<" ";
j++;
}
getch();
return;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.