کد:
#include <iostream>
#include "conio.h"
#include "stdlib.h"
#include "ctype.h"
using namespace std;
#define M 9
//~~~~~~~~~~~~~~~constants for dijkstra~~~~~~~~~~
#define INFINITY 100
#define MEMBER 1
#define NOMEMBER 0
//~~~~~~~~~~~~~~~~~~~~~~~~END~~~~~~~~~~~~~~~~~~~~~
char node[M + 2];
//~~~~~~~~~~~~~~~~~ADJ LIST~~~~~~~~~~~~~~~~~~~~~~~~~~~
class adjList {
friend class nodeList;
private:
int info;
int weight;
adjList *next;
};
//~~~~~~~~~~~~~~~~~~~~~NODE TYPE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class nodeType {
friend class nodeList;
private:
char info;
adjList *listPtr;
nodeType *nextNode;
};
//~~~~~~~~~~~~~~~~~~~~~~~~NODE LIST~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class nodeList{
public:
nodeList();
~nodeList();
int** makeGraph();
void print();
void depth();
void BFS();
void dijkstra(int w[][M], int , int , int *);
private:
nodeType *start;
};
//~~~~~~~~~~~~~~~~~~~QUEUE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class queue {
friend void nodeList::BFS();//friend of BFS function in nodeList clss
public:
queue();
int empty();
void addQ(adjList *);
adjList* qremove(void);
private:
adjList *items[M+1];
int front;
int rear;
};
//~~~~~~~~~~~~~~~STACK~~~~~~~~~~~~~
class stack {
friend void nodeList::depth(); //friend of depth function
public:
stack();
int empty();
private:
int myTop;
adjList *item[M + 1];
};
//~~~~~~~~~~~~~~~~~DIJKSTRA~~~~~~~~~~~~~~~~~~~~~
کد:
#include "class header.h"
queue::queue()
{
front = 0;
rear = -1;
}
//~~~~~~~~~~~~~~~~~
int queue::empty()
{
if(rear < front)
return 1;
return 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~
void queue::addQ(adjList *x)
{
items[++rear] = x;
}
//~~~~~~~~~~~~~~~~~~~~~~
adjList* queue::qremove()
{
return items[front++];
}
//~~~~~~~~~~~~~~~~~~~~~~~~
nodeList::nodeList()
{
start = NULL; //walk around nodes and can access to adjLists
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nodeList::~nodeList()
{
nodeType *p;
while(start != NULL)
{
p = start;
start = start -> nextNode;
delete p;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int stack::empty()
{
if(myTop == -1)
return 1;
else
return 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stack::stack()
{
myTop = -1;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int** nodeList::makeGraph()
{
nodeType *np, *eofnp = NULL;
adjList *ap, *eofap = NULL;
char data;
int num, i = 0;
int we;
while(1)
{
int counter=0;
cout << "Enter data of a node:";
data = _getche(); //getche(); get a character not a string
cout << "\n";
if(data == '\r')
break;
else {
node[++i] = data;
np = new nodeType;
counter++;
np -> info = data;
np -> listPtr = NULL;
np -> nextNode = NULL;
if(start == NULL)
start = eofnp = np;
else {
eofnp -> nextNode = np;
eofnp = np;
}
eofap = NULL;
while(1)
{
cout << "Enter adjacy list node( -1 to end):";
cin >> num;
if(num == -1)
break;
ap = new adjList;
ap -> info = num;
ap -> next = NULL;
if(eofap == NULL)
{
eofnp -> listPtr = ap;
eofap = ap;
}
else {
eofap -> next = ap;
eofap = ap;
}//end of else
// Ask weight of adjacy
cout << "Enter weight:";
cin>>we;
ap->weight=we;
} //end of inner while
}//end of else
}//end of while
//##################--->>> MAKING WEIGHT MATRIX <<<<------###############
int j=0;
nodeType *nodep;
adjList *adjp;
int **copy = new int*[M];
while(nodep!=NULL)
{
for(int i=0;;i++)
{
copy[i] = new int[M];
adjp=nodep->listPtr;
// for(int j=0;;j++)
//{
j=0;
while(adjp !=NULL)
{
copy[i][j]=adjp->weight;
adjp=adjp->next;
j++;
}
nodep=nodep->nextNode;
}
// }
}
return copy;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void nodeList::print()
{
nodeType *np;
adjList *ap;
np = start;
cout << "\nAdjacy list is :\n";
while(np != NULL)
{
cout << np -> info << " -> ";
ap = np -> listPtr;
while(ap != NULL)
{
cout << ap -> info << " ";
ap = ap -> next;
}
cout << "\n";
np = np -> nextNode;
}
cout<<"\n\nmeaning of numbers:";
for(int k=1;k<M;k++)
cout<<k<<"="<<node[k]<<" ";
cout<<"\n";
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void nodeList::depth()
{
stack s;
nodeType *h;
adjList *p, *q;
int status[M + 1];//1=un-visited, 2= pending, 3=visited(printed)
for(int i = 1; i <= M; i++)
status[i] = 1;// assume 1 to all of the nodes' status
p = new adjList;
p -> info = 1;
p -> next = start -> listPtr;
s.item[++(s.myTop)] = p; //push to stack
status[p -> info] = 2;
q = p;
cout << "Depth first traversal is:\n";
while(!(s.empty()))
{
p = s.item[(s.myTop) --]; //pop from stack
status[p -> info] = 3;
cout << node[p -> info] << " ";
h = start;
while(h != NULL) // find poped node inorder to push its adj
{
if(node[p -> info] == h -> info)
break;
h = h -> nextNode;
}
q = h -> listPtr;
while(q != NULL)
{
if(status[q -> info] == 1)
{
status[q -> info] = 2;
s.item[++(s.myTop)] = q; //push to stack
}//end of if
q = q -> next;
} //end of while
}//end of while
}//end of depth
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void nodeList::BFS()
{
queue Q;
nodeType *h;
adjList *p, *q;
int status[M + 1];//1=un-visited, 2= pending, 3=visited(printed)
for(int i = 1; i <= M; i++)
status[i] = 1;// assume 1 to all of the nodes' status
p = new adjList;
p -> info = 1;
p -> next = start -> listPtr;
Q.addQ(p);
status[p->info]=2;
q=p;
cout << "\n\n\n(BFS) breadth first traversal is:\n";
while(!(Q.empty()))
{
p=Q.qremove();
status[p->info]=3;
cout<<node[p->info]<<" ";
h=start;
while(h!=NULL)
{
if(node[p->info]==h->info)
break;
h=h->nextNode;
}
q=h->listPtr;
while(q !=NULL)
{
if(status[q->info]==1)
{
status[q->info]=2;
Q.addQ(q);
}
q=q->next;
}
}
}
void nodeList::dijkstra(int w[][M], int s, int t, int *pd)
{
int distance[M], found[M];
int current, i, k, dc;
int smallDist, newDist;
//initialization
for(i = 0; i < M; i++)
{
found[i] = NOMEMBER;
distance[i] = INFINITY;
}
found[s] = MEMBER;
distance[s] = 0;
current = s;
while(current != t)
{
smallDist = INFINITY;
dc = distance[current];
for(i = 0; i < M; i++)
if(found[i] == NOMEMBER)
{
newDist = dc + w[current][i];
if(newDist < distance[i])
distance[i] = newDist;
//determine the smallest distance
if(distance[i] < smallDist)
{
smallDist = distance[i];
k = i;
}
}//end of for, if
found[k] = MEMBER;
current = k;
}//end of while
*pd = distance[t];
}
//void
void ask(void);
int main()
{
int pd,s=0,t=1;
int **x;
nodeList list;
system("cls");
x=list.makeGraph();
list.print();
list.depth();
list.BFS();
list.dijkstra(x,s,t,&pd);
_getch();
void ask();
_getch();
return 0;
}
void ask(void)
{
int s;
cout<<" please chose one of this algorithms to finding the shortest path";
switch(s)
{
case 1 : cout<<"0";
}
}
بعد کامپایل با ارور cannot convert from int** to int[][9] مواجه میشم