graph using adjacency list


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: graph using adjacency list

Threaded View

  1. #1
    Join Date
    Apr 2010
    Posts
    1

    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 05:12 AM.

Similar Threads

  1. Replies: 7
    Last Post: 03-17-2009, 01:40 AM
  2. C++ Graph Representation Problem
    By Peter_APIIT in forum C++
    Replies: 10
    Last Post: 01-23-2009, 03:48 AM
  3. Replies: 6
    Last Post: 09-22-2006, 01:52 PM
  4. Adjacency list representation of a graph
    By ThePrise in forum Java
    Replies: 4
    Last Post: 11-23-2005, 02:53 PM
  5. ListBot Going Out of Business
    By Larry Rebich in forum vb.announcements
    Replies: 1
    Last Post: 06-28-2001, 02: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center