title: Adjacency list to create graph and Breadth First Search (BFS) and Fepth First Search (DFS)
body: <p>Here is my code which implements graph using adjacency list and do a Breadth First Search (BFS) and Depth First Search (DFS).
  While creating graph and adding edges I am facing problem of a fault in creating graph. I am not sure of the pointer that I have used.
  Can you suggest where am I wrong?</p>
  
  <pre><code>#include "Graph.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  int Graph::pop()
  {
      if (top = -1) {
          cout &lt;&lt; "empty";
          return 0;
      } else
          return stack[top--];
  }
  
  void Graph::push(int value)
  {
      if (top &gt;= 50) {
          cout &lt;&lt; "full";
  
      } else
          stack[++top] = value;
  }
  
  void Graph::add(int val)
  {
      if (rear != 49)
          qu[++rear] = val;
      else
          cout &lt;&lt; "full";
  }
  
  int Graph::del()
  {
      if (rear == front) {
          cout &lt;&lt; "empty";
          return 0;
      } else
          front += 1;
      return qu[front];
  }
  
  int Graph::gethead()
  {
      return rear;
  }
  
  int Graph::gettail()
  {
      return front;
  }
  
  node *Graph::createnode(int t)
  {
      node *temp;
      temp = new node;
      temp-&gt;val = t;
      temp-&gt;next = NULL;
      return temp;
  }
  
  
  
  Graph::Graph()
  {
      rear = -1;
      front = -1;
      top = -1;
  }
  
  Graph::Graph(int t)
  {
      struct arr r[t];
      a = r;
      for (int i = 0; i &lt; t; i++) {
          a[i].key = i;
          a[i].begin = NULL;
          a[i].end = NULL;
      }
  
      int q[t];
      p = q;
      for (int k = 0; k &lt; t; k++) {
          p[k] = 0;
      }
  }
  
  void Graph::visited(int v1)
  {
      p[v1] = 1;
  }
  
  bool Graph::isvisited(int v1)
  {
      if (p[v1] == 1)
          return true;
      else
          false;
  }
  
  void Graph::addEdge(int v1, int v2)
  {
  
      if (a[v1].begin == NULL) {
          node *temp = createnode(v2);
          a[v1].begin = temp;
          a[v1].end = temp;
      } else {
          node *temp = createnode(v2);
          node *temp1 = a[v1].end;
          temp1-&gt;next = temp;
          a[v1].end = temp;
      }
  
      if (a[v2].begin == NULL) {
          node *temp = createnode(v1);
          a[v2].begin = temp;
          a[v2].end = temp;
      } else {
          node *temp = createnode(v1);
  
          //node *temp2=a[v2].end;
          //temp2-&gt;next=temp;
          a[v2].end-&gt;next = temp;
          //a[v2].end-&gt;next=temp;
          a[v2].end = temp;
      }
  }
  
  void Graph::deleteEdge(int v1, int v2)
  {
      node *temp;
      temp = a[v1].begin;
      if (a[v1].begin-&gt;val == v2) {
          a[v1].begin = a[v1].begin-&gt;next;
          delete(temp);
          temp = NULL;
      } else {
          node *t = temp-&gt;next;
          while (t != NULL) {
              if (t-&gt;val == v2) {
                  temp-&gt;next = t-&gt;next;
                  delete(t);
              } else {
                  temp = t;
                  t = t-&gt;next;
              }
          }
      }
  
  
      node *temp1;
      temp1 = a[v2].begin;
      if (a[v2].begin-&gt;val == v1) {
          a[v2].begin = a[v2].begin-&gt;next;
          delete(temp);
          temp = NULL;
      } else {
          node *t = temp-&gt;next;
          while (t != NULL) {
              if (t-&gt;val == v1) {
                  temp-&gt;next = t-&gt;next;
                  delete(t);
              } else {
                  temp = t;
                  t = t-&gt;next;
              }
          }
      }
  }
  
  void Graph::BFS(int source)
  {
      add(source);
      visited(source);
      node *temp = a[source].begin;
      while (temp != a[source].end) {
          add(temp-&gt;val);
      }
      int r = del();
      cout &lt;&lt; r &lt;&lt; endl;
      while (gethead() != gettail()) {
          int v1 = del();
          visited(v1);
          node *temp1;
          temp1 = a[v1].begin;
          while (temp1 != NULL) {
              if (p[temp1-&gt;val] != 1) {
                  add(temp1-&gt;val);
                  temp1 = temp1-&gt;next;
              } else
                  temp1 = temp1-&gt;next;
          }
          if (temp1 == NULL)
              cout &lt;&lt; v1 &lt;&lt; endl;
      }
  }
  
  void Graph::DFS(int source)
  {
      int c = source;
      cout &lt;&lt; c;
      visited(c);
      node *temp2 = a[source].begin;
      while (a[source].end != NULL) {
          push(temp2-&gt;val);
          temp2 = temp2-&gt;next;
      }
      c = pop();
      while (top != -1) {
          node *temp = a[c].begin;
          if (p[c] != 1) {
              cout &lt;&lt; c;
              while (temp != NULL) {
                  push(temp-&gt;val);
                  temp = temp-&gt;next;
                  visited(c);
                  c = pop();
              }
          }
          if (p[c] = 1) {
              c = pop();
          }
      }
  }
  </code></pre>