|
-
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.
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, 02: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, 01: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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks