Hi,Is someone could help me how is it possible to implement Generalized Linked Lists?
I've implemented it somehow but I'm not sure it works or not. Now I'm going to print the nodes I've implemented.When I want to declare the print function in class Glist with first1 argument,I get this eror: syntax error : identifier 'first1'.I don't know why it couldn't recognize first1 though I've already declared it in class Glist.

class Gnode {
public:
	int tag;
	int flag;
	char* data;
	Gnode* link;
	Gnode* dlink;
};

class Glist {
public:
	Gnode* first1;
	Gnode* first2;
public:
	Glist () {
		first1=new Gnode;
		first2=new Gnode;
		first1->link=NULL;
		first1->dlink=NULL;
		first1->tag=0;
		first1->flag=0;
		first1->data=NULL;
		first2->data=NULL;
		first2->tag=0;
		first2->link=NULL;
		first2->dlink=NULL;
	}
	void Add ();
	void print (first1);
}
};
void Glist::print(Gnode* p){
	if (p->tag==0)
		cout<<p->data;
	else {
		print(p->dlink);
	p=p->link;
	}
}

Recommended Answers

All 10 Replies

>>void print (first1);
Just like arguments to any other function you have to tell it what kind of objectg first1 is -- such as void print (Gnode* first1);

when I'm using Gnode* first1,Is it as same as the one I've declared before? I mean I want to print the nodes recursively from the Gnode* first1 that I initialized in List constructor.

The variable name in the parameter to the print() function is NOT the same as the variable name declared in the class -- the name in the parameter will override (hide) the one declared in the class.

If you want to use the variable declared in the class then do not specify a parameter to print(). Just simply this: void print()

as you see my print function is printing the nodes recursively and my function must have argument.what's your suggestion?

Well, you could start by renaming the argument to avoid confusion (you, not the compiler). void print(Gnode* node); In the class declaration it doesn't really matter what you call it -- the important thing is that you have to give it a data type. Name the variable anything you want.

Thanks but I haven't got it,could you please explain me more?

Read up on how to declare functions. Parmeters have to have a data type as well as a name, something like int foo(int n) the variable n in the parameter has data type of integer. In your program the compiler produced the error because you forgot to add the data type for the parameter.

You coded it correctly in the second code snippet you posted (function implementation code). You just made the omission in the first code snippet (class declaration).

Yes,I know that but my problem is that if I use Gnode* first1 in the class declaration is it as same as the first1 that I declared in class Glist?
I think you said that instead of Gnode* first1 I have to use forexample Gnode* node to start printing from first1.Is that true?

>>if I use Gnode* first1 in the class declaration is it as same as the first1 that I declared in class Glist?

I already told you -- they are NOT the same.

>>I think you said that instead of Gnode* first1 I have to use forexample Gnode* node to start printing from first1.Is that true?
I didn't (or don't think I did) say that. You have to start the recursive process from somewhere outside that print() function, like in main() or from some other function in Gnode class.

int main()
{
   Gnode n;
   n.print(n.first1);
}

Ok,I try to do that,Thanks.

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.