Self Referential Structure
Self Referential Structure
Explain structure.
A structure is a user defined data type, which groups a set
of data types. It is a collection of variables of different type
under single name. A structure provides a convenience to
group of related data types. A structure can contain
variables, pointers, other structures, arrays or pointers. A
structure is defined with the keyword ‘struct’
Ex:
struct employee
{
int empid;
char name[20];
float salary;
};
..
..
struct employee Ram;
Structures:
Structure definition:
struct tag_name
{
data type member1;
data type member2;
…
…
}
Example:
struct library_books
{
char title[20];
char author[15];
int pages;
float price;
};
The keyword struct informs the compiler for holding fields (
title, author, pages and price in the above example). All
these are the members of the structure. Each member can
be of same or different data type.
struct node {
int data1;
char data2;
struct node* link;
};
int main()
{
struct node ob;
return 0;
}
• In the above example ‘link’ is a pointer to a structure of type
‘node’. Hence, the structure ‘node’ is a self-referential
structure with ‘link’ as the referencing pointer.
struct node {
int data1;
int data2;
struct node* link;
};
int main()
{
struct node ob1; // Node1
// Initialization
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
// Initialization
ob2.link = NULL;
ob2.data1 = 30;
ob2.data2 = 40;