Abstract data type in data structure

Before knowing about the abstract data type, we should know about the what is a data structure.

What is data structure?

A data structure is a technique of organizing the data so that the data can be utilized efficiently. There are two ways of viewing the data structure:

  • Mathematical/ Logical/ Abstract models/ Views: The data structure is the way of organizing the data that requires some protocols or rules. These rules need to be modeled that come under the logical/abstract model.
  • Implementation: The second part is the implementation part. The rules must be implemented using some programming language.

Why data structure?

The following are the advantages of using the data structure:

  • These are the essential ingredients used for creating fast and powerful algorithms.
  • They help us to manage and organize the data.
  • Data structures make the code cleaner and easier to understand.

What is abstract data type?

An abstract data type is an abstraction of a data structure that provides only the interface to which the data structure must adhere. The interface does not give any specific details about something should be implemented or in what programming language.

In other words, we can say that abstract data types are the entities that are definitions of data and operations but do not have implementation details. In this case, we know the data that we are storing and the operations that can be performed on the data, but we don't know about the implementation details. The reason for not having implementation details is that every programming language has a different implementation strategy for example; a C data structure is implemented using structures while a C++ data structure is implemented using objects and classes.

For example, a List is an abstract data type that is implemented using a dynamic array and linked list. A queue is implemented using linked list-based queue, array-based queue, and stack-based queue. A Map is implemented using Tree map, hash map, or hash table.

Abstract data type model

Before knowing about the abstract data type model, we should know about abstraction and encapsulation.

Abstraction: It is a technique of hiding the internal details from the user and only showing the necessary details to the user.

Encapsulation: It is a technique of combining the data and the member function in a single unit is known as encapsulation.

Abstract data type in data structure

The above figure shows the ADT model. There are two types of models in the ADT model, i.e., the public function and the private function. The ADT model also contains the data structures that we are using in a program. In this model, first encapsulation is performed, i.e., all the data is wrapped in a single unit, i.e., ADT. Then, the abstraction is performed means showing the operations that can be performed on the data structure and what are the data structures that we are using in a program.

Let's understand the abstract data type with a real-world example.

If we consider the smartphone. We look at the high specifications of the smartphone, such as:

  • 4 GB RAM
  • Snapdragon 2.2ghz processor
  • 5 inch LCD screen
  • Dual camera
  • Android 8.0

The above specifications of the smartphone are the data, and we can also perform the following operations on the smartphone:

  • call(): We can call through the smartphone.
  • text(): We can text a message.
  • photo(): We can click a photo.
  • video(): We can also make a video.

The smartphone is an entity whose data or specifications and operations are given above. The abstract/logical view and operations are the abstract or logical views of a smartphone.

The implementation view of the above abstract/logical view is given below:

The above code is the implementation of the specifications and operations that can be performed on the smartphone. The implementation view can differ because the syntax of programming languages is different, but the abstract/logical view of the data structure would remain the same. Therefore, we can say that the abstract/logical view is independent of the implementation view.

Note: We know the operations that can be performed on the predefined data types such as int, float, char, etc., but we don't know the implementation details of the data types. Therefore, we can say that the abstract data type is considered as the hidden box that hides all the internal details of the data type.

Data structure example

Suppose we have an index array of size 4. We have an index location starting from 0, 1, 2, 3. Array is a data structure where the elements are stored in a contiguous location. The memory address of the first element is 1000, second element is 1004, third element is 1008, and the fourth element is 1012. Since it is of integer type so it will occupy 4 bytes and the difference between the addresses of each element is 4 bytes. The values stored in an array are 10, 20, 30 and 40. These values, index positions and the memory addresses are the implementations.

The abstract or logical view of the integer array can be stated as:

  • It stores a set of elements of integer type.
  • It reads the elements by position, i.e., index.
  • It modifies the elements by index
  • It performs sorting

The implementation view of the integer array: