BFS with Adjacency List!
Sep 4, 2011 at 3:32pm UTC
Hi everyone, I have created an adjacency list and I want to apply the BFS.
Anybody can help? Thanks in advance!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#include<stdlib.h>
#include<iostream>
using namespace std;
class Vertex
{
friend class List;
int value;
Vertex *next;
Vertex *end;
Vertex() {value= 0; next=NULL; end=NULL;}
void setValue(int node) {value=node;}
int getValue() {return value;}
void ptrNext(Vertex *ptr) {next=ptr;}
Vertex *getNext() {return next;}
void ptrEnd(Vertex *ptr) {end=ptr;}
Vertex *getEnd() {return end;}
};
class List {
Vertex *first;
Vertex *last;
public :
List() {first=NULL; last=NULL;}
void makeList() {
int node;
Vertex *newNode;
Vertex *lastNode;
cout<<"\n Insert Node: " ;
cin>>node;
newNode=new Vertex;
newNode->setValue(node);
if (first==NULL) {
first=newNode;
last=newNode;
}
else {
lastNode=first;
while ((lastNode->getEnd()!=NULL)&&(lastNode->getValue()!=node)) {
lastNode=lastNode->getEnd();
}
if (lastNode->getValue()!=node) {
last->ptrEnd(newNode);
last=newNode;
}
else {
cout<<"\n The inserted node is already present!" ;
}
}
}
void setAdj() {
Vertex *adjValue, *currentNode, *lastAdj;
int adjNode;
int flag;
if (first==NULL) {
cout<<"\n There aren't any nodes!\n" ;
}
else {
currentNode=first;
while (currentNode!=NULL) {
cout<<"\n Insert the adjacencies of " <<currentNode->getValue()<<"\n " ;
lastAdj=currentNode;
system("cls" );
cout<<"\n 0: Go to the next node!" ;
cout<<"\n 1: Continue with the adjacencies of " <<currentNode->getValue()<<": " ;
cin>>flag;
if (flag==1) {
cout<<"\n Insert the adjacent node: " ;
cin>>adjNode;
adjValue=new Vertex;
adjValue->setValue(adjNode);
if ((lastAdj->getNext()==NULL)&&(currentNode->getValue()!=adjNode)) {
lastAdj->ptrNext(adjValue);
lastAdj=adjValue;
}
else {
while ((lastAdj->getNext()!=NULL)&&((lastAdj->getValue()!=adjNode)&&(currentNode->getValue()!=adjNode))) {
lastAdj=lastAdj->getNext();
}
if ((currentNode->getValue()==adjNode)||(lastAdj->getValue()==adjNode)) {
cout<<"\n The inserted node was already declared as adjacent!" ;
}
else {
lastAdj->ptrNext(adjValue);
lastAdj=adjValue;
}
}
}
else {
currentNode=currentNode->getEnd();
}
}
}
}
void printList() {
Vertex *node, *adj;
node=first;
if (first==NULL) {
cout<<"\n The graph is empty!\n" ;
}
else {
cout<<"\n Adjacency list:\n " ;
while (node!=NULL) {
cout<< "\n" << node->getValue()<< " --- " ;
adj=node->getNext();
while (adj!=NULL) {
cout<<adj->getValue()<<" " ;
adj=adj->getNext();
}
node=node->getEnd();
}
}
}
};
int main() {
int choice;
List call;
do
{
cout<<"\n\n 1: Insert node!" ;
cout<<"\n 2: Set adjacency!" ;
cout<<"\n 3: Print adjacency list!" ;
cout<<"\n 4: Exit!" ;
cout<<"\n\n Choice: " ;
cin>>choice;
switch (choice)
{
case 1:
system("cls" );
call.makeList();
break ;
case 2:
system("cls" );
call.setAdj();
break ;
case 3:
system("cls" );
call.printList();
break ;
case 4:
break ;
}
} while (choice!=4);
}
Sep 4, 2011 at 10:02pm UTC
You forgot to describe the problem. What is the program supposed to do? What errors are you getting?
Sep 4, 2011 at 10:30pm UTC
There are no errors.
I made the adjacency list (with dynamic values) and now I want to visit the graph using BFS, but I don't know where to start from!
Topic archived. No new replies allowed.