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

  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.

  2. #2
    Join Date
    Apr 2010
    Posts
    5
    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.

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    Quote Originally Posted by MirekCerny View Post
    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

  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