return Type

Started by
24 comments, last by Anri 18 years, 6 months ago
Quote:Original post by EvilNando
Im sorry not to help with anything .. but I just wanted to say that I never heard of nodes and Im interested in knowing what they are

can u direct me to any tutorials bout this?

thanks a lot

Hi
Read about linked list and trees ( like binary tree )

Here at GameDev.net in articles check out Subcategory: Data Structures

http://www.gamedev.net/reference/list.asp?categoryid=25#266


and about linked list for example
http://www.google.pl/search?hl=pl&q=linked+list&btnG=Szukaj+w+Google&lr=

Cheers

[edit]
This link is even better [smile]
http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/ds_ToC.html
[/edit]

[Edited by - Estor on September 15, 2005 6:50:43 AM]
"The Gods Made Heavy Metal And They Saw That It Was Good They Said To Play It Louder Than Hell We Promised That We WouldWhen Losers Say Its Over With You Know That It's A Lie The Gods Made Heavy Metal And It's Never Gonna Die"THE GODS MADE HEAVY METAL/by ManOwaR
Advertisement
Quote:Original post by Estor
Quote:Original post by Ksingh30
ok I think I wasnt clear before. Its a function thats either gonna return a character, or a pointer to a node, which is defined as follows:
struct node
{
char info;
struct node* next;
};

this problem is keeping me back, I cant go on until I get this figured out, and the assignment is due tommorow, if could just get this the rest wont be hard.


Ooook... [looksaround]

*** Source Snippet Removed ***

In this case you can return pointer to retVal becase it's static, but be aware that if someone will do something like this:

node* n1 = someFunc();
node* n2 = someFunc();

then n1 and n2 will have exacly this same values.

You can try this:

*** Source Snippet Removed ***

Bat you must remeber to delete returned value:

node* n1 = someFunc();
/* use n1 */
delete n1;


I assume you are expecting something like this:
node* someFunc( /* your params */ );
char* someFunc( /* your params */ );

but like ricekrispyw wrote

Quote:Original post by ricekrispyw
1) You cannot have an overloaded function that differs only in return type


So just return node struct, and use next field to check is it char or next node.
If next is != NULL then you have node, else info has important char.

Hope this helps [smile]

ok i think I need to be more clear.
this is what I have:

struct Atom
{ char data;
};
struct Node
{
union pointtype
{ struct *Atom AtomPointer;
struct *Node Node pointer;
}point;
struct node* next;
};


ok now I have to write a function which returns the first element in a list. which meants it could be a pointer to Atom or Node.

Why don't you add extra data to your struct like this?

struct Node{    bool    point_is_atom;  // true if it's pointer to atom    union pointtype    {        struct *Atom AtomPointer;        struct *Node Node pointer;    } point;    struct node* next;};


And what exacly is your problem...
1. You don't know how to return this Node struct?
2. Fill data in it

Other thing... is Node something that you got from your teacher or mayby it's your job to design this?

Quote:
ok now I have to write a function which returns the first element in a list. which meants it could be a pointer to Atom or Node.


Sorry but right now you have every thing you need to write such a function?
This http://www.gamedev.net/community/forums/topic.asp?topic_id=344263 + this thread.
If adding simple bool is not an option, than i don't have any idea how your prof want it to be done.
"The Gods Made Heavy Metal And They Saw That It Was Good They Said To Play It Louder Than Hell We Promised That We WouldWhen Losers Say Its Over With You Know That It's A Lie The Gods Made Heavy Metal And It's Never Gonna Die"THE GODS MADE HEAVY METAL/by ManOwaR
no Im not sure if that will work for me. the function is called "car" which comes from LISP, im building a listp interpeter on top of a circular linked list.
car returns the first elements of a list which can be an atom or a sub list.
car(ABC)=A car((AB)ACA)=(AB)
so either my car needs to return pointer to a list, or the Atom

so for example car can be

Atom* car(Node* L)

or

list car(list L)

list is a pointer to a node
That's more or less what I guessed you were doing. And yeah, in that case, the extra bool (or enum, in the general case of more than two "things something can be") is just what you need. The problem is that you need to emulate some kind of dynamic typing (which is what Lisp uses) in a statically-typed language. When a Lisp interpreter runs, it can look at an object and "check" its type; that information is part of the object data.

So as I'm pretty sure I said before (in another thread apparently?), [google] "discriminated union".

Another option would be to use polymorphism, and enable RTTI:

struct ListNode {  virtual ~ListNode(); // needed so we can have RTTI};typedef std::list<ListNode*> myListType;// But I guess you have to write your own list, because profs are silly like that :)struct IntNode : public ListNode {  int value;}struct SublistNode : public ListNode {  myListType theSubList;}// Then when you extract a ListNode from a list:ListNode* x= getSomeNodeFromSomeList();if (IntNode* y = dynamic_cast<IntNode*>(x)) {  int theValue = y->value;} else {  // the dynamic_cast returned a null pointer if we got here,  // which means the list node is not actually an int node.}
I believe what you are looking for is to return a pointtype (or a node) instead of one of the specific types. A bool keeping track of what type a node currently is will become important later on down the line, so make sure you do that too.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
ok I still dont understand.
this is what i have
struct atom{    char value;};struct node{     union pointer	//To determine what this node of the list points to     { 	struct atom  *AtomPointer;	struct node  *ListPointer;     }point;     struct node* next;};typedef node* list;


ok the function I have is suppose to return the first element of a list so if the list=(ABC) it will return A, if the list=((AB)C) it will return (AB)
so this function name is car(from LISP)
so what this funtion needs to do take in a list(defined as a pointer to node) and return the first element in the list.
Ok, so the .point member of the input node (which is the first node of the list) is your union, which holds the thing you want to return. Then you either grab the list pointer or the atom pointer out of that. The grabbing could be done before or after returning from the function. If you do it before, then you would have to return some void* or something, and the calling function won't know which it got back. If you do it after, you need for the calling function to determine which it has, and handle it appropriately.

But in any case, you need to add extra information to the node, in order to do that determination. The node is not capable of remembering on its own whether it holds an atom or a list; you need an extra field whose value indicates which it is. When you go to extract the pointer, you can then check that value, and use the appropriate member of the union.
I know this has nothing to do with the thread, but I couldn't help but post this.

Quote:
You just better start sniffin' your own rank subjugation, Jack, 'cause it's just you against your tattered
libido, the bank, and the mortician forever, man, and it wouldn't be luck if you could get out of life alive. -Guns N' Roses


That is probably the best sig I have seen in a while on this site. Good one.


struct atom
{ char value;
};
struct node
{
union pointer //To determine what this node of the list points to
{
struct atom *AtomPointer;
struct node *ListPointer;

}point;
struct node* next;
};
typedef node* list;


ok in the case above lets say if I have a list L;
how can I test what the point points to?

if(L->point.Atomointer) does not work

This topic is closed to new replies.

Advertisement