A data type defines the type of data structure. A data type can be categorized into a primitive data type (for example integer, float, double etc
.) or an abstract data type (for example list, stack, queue
etc.). In this article, we will discuss about Abstract Data Types (ADT).
Takeaways
Abstract data type
in data structure type allows reusability of a code i.e. it makes it convenient for a programmer to write a shorter code.
What is Abstract Data Type in Data Structure?
An Abstract Data Type
in data structure is a kind of a data type whose behavior is defined with the help of some attributes and some functions. Generally, we write these attributes and functions inside a class or a structure so that we can use an object of the class to use that particular abstract data type.
Examples of Abstract Data Types in Data Structure are list
, stack
, queue
etc.
Abstract Data Type Model
List ADT
Lists
are linear data structures in which data is stored in a non – continuous fashion. List consists of data storage boxes called ‘nodes
‘. These nodes are linked to each other i.e. each node consists of the address of some other block. In this way, all the nodes are connected to each other through these links.
You can read more about lists in this article: Linked List Data Structure.
Given below are some of the important operations that are defined in List ADT.
// defining node of the list
class node{
public:
int data; // to store the data
node* next; // to store the address of the next List node
node(int val) // a constructor to initialize the node parameters
{
data=val;
next=NULL;
}
}
class list{
int count=0; // to count the number of nodes in the list
public:
int front(); // returns value of the node present at the front of the list
int back(); // returns value of the node present at the back of the list
void push_front(int val); // creates a pointer with value = val and keeps this pointer to the front of the linked list
void push_back(int val); // creates a pointer with value = val and keeps this pointer to the back of the linked list
void pop_front(); // removes the front node from the list
void pop_back(); // removes the last node from the list
bool empty(); // returns true if list is empty, otherwise returns false
int size(); // returns the number of nodes that are present in the list
};
Stack ADT
Stack is a linear data structure in which data can be only accessed from its top. It only has two operations i.e. push (used to insert data to the stack top) and pop (used to remove data from the stack top).
You can read more about the stack in this article: Stack Data Structure.
Given below are some of the important operations that are defined in Stack ADT
class node{
public:
int data; // to store data in a stack node
node* next; // to store the address of the next node in the stack
node(int val) // a constructor to initialize stack parameters
{
data=val;
next=NULL;
}
};
class stack(){
int count=0; // to count number of nodes in the stack
public:
int top(); // returns value of the node present at the top of the stack
void push(int val); // creates a node with value = val and put it at the stack top
void pop(); // removes node from the top of the stack
bool empty(); // returns true if stack is empty, otherwise returns false
int size(); // returns the number of nodes that are present in the stack
};
Queue ADT
Queue is a linear data structure in which data can be accessed from both of its ends i.e. front and rear. It only has two operations i.e. push (used to insert data to the rear of the queue) and pop (used to remove data from the front of the queue).
You can read more about queues in this article: Queue Data Structure.
Given below are some of the important operations that are defined in Queue ADT.
class node{
public:
int data; // to store data in a stack node
node* next; // to store the address of the next node in the stack
node(int val) // a constructor to initialize stack parameters
{
data=val;
next=NULL;
}
};
class queue{
int count=0; // to count number of nodes in the stack
public:
int front(); // returns value of the node present at the front of the queue
int back(); // returns value of the node present at the back of the queue
void push(int val); // creates a node with value = val and put it at the front of the queue
void pop(); // removes node from the rear of the queue
bool empty(); // returns true if queue is empty, otherwise returns false
int size(); // returns the number of nodes that are present in the queue
};
Abstract Data Type Example
Let us understand an abstract data type with the help of a real world example.
We all know what Alexa is and what actions we can perform using Alexa. Below is an abstract data type written for some of the functions of alexa.
class Alexa{
int battery=100; // a variable used to define percentage of battery present in Alexa
public:
void playSong(string genre); // function to play a song depending on the input genre
void turnLightsOn(); // function to turn on the lights of the house
void turnLightsOff(); // function to turn on the lights of the house
void volumeUp(); // function to increase the volume of the Alexa
void volumeDown(); // function to reduce the volume of the Alexa
int showBattery(); // function that returns the battery left in the Alexa
void turnACon(); // function to turn on the Air Conditioner
void turnACoff(); // function to turn off the Air Conditioner
}
Advantages of Abstract Data Type
- Abstract data type in data structure makes it very easy for us to use the complex data structures along with their complex functions. It follows an object-oriented programming paradigm.
- By using abstract data types, we can also customize any data structure depending on how we plan to use that particular data structure.
- Abstract data type in data structure follows the concept of reusability of a code. This means that we don’t have to write a particular piece of code again and again. We can just create an abstract data type and we can use it by simply calling the functions present in it.
Take your C++ programming skills to new heights with our Data Structures in C++ Course. Don’t miss out!
Conclusion
- An abstract data type in data structure is a kind of a data type whose behavior is defined with the help of some attributes and some functions.
- An abstract data type in data structure can be that of a list data structure, stack data structure and a queue data structure. Several valid operations on a particular data structure are defined in the abstract data type.
- Abstract data types in data structure follow an object – oriented paradigm.