0% found this document useful (0 votes)
27 views

Nested Structures ADT

The document discusses nested structures in C programming. It provides an example of a nested structure with an Employee structure that contains member variables like id and name, and also contains a nested Date structure with member variables like dd, mm, and yyyy. Members of the nested structure can be accessed using the dot operator, like employee.date.dd. The document also provides examples of declaring, defining, initializing, and printing values of a nested structure.

Uploaded by

kharbsahab2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Nested Structures ADT

The document discusses nested structures in C programming. It provides an example of a nested structure with an Employee structure that contains member variables like id and name, and also contains a nested Date structure with member variables like dd, mm, and yyyy. Members of the nested structure can be accessed using the dot operator, like employee.date.dd. The document also provides examples of declaring, defining, initializing, and printing values of a nested structure.

Uploaded by

kharbsahab2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

The embedded structure enables us to declare the structure inside the structure.

Hence,
it requires less line of codes but it can not be used in multiple data structures. Consider
the following example.

1. struct Employee
2. {
3. int id;
4. char name[20];
5. struct Date
6. {
7. int dd;
8. int mm;
9. int yyyy;
10. }doj;
11. }emp1;

Accessing Nested Structure


We can access the member of the nested structure by
Outer_Structure.Nested_Structure.member as given below:

1. e1.doj.dd
2. e1.doj.mm
3. e1.doj.yyyy

C Nested Structure example


Let's see a simple example of the nested structure in C language.

1. #include <stdio.h>
2. #include <string.h>
3. struct Employee
4. {
5. int id;
6. char name[20];
7. struct Date
8. {
9. int dd;
10. int mm;
11. int yyyy;
12. }doj;
13. }e1;
14. int main( )
15. {
16. //storing employee information
17. e1.id=101;
18. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
19. e1.doj.dd=10;
20. e1.doj.mm=11;
21. e1.doj.yyyy=2014;
22.
23. //printing first employee information
24. printf( "employee id : %d\n", e1.id);
25. printf( "employee name : %s\n", e1.name);
26. printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e
1.doj.yyyy);
27. return 0;
28. }

Output:

employee id : 101
employee name : Sonoo Jaiswal
employee date of joining (dd/mm/yyyy) : 10/11/2014

Passing structure to function


Just like other variables, a structure can also be passed to a function. We may pass the
structure members into the function or pass the structure variable at once. Consider the
following example to pass the structure variable employee to a function display() which is
used to display the details of an employee.

1. #include<stdio.h>
2. struct address
3. {
4. char city[20];
5. int pin;
6. char phone[14];
7. };
8. struct employee
9. {
10. char name[20];
11. struct address add;
12. };
13. void display(struct employee);
14. void main ()
15. {
16. struct employee emp;
17. printf("Enter employee information?\n");
18. scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
19. display(emp);
20. }
21. void display(struct employee emp)
22. {
23. printf("Printing the details....\n");
24. printf("%s %s %d %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
25. }

Self-referential structure plays a very important role in creating other data structures
like Linked list.
o In this type of structure, the object of the same structure points to the same data
structure and refers to the data types of the same structure.
o It can have one or more pointers pointing to the same type of structure as their
member.
o The self-referential structure is widely used in dynamic data structures such
as trees, linked lists, etc.

Why do we require a Self-referential structure?


Self-referential structure plays a vital role in the linked list, trees, graphs, and many more
data structures. By using the structure, we can easily implement these data structures
efficiently. For defining a particular node, the structure plays a very important role. In a
linked list, the structure is used to store the data information and the address of the next
node in the given linked list. Mainly the data part is of integer type, and the link address
part is of pointer type, which holds the address of the next node, so that it can for the
Link ultimately.

As we all know, the role of a linked list is used to store the data elements of the same
type, in which address locations are not at the consecutive difference. In this linear linked
list, the current node holds the address of the next node so that it ultimately forms the
Link between the current node and the next node.
Here in the linked list, we will define a common structure, which contains the data and
address pointer to carry the address of the next node, so to form the multiple nodes, we
from each node using the node structure.

Unlike a static data structure such as an array where the size of the array limits the number
of elements that can easily to inserted into the array, a self-referential structure can
dynamically be expanded or contracted.

Operations like the insertion or deletion of nodes in a self-referential structure involve


straightforward alteration of pointers.

Example 1: Creating a random self-referential structure in the C programming


language.

1. // Dummy Self-referential structure in C language


2. struct node // Creating a node of structure type contains data

3. // part of integer type and a pointer of structure type to


4. // hold the address of the next node
5. {
6. int d1 ; // data 1 of integer type
7. int d2 ; // data 2 of integer type
8. struct node * link ; // Self referential structure link is pointing the same
9. // structure of the type node
10. } ;
11. Int main()
12. {
13. Struct node obj; // Creating an object of the node type, this o
bject is // consists of three parts one is data 1 and
14. // second is of data 2, and third is of link type pointer
15. // carries the address of the next node.
16. Return 0 ;
17. }
Anonymous unions/structures are also known as unnamed unions/structures
as they don’t have names. Since there is no names, direct objects(or variables)
of them are not created and we use them in nested structure or unions.
Definition is just like that of a normal union just without a name or tag. For
example,
// Anonymous union example
union
{
char alpha;
int num;
};

// Anonymous structure example


struct
{
char alpha;
int num;
};
Since there is no variable and no name, we can directly access members. This
accessibility works only inside the scope where the anonymous union is
defined.
Following is a complete working example of anonymous union.

// C Program to demonstrate working of anonymous union

#include <stdio.h>

struct Scope {

// Anonymous union

union {

char alpha;

int num;

};
};

int main()

struct Scope x, y;

x.num = 65;

y.alpha = 'A';

// Note that members of union are accessed directly

printf("y.alpha = %c, x.num = %d", y.alpha, x.num);

return 0;

ADT but before understanding what ADT is let us consider different in-built data
types that are provided to us. Data types such as int, float, double, long, etc.
are considered to be in-built data types and we can perform basic operations
with them such as addition, subtraction, division, multiplication, etc. Now there
might be a situation when we need operations for our user-defined data type
which have to be defined. These operations can be defined only as and when
we require them. So, in order to simplify the process of solving problems, we
can create data structures along with their operations, and such data structures
that are not in-built are known as Abstract Data Type (ADT).
Abstract Data type (ADT) is a type (or class) for objects whose behavior is
defined by a set of values and a set of operations. The definition of ADT only
mentions what operations are to be performed but not how these operations will
be implemented. It does not specify how data will be organized in memory and
what algorithms will be used for implementing the operations. It is called
“abstract” because it gives an implementation-independent view.
The process of providing only the essentials and hiding the details is known as
abstraction.

The user of data type does not need to know how that data type is implemented,
for example, we have been using Primitive values like int, float, char data types
only with the knowledge that these data type can operate and be performed on
without any idea of how they are implemented.
So a user only needs to know what a data type can do, but not how it will be
implemented. Think of ADT as a black box which hides the inner structure and
design of the data type. Now we’ll define three ADTs
namely List ADT, Stack ADT, Queue ADT.

You might also like