Hi all, I have a c++ program i'm working on focusing on pointers in a circular linked list.

I need to have the user specify the position of the "dog" in the line (the program is a mock dog show). Then a function checks for the specified position and returns a pointer to it. however, I am stuck on where to go after I return the pointer.

struct Nodetype
{
int id;
char name[20];
char breed[50];
int position;
Nodeptr Left, Right;
int numnodes;
};


void Reposition(Nodeptr Head)
{       
        int pos;
	bool Empty(Nodeptr List);
        
        do{
        cout<<"Enter the position of the dog you wish to move: ";	
        cin>>pos;
        cin.ignore();

   	Nodeptr Search2(Nodeptr List, int pos);
        }while(
        

   		//This is where I want to access p but don't know how.
                //I need to reposition it so i need to access its Right and Left
   )
   		return;	
}


Nodeptr Search2(Nodeptr Head, int pos)
{
  int found = 0;
  Nodeptr p;
  bool Empty(Nodeptr List);
  if (Empty(Head))
     return NULL;
  else
     {
       p = Head->Right;
       do
	 {
	   if (pos == p->position)
	      found = 1;
	   else
	      p = p->Right;
		
	  }while (!found &&(p != Head));
       if (p != Head)
	  return p;
       else
	  return NULL;
     }
}

I have tried to use the returned pointer, p, in the reposition() function. Maybe I don't know what exactly i am returning????


Ideas or any help would be awesome thanks!

eric

Recommended Answers

All 4 Replies

//This is where I want to access p but don't know how.

The variable named 'p' does not exist anywhere within the scope of your Reposition() function.

Either pass it in from elsewhere or declare a local var named 'p'.. if this is what you desire.

So far, a variable named 'p' only exists as a local variable inside of the Search2() function.

The variable named 'p' does not exist anywhere within the scope of your Reposition() function.

Either pass it in from elsewhere or declare a local var named 'p'.. if this is what you desire.

Thanks for the speedy reply. I have tried defining it in the reposition() func as follows, but when the program gets to the point where it executes the line "cout<<p->Right->name;", i get a crash.

void Reposition(Nodeptr Head)
{       
        int pos;
        Nodeptr p;
     
	bool Empty(Nodeptr List);
        
        cout<<"Enter the position of the dog you wish to move: ";	
        cin>>pos;
        cin.ignore();
        //This function searches for a dog based upon position in the line.
  	//It returns a pointer to the node (dog) if the position exists and
  	//NULL if it does not.  See below for the actual function.

   	Nodeptr Search2(Nodeptr List, int pos);

        cout<<p->Right->name;
        system("PAUSE");
   	return;
}

i can't use the pointer p and I'm confused as to why? any other insights?

eric

Since you are declaring 'p' locally from within the function, it will be destroyed with the function.. just as any locally declared function variable.

Additionally, you declare 'p' and it is never assigned anything. Later in your function you attempt to dereferrence a NULL pointer.

So the answer is, to declare a gobal variable named 'p' inside of int main() that you can pass into all your functions.

think i fixed it with a spin off of your insight...

void Reposition(Nodeptr Head)
{       
        int pos;
        Nodeptr p;
	bool Empty(Nodeptr List);
		
	Nodeptr Search2(Nodeptr List, int pos);
        
        cout<<"Enter the position of the dog you wish to move: ";	
        cin>>pos;
        cin.ignore();

   	p = Search2(Head, pos);
        
        cout<<p->name;
        system("PAUSE");
   
   	return;	
}

Nodeptr Search2(Nodeptr Head, int pos)
{
  int found = 0;
  Nodeptr p;
  bool Empty(Nodeptr List);
  if (Empty(Head))
     return NULL;
  else
     {
       p = Head->Right;
       do
	 {
	   if (pos == p->position)
	      found = 1;
	   else
	      p = p->Right;
		
	  }while (!found &&(p != Head));
       if (p != Head)
	  return p;
       else
	  return NULL;
     }
}

working like a charm, thanks!!!!!!!!!!

eric

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.