Home » Primitive vs non-primitive data structure

Primitive vs non-primitive data structure

by Online Tutorials Library

Primitive vs non-primitive data structure

Data structure means organizing the data in the memory. The data can be organized in two ways either linear or non-linear way.

There are two types of data structure available for the programming purpose:

  • Primitive data structure
  • Non-primitive data structure

Primitive data structure is a fundamental type of data structure that stores the data of only one type whereas the non-primitive data structure is a type of data structure which is a user-defined that stores the data of different types in a single entity.

Primitive vs non-primitive data structure

In the above image, we can observe the classification of the data structure. The data structure is classified into two types, i.e., primitive and non-primitive data structure. In the case of primitive data structure, it contains fundamental data types such as integer, float, character, pointer, and these fundamental data types can hold a single type of value. For example, integer variable can hold integer type of value, float variable can hold floating type of value, character variable can hold character type of value whereas the pointer variable can hold pointer type of value.

In the case of non-primitive data structure, it is categorized into two parts such as linear data structure and non-linear data structure. Linear data structure is a sequential type of data structure, and here sequential means that all the elements in the memory are stored in a sequential manner; for example, element stored after the second element would be the third element, the element stored after the third element would be the fourth element and so on. We have different linear data structures holding the sequential values such as Array, Linked list, Stack, Queue.

Non-linear data structure is a kind of random type of data structure. The non-linear data structures are Tree and Graph.

Let’s understand the differences between the primitive and non-primitive data structure.

Primitive vs non-primitive data structure

Primitive data structure Non-primitive data structure
Primitive data structure is a kind of data structure that stores the data of only one type. Non-primitive data structure is a type of data structure that can store the data of more than one type.
Examples of primitive data structure are integer, character, float. Examples of non-primitive data structure are Array, Linked list, stack.
Primitive data structure will contain some value, i.e., it cannot be NULL. Non-primitive data structure can consist of a NULL value.
The size depends on the type of the data structure. In case of non-primitive data structure, size is not fixed.
It starts with a lowercase character. It starts with an uppercase character.
Primitive data structure can be used to call the methods. Non-primitive data structure cannot be used to call the methods.

Primitive data structure

Primitive data structure is a data structure that can hold a single value in a specific location whereas the non-linear data structure can hold multiple values either in a contiguous location or random locations

The examples of primitive data structure are float, character, integer and pointer. The value to the primitive data structure is provided by the programmer. The following are the four primitive data structures:

  • Integer: The integer data type contains the numeric values. It contains the whole numbers that can be either negative or positive. When the range of integer data type is not large enough then in that case, we can use long.
  • Float: The float is a data type that can hold decimal values. When the precision of decimal value increases then the Double data type is used.
  • Boolean: It is a data type that can hold either a True or a False value. It is mainly used for checking the condition.
  • Character: It is a data type that can hold a single character value both uppercase and lowercase such as ‘A’ or ‘a’.

Non-primitive data structure

The non-primitive data structure is a kind of data structure that can hold multiple values either in a contiguous or random location. The non-primitive data types are defined by the programmer. The non-primitive data structure is further classified into two categories, i.e., linear and non-linear data structure.

In case of linear data structure, the data is stored in a sequence, i.e., one data after another data. When we access the data from the linear data structure, we just need to start from one place and will find other data in a sequence.

The following are the types of linear data structure:

  • Array: An array is a data structure that can hold the elements of same type. It cannot contain the elements of different types like integer with character. The commonly used operation in an array is insertion, deletion, traversing, searching.

For example:

int a[6] = {1,2,3,4,5,6};

The above example is an array that contains the integer type elements stored in a contiguous manner.

  • String: String is defined as an array of characters. The difference between the character array and string is that the string data structure terminates with a ‘NULL’ character, and it is denoted as a ‘

You may also like