Nested Structures ADT
Nested Structures ADT
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;
1. e1.doj.dd
2. e1.doj.mm
3. e1.doj.yyyy
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
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.
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.
#include <stdio.h>
struct Scope {
// Anonymous union
union {
char alpha;
int num;
};
};
int main()
struct Scope x, y;
x.num = 65;
y.alpha = 'A';
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.