Hi i have have some how made a grid using linked list in c++ instead of input data iam just incrementing size to know whether nodes are made or not but i dont know whether actually grid is made or not because i dont know how to display a grid can anyone tell me a method to display my grid so i should know whether my grid is as it should be.Please also tell if there is mistake and i also want to know how to move a star in my grid and output it on screen i.e user will move it
please run and check it. Thanks
below is my code:-
#include<iostream.h>
//the grid that i have made is of 2 rows and 4 columns
class grid // grid class
{
private:
int size;
grid *east;//this pointer acting as next pointer or right pointer
grid *west;//this pointer acting as previous pointer or left pointer
grid *north;//this pointer acting as up pointer
grid *south;//this pointer acting as down pointer
public:
grid()
{
size=0;
east=west=north=south=NULL;
}
grid(int s)
{
size=s;
east=west=north=south=NULL;
}
void setsize(int s)
{
size=s;
}
int getsize()
{
return size;
}
void setwest(grid *w)
{
west=w;
}
grid *getwest()
{
return west;
}
void seteast(grid *e)
{
east=e;
}
grid *geteast()
{
return east;
}
void setnorth(grid *n)
{
north=n;
}
grid *getnorth()
{
return north;
}
void setsouth(grid *s)
{
south=s;
}
grid *getsouth()
{
return south;
}
};
class gridlist //grid list class
{
private:
grid *head;
grid *current;
grid *lastcurrent;
public:
gridlist()
{
head=NULL;
current=NULL;
lastcurrent=NULL;
}
void create()// creating grid
{grid *temp=new grid();
int i=1;
for(int row=1;row<=2;row++)//loop for row the grid is 2 x 4 2 rows and 4 columns
{
for(int col=1;col<=4;col++)// column loop
{
if(head==NULL)
{
temp->setsize(i);
head=current=lastcurrent=temp;
cout<<current->getsize()<<endl;
}
elseif(i<=4)
{
temp->setsize(i);
lastcurrent=current;
current->seteast(temp);
current=temp;
current->setwest(lastcurrent);
current->seteast(NULL);
cout<<current->getsize()<<endl;
}
if(i>4)// problem here after the fourth node
{/*i have confusion whether after the 4th node the fifth node is connecting with first node or not. 4th node should not have connection
with 5th node i.e 4th node should point to null which is done above similarly 6th node should have connection with node 2 and 5 and so on
this is why i need to make display function to confirm that please help*/
grid *temp1=new grid();
grid * temp3= new grid();
temp1=head;
temp3=head;
current=head;
lastcurrent=head;
temp->setsize(i);
temp3->setsouth(current);
temp3->setnorth(temp);
/*while(temp1->getwest()!=head)
{
lastcurrent=lastcurrent->getwest();
temp1->setsouth(current);
temp1->setsize(i);
}*/
cout<<current->getsize()<<endl;
}
i++;
}
}
}
};
void main()
{
gridlist l;
l.create();
}
I didn't check your implementation of gridlist (because of the lack of indentation).
methods to traverse your grid:
* LRUB: start at upper left.
_store the down pointer.
_go to the right all you can.
_go to the stored down.
_Repeat
* Snake: start at upper left.
_go to the right all you can
_go down
_go left all you can
_go down
_Repeat
* Flood fill: start at any node
_If the node was already visited, end.
_Visit the node
_Flood fill its neighbours.