answersLogoWhite

0

You cannot have recursion within a data structure:

struct foo {

int x;

foo y; // compiler error

};

This has to fail; there is no end point to the recursion. If a foo is member of a foo, then the member foo also requires a member foo, and so on to infinity...

If a structure needs to refer to another instance of itself, we can use a member pointer:

struct foo {

int x;

foo* y; // ok

};

A pointer works because all pointers are the same length regardless of the pointer's type (the type being referred to).

Using member pointers like this is fundamental to many data structures, such as linked lists:

struct node {

int data;

node* prev; // previous node in the sequence (may be NULL)

node* next; // next node in the sequence (may be NULL)

};

User Avatar

Wiki User

7y ago

Still curious? Ask our experts.

Chat with our AI personalities

RossRoss
Every question is just a happy little opportunity.
Chat with Ross
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa

Add your answer:

Earn +20 pts
Q: What is the recursive solution in data structure?
Write your answer...
Submit
Still have questions?
magnify glass
imp