Linked Representation

We will solely focus on linked representation, where you can imagine in this way,

  • we have 2 table

  • first table has the nodes data alongside the edge list

  • each node has a separate table for it's own edge list (also can be NULL!)

So our skeleton class will be like this,

class Node {
public:
    int data;
    Node* next_link; 
    Node* adjacent;
    Node* destination;

    Node (int data) {
        this->data = data;
        this->next_link = NULL;
        this->adjacent = NULL;
        this->destination = NULL;
    }
    Node (Node* destination) {
        this->data = 0;
        this->next_link = NULL;
        this->adjacent = NULL;
        this->destination = destination;
    }
};

We will be using an another class to do operations on it! Here I am using one Node class for both node list and adjacency list, but feel free to divide it into two classes.

8.3 Finding Node

It's preety straightforward approach. A loop of course!

If you are not sure, how to actually place this function, here's a quick peek!

Here, printAll() will help you to debug and print!

8.4 Delete Node Only

Just like searching, if we are dealing with only nodes (let's forget about edge table!), check this way,

8.5 Finding Edge

For edge, we have to go through each nodes and then access it's edge list. So the journey begins from here,

8.6 Inserting Nodes

While inserting we can simply use our 8.3 function to ensure that function doesn't exist previously. That's it!

8.7 Inserting Edges

Inserting edges is gonna be a bit more tricky. We gotta check if our 2 desired nodes are there or not. And then we can think about inserting the data into our edge list.

8.8 Deleting Edge

While deleting edges, we also have to go through the same trouble as 8.7. Figure our either it is possible or not, then iterate through the edge list of node1.

8.9 Delete Node

In 8.4, we simply deleted a node. But what if we have an edge list for that node? It'll be a long job. We have to iterate through the edge list of each node and erase the trace of our sweet node that we don't want to eat :), and finally we can leave the rest to 8.4.

Wrapping up!

So by combining all those functions, we can write something like this,

Feel free to go through the commented lines, tweaking around!

Here print function is for your debugging friend!

And you can implement all these with STL library as well. Here's a sample,

Last updated

Was this helpful?