Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
}
else if(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();


}
Posted
Comments
cariolihome 3-Feb-11 19:03pm    
What did You mean when replacing pointers temp1 and temp3 on newly created objects with head pointer ?
grid *temp1=new grid();
grid * temp3= new grid();
temp1=head;
temp3=head;
irtz 4-Feb-11 10:13am    
ihv made temp1 that will point at head but temp3 will move as 5th node son temp1 will also come down but i cannot manage it

1 solution

I would suggest you use a debugger to inspect your variables. The debugger will be your best friend. The earlier you learn how to use it, the more productive you will become. Set a breakpoint, step through your code, and you will see how variables changes, and detect reasons for invalid states.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900