Data types vs data structure
What is a data type
The two things we must know about the data types are:
- It defines a certain domain of values.
- It defines operations allowed on those values.
For example:
If the data is of int (integer) type
Then it takes only integer values
Operations that can be performed on the data type are addition, subtraction, multiplication, bitwise operations, etc.
If the data is of float (floating) type
Then it takes only floating type values.
Operations that can be performed on the data type are addition, subtraction, multiplication, division, etc. (bitwise and % operations are not allowed).
So, it is cleared from the above examples that data type does not only define a certain domain of values but also defines operations that can be performed on those values.
User-defined data type
In contrast to primitive data types, there is a concept of user-defined data types. The values and the operations that can be performed on the primitive data types are not specified by the language itself, but it is specified by the user only. The examples of user-defined data types are structure, union, and enumeration. By using the structures, we can define our own type by combining other primitive data types. Let's understand through an example.
struct point
{
int x;
int y;
}
In the above code, we have created a user-defined data type named 'point' that is made by combining two primitive data types of integer type named 'x' and 'y'.
Abstract data types
Abstract data types are like user-defined data types, which define the operations on the values using functions without specifying what is inside the function and how the operations are performed.
For example:
Stack ADT: Here, the stack consists of elements of the same type arranged in sequential order. The following are the operations that can be performed on the stack are:
- initialize(): This method initializes the stack to be empty.
- push(): It is a method used for inserting an element into the stack.
- pop(): It is a method used for removing the element from the stack.
- isEmpty(): This method is used to check whether the stack is empty or not.
- isfull(): It checks whether the stack is full or not.
Data type | Data structure |
---|---|
A Data type is one of the forms of a variable to which the value can be assigned of a given type only. This value can be used throughout the program. | A Data structure is a collection of data of different data types. This collection of data can be represented using an object and can be used throughout the program. |
The implementation of data type is known as an abstract implementation. | The implementation of data structure is known as a concrete implementation. |
It can hold value but not data. Therefore, we can say that it is data-less. | It can hold multiple types of data within a single object. |
In case of data type, a value can be assigned directly to the variables. | In the case of data structure, some operations are used to assign the data to the data structure object. |
`There is no problem in the time complexity. | When we deal with a data structure object, time complexity plays an important role. |
The examples of data type are int, float, char. | The examples of data structure are stack, queue, tree, graph. |
No comments:
Post a Comment