SlideShare a Scribd company logo
// This code currently works. Run it and get a screen shot of its
output.
// Next, Convert this code to a class. Have the class in the same
file as the int main.
//please get rid of struct and using :: to covert
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct AdjListNode
{
int dest;
struct AdjListNode* next;
};
struct AdjList
{
struct AdjListNode *head;
};
struct Graph
{
int V;
struct AdjList* array;
};
struct AdjListNode * newAdjListNode(int dest)
{
struct AdjListNode * newNode = new AdjListNode;
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
struct Graph* createGraph(int V)
{
struct Graph* graph = new Graph;
graph->V = V;
graph->array = new AdjList;
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
void addEdge(struct Graph* graph, int src, int dest)
{
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
struct AdjListNode* pCrawl = graph->array[v].head;
cout << endl << "Adjacency list of vertex " << v <<
endl << " head";
while (pCrawl)
{
cout << "-> " << pCrawl->dest;
pCrawl = pCrawl->next;
}
cout << endl;
}
}
int main()
{
int V = 5;
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
printGraph(graph);
system("pause");
return 0;
}
Solution
With using struct:
program:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct AdjListNode
{
int dest;
struct AdjListNode* next;
};
struct AdjList
{
struct AdjListNode *head;
};
struct Graph
{
int V;
struct AdjList* array;
};
struct AdjListNode * newAdjListNode(int dest)
{
struct AdjListNode * newNode = new AdjListNode;
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
struct Graph* createGraph(int V)
{
struct Graph* graph = new Graph;
graph->V = V;
graph->array = new AdjList;
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
void addEdge(struct Graph* graph, int src, int dest)
{
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
struct AdjListNode* pCrawl = graph->array[v].head;
cout << endl << "Adjacency list of vertex " << v <<
endl << " head";
while (pCrawl)
{
cout << "-> " << pCrawl->dest;
pCrawl = pCrawl->next;
}
cout << endl;
}
}
int main()
{
int V = 5;
struct Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
printGraph(graph);
system("pause");
return 0;
}
Using Classes:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class AdjListNode
{
public:
int dest;
AdjListNode* next;
};
class AdjList
{
public:
AdjListNode *head;
};
class Graph
{
public:
int V;
AdjList* array;
};
AdjListNode* newAdjListNode(int dest)
{
AdjListNode * newNode = new AdjListNode;
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
Graph* createGraph(int V)
{
Graph* graph = new Graph;
graph->V = V;
graph->array = new AdjList;
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
void addEdge(Graph* graph, int src, int dest)
{
AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
void printGraph(Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
AdjListNode* pCrawl = graph->array[v].head;
cout << endl << "Adjacency list of vertex " << v << endl <<
" head";
while (pCrawl)
{
cout << "-> " << pCrawl->dest;
pCrawl = pCrawl->next;
}
cout << endl;
}
}
int main()
{
int V = 5;
Graph* graph = createGraph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
printGraph(graph);
system("pause");
return 0;
}

More Related Content

DOCX
This code currently works... Run it and get a screen shot of its .docx
PDF
Im having trouble figuring out how to code these sections for an a.pdf
PDF
C code on linked list #include stdio.h #include stdlib.h.pdf
PPT
Data structures cs301 power point slides lecture 03
PDF
could you implement this function please, im having issues with it..pdf
PDF
pleaase I want manual solution forData Structures and Algorithm An.pdf
PPT
computer notes - Data Structures - 3
PDF
Can you finish and write the int main for the code according to the in.pdf
This code currently works... Run it and get a screen shot of its .docx
Im having trouble figuring out how to code these sections for an a.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
Data structures cs301 power point slides lecture 03
could you implement this function please, im having issues with it..pdf
pleaase I want manual solution forData Structures and Algorithm An.pdf
computer notes - Data Structures - 3
Can you finish and write the int main for the code according to the in.pdf

Similar to This code currently works. Run it and get a screen shot of its ou.docx (20)

PDF
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
PDF
Write a C program that reads the words the user types at the command.pdf
PDF
Rewrite this code so it can use a generic type instead of integers. .pdf
PDF
can you add a delete button and a add button to the below program. j.pdf
DOCX
#include stdafx.h #include iostream using namespace std;vo.docx
PDF
C++ programs
DOCX
Write a program that reads a graph from a file and determines whether.docx
PDF
in this assignment you are asked to write a simple driver program an.pdf
PDF
Lab-2.4 101.pdf
ZIP
PDF
Complete the implementation of the Weighted Graph that we began in t.pdf
PDF
TutorialII_Updated____niceupdateprogram.pdf
PPTX
C Assignment Help
PDF
So I have this code(StackInAllSocks) and I implemented the method but.pdf
PDF
C program
PPT
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
PDF
maincpp Build and procees a sorted linked list of Patie.pdf
PDF
Most Important C language program
PDF
TypeScript Introduction
DOCX
Lab Week 2 Game Programming.docx
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
Write a C program that reads the words the user types at the command.pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
can you add a delete button and a add button to the below program. j.pdf
#include stdafx.h #include iostream using namespace std;vo.docx
C++ programs
Write a program that reads a graph from a file and determines whether.docx
in this assignment you are asked to write a simple driver program an.pdf
Lab-2.4 101.pdf
Complete the implementation of the Weighted Graph that we began in t.pdf
TutorialII_Updated____niceupdateprogram.pdf
C Assignment Help
So I have this code(StackInAllSocks) and I implemented the method but.pdf
C program
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
maincpp Build and procees a sorted linked list of Patie.pdf
Most Important C language program
TypeScript Introduction
Lab Week 2 Game Programming.docx

More from Komlin1 (20)

DOCX
Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
DOCX
Theory and Research Related to Social Issue By now, you have had t.docx
DOCX
Theory and the White-Collar OffenderOur previous week’s discussion.docx
DOCX
There are 2 questions part A and B. All questions and relevant att.docx
DOCX
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
DOCX
Theoretical PerspectiveIdentify at least one human developme.docx
DOCX
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
DOCX
Theories of Behavior TimelineComplete the following tabl.docx
DOCX
Thematic Issues Globalization; Islam & the West.docx
DOCX
The written portion of the research paper should be 9-11 pages in le.docx
DOCX
The World since 1945Country Report- SAUDI ARABIA     Histo.docx
DOCX
The world runs on Big Data.  Traditionally, Data has been expressed .docx
DOCX
the    1.The collaborative planning Methodology is the f.docx
DOCX
The word stereotype originally referred to a method used by printers.docx
DOCX
The Value of Critical Thinking  Please respond to the followin.docx
DOCX
The Value Chain Concept Please respond to the following·.docx
DOCX
The wealth and energy between 1880 and 1910 was a unique and dynamic.docx
DOCX
The Value of Research in Social PolicyWhile research can be intere.docx
DOCX
The United States’ foreign policy until the end of the nineteenth ce.docx
DOCX
The Value Chain Concept Please respond to the followingDescribe.docx
Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
Theory and Research Related to Social Issue By now, you have had t.docx
Theory and the White-Collar OffenderOur previous week’s discussion.docx
There are 2 questions part A and B. All questions and relevant att.docx
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
Theoretical PerspectiveIdentify at least one human developme.docx
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
Theories of Behavior TimelineComplete the following tabl.docx
Thematic Issues Globalization; Islam & the West.docx
The written portion of the research paper should be 9-11 pages in le.docx
The World since 1945Country Report- SAUDI ARABIA     Histo.docx
The world runs on Big Data.  Traditionally, Data has been expressed .docx
the    1.The collaborative planning Methodology is the f.docx
The word stereotype originally referred to a method used by printers.docx
The Value of Critical Thinking  Please respond to the followin.docx
The Value Chain Concept Please respond to the following·.docx
The wealth and energy between 1880 and 1910 was a unique and dynamic.docx
The Value of Research in Social PolicyWhile research can be intere.docx
The United States’ foreign policy until the end of the nineteenth ce.docx
The Value Chain Concept Please respond to the followingDescribe.docx

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Lesson notes of climatology university.
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Structure & Organelles in detailed.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Chinmaya Tiranga quiz Grand Finale.pdf
Lesson notes of climatology university.
A systematic review of self-coping strategies used by university students to ...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
2.FourierTransform-ShortQuestionswithAnswers.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharma ospi slides which help in ospi learning
Cell Structure & Organelles in detailed.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Institutional Correction lecture only . . .
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Computing-Curriculum for Schools in Ghana
Microbial diseases, their pathogenesis and prophylaxis
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Final Presentation General Medicine 03-08-2024.pptx

This code currently works. Run it and get a screen shot of its ou.docx

  • 1. // This code currently works. Run it and get a screen shot of its output. // Next, Convert this code to a class. Have the class in the same file as the int main. //please get rid of struct and using :: to covert #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; struct AdjListNode { int dest; struct AdjListNode* next; }; struct AdjList { struct AdjListNode *head; }; struct Graph { int V; struct AdjList* array; }; struct AdjListNode * newAdjListNode(int dest) { struct AdjListNode * newNode = new AdjListNode; newNode->dest = dest; newNode->next = NULL; return newNode; }
  • 2. struct Graph* createGraph(int V) { struct Graph* graph = new Graph; graph->V = V; graph->array = new AdjList; int i; for (i = 0; i < V; ++i) graph->array[i].head = NULL; return graph; } void addEdge(struct Graph* graph, int src, int dest) { struct AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; } void printGraph(struct Graph* graph) { int v; for (v = 0; v < graph->V; ++v) { struct AdjListNode* pCrawl = graph->array[v].head; cout << endl << "Adjacency list of vertex " << v << endl << " head"; while (pCrawl) {
  • 3. cout << "-> " << pCrawl->dest; pCrawl = pCrawl->next; } cout << endl; } } int main() { int V = 5; struct Graph* graph = createGraph(V); addEdge(graph, 0, 1); addEdge(graph, 0, 4); addEdge(graph, 1, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 3); addEdge(graph, 3, 4); printGraph(graph); system("pause"); return 0; } Solution With using struct: program: #include "stdafx.h"
  • 4. #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; struct AdjListNode { int dest; struct AdjListNode* next; }; struct AdjList { struct AdjListNode *head; }; struct Graph { int V; struct AdjList* array; }; struct AdjListNode * newAdjListNode(int dest) { struct AdjListNode * newNode = new AdjListNode;
  • 5. newNode->dest = dest; newNode->next = NULL; return newNode; } struct Graph* createGraph(int V) { struct Graph* graph = new Graph; graph->V = V; graph->array = new AdjList; int i; for (i = 0; i < V; ++i) graph->array[i].head = NULL; return graph; } void addEdge(struct Graph* graph, int src, int dest) { struct AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode;
  • 6. newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; } void printGraph(struct Graph* graph) { int v; for (v = 0; v < graph->V; ++v) { struct AdjListNode* pCrawl = graph->array[v].head; cout << endl << "Adjacency list of vertex " << v << endl << " head"; while (pCrawl) { cout << "-> " << pCrawl->dest; pCrawl = pCrawl->next; } cout << endl; } } int main() { int V = 5;
  • 7. struct Graph* graph = createGraph(V); addEdge(graph, 0, 1); addEdge(graph, 0, 4); addEdge(graph, 1, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 3); addEdge(graph, 3, 4); printGraph(graph); system("pause"); return 0; } Using Classes: #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; class AdjListNode { public: int dest; AdjListNode* next; };
  • 8. class AdjList { public: AdjListNode *head; }; class Graph { public: int V; AdjList* array; }; AdjListNode* newAdjListNode(int dest) { AdjListNode * newNode = new AdjListNode; newNode->dest = dest; newNode->next = NULL; return newNode; } Graph* createGraph(int V) { Graph* graph = new Graph; graph->V = V; graph->array = new AdjList;
  • 9. int i; for (i = 0; i < V; ++i) graph->array[i].head = NULL; return graph; } void addEdge(Graph* graph, int src, int dest) { AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; } void printGraph(Graph* graph) { int v; for (v = 0; v < graph->V; ++v) { AdjListNode* pCrawl = graph->array[v].head; cout << endl << "Adjacency list of vertex " << v << endl << " head"; while (pCrawl) { cout << "-> " << pCrawl->dest;
  • 10. pCrawl = pCrawl->next; } cout << endl; } } int main() { int V = 5; Graph* graph = createGraph(V); addEdge(graph, 0, 1); addEdge(graph, 0, 4); addEdge(graph, 1, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 3); addEdge(graph, 3, 4); printGraph(graph); system("pause"); return 0; }