-
create graph
#include<iostream>
using namespace std;
class Graph{
public:
void addVertex(int vertex);
void display();
TCSGraph(){
head = NULL;
}
~Graph(){}
private:
struct ListNode
{
string name;
struct ListNode *next;
};
ListNode *head;
};
void Graph::addVertex(int vertex){
ListNode *newNode;
ListNode *nodePtr;
string vName;
for(int i = 0; i < vertex ; i++ ){
cout << "what is the name of the vertex"<< endl;
cin >> vName;
newNode = new ListNode;
newNode->name = vName;
newNode->next = NULL;
if (!head)
head = newNode;
else{
nodePtr = head;
while(nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
}
void Graph::display(){
ListNode *nodePtr;
nodePtr = head;
while(nodePtr){
cout << nodePtr->name<< endl;
nodePtr = nodePtr->next;
}
}
void displayMenu(){
cout << endl << endl;
cout << " 1. Add a vertice" << endl;
cout << " 2. Displaying a graph by printing out the adjacency lists" << endl;
cout << " 0. Quit" << endl;
cout << endl;
cout << "Please enter your option: ";
}
int main(){
int choice;
int vertex;
bool done = false;
do{
displayMenu();
cin >> choice;
TCSGraph g;
if(choice==1){
cout << " how many vertex u wan to add" << endl;
cin >> vertex;
g.addVertex(vertex);
}
else if (choice==2){
g.display();
}
}while(!done);
return 0;
}
Last edited by ITNoob; 04-22-2010 at 04:12 AM.
-
Hello,
your problem is that every pass of the loop, you create and destroy the TCSGraph object. Therefore it is always empty when you try to display it.
You have to put the declaration of the variable before the loop.
Second, you don't dealocate the dynamically created Nodes when TCSGraph is destroyed, which results in memory leaks.
And last but not least, if this is the verbatim source, learn to indent the code properly. It helps with reading and orientation immensely.
-
 Originally Posted by MirekCerny
Hello,
your problem is that every pass of the loop, you create and destroy the TCSGraph object. Therefore it is always empty when you try to display it.
You have to put the declaration of the variable before the loop.
Second, you don't dealocate the dynamically created Nodes when TCSGraph is destroyed, which results in memory leaks.
And last but not least, if this is the verbatim source, learn to indent the code properly. It helps with reading and orientation immensely.
The web site destroys indention unless you use [CODE and [/CODE tags.
Similar Threads
-
By justinsbabe5000 in forum Java
Replies: 7
Last Post: 03-17-2009, 12:40 AM
-
By Peter_APIIT in forum C++
Replies: 10
Last Post: 01-23-2009, 03:48 AM
-
By Shailesh33 in forum C++
Replies: 6
Last Post: 09-22-2006, 12:52 PM
-
By ThePrise in forum Java
Replies: 4
Last Post: 11-23-2005, 02:53 PM
-
By Larry Rebich in forum vb.announcements
Replies: 1
Last Post: 06-28-2001, 01:22 PM
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|