Difference between primitive and non primitive data structures

The simple concept of data structures is organizing the data in memory. These organizations in memory can be linear and in a non-linear manner.

There are two different types of data structures one in primitive data type and the other in non-primitive data types.

Difference between primitive and non primitive data structures

Primitive Data Structure Non-Primitive Data Structure
Primitive Data Structure are predefined in the language Non-Primitive Data structure are not defined in language and created by the programmer
Primitive Data structures will have a certain value Non Primitive Data structure can have NULL value
The size depends upon the type of data structure The size of non primitive data structure are not fixed
The primitive data structure starts with lowercase The non primitive data type starts with an uppercase
Can be used to call methods to perform operations Cannot be used.

Primitive Data Structures

Primitive data structures can hold only a single value in one specific location, unlike the non-primitive data structures which can be in a linear and non-linear order. 

Primitive data structures are the predefined types of data that are supported in the programming language. 

These are the fundamental data types of the language. For example float, integer, character, and pointer. 

The primitive data structures can also be termed as the building blocks for data manipulation. The primitive data structures only contain values that are provided by the programmer. The four main data structures found in every language are 

Integer – The integers are used to represent the numeric data. The integer generally stores whole numbers which can be positive and negative. Long can be used in cases where the range of integer data type is not large enough.

Float – The float data type is used to represent the fractional numbers or numbers with decimal figures in the languages. Double can be used to increase the range and precision of decimal figures that float data type holds.

Boolean – The Boolean data type can only take up to two values that are TRUE or FALSE. Mostly, the boolean values are used for conditional testing.

Character – The character data type is used to store single word characters both upper and lower case such as ‘Z’ or ‘z’. 

 

Non-Primitive Data Structures

The Non-Primitive Data Structures are created with the help of the primitive data structures. The Non-primitive data structures are more complicated than primitive data structures but highly useful.

The non-primitive data types are the types that are defined by the programmer. They are further classified into linear and nonlinear data types. 

The linear data types are storing the data inside the memory in a sequence one after. 

When you have to retrieve the data from the linear data structure then you have to just start from one place and you can find other data in a sequence. 

The various types of Linear data structures are – 

Array – An array data structure can hold a fixed number of primitive data structures (such as integer, character, etc.). All the elements of an array should be of the same primitive data structure type, an array cannot store separate data types such as integer with character. An array is a commonly used data structure in many algorithms. The basic operation that can be performed on the array is insertion, deletion, searching, updating, and traversing an array. 

Array representation

 int array [10] = {1,2,3,4,5,6,7,8,9,10};

The above array example stores a similar primitive data structure i.e. integer in a contiguous manner.

String – The string data structure can be defined as an array of characters. The main difference between the character array and the string data structure is that string data structures terminate with a special character called NULL denoted as ‘\0’. The common operations performed on the string data structure is concatenation, copying, replacing, counting, and searching.

String representation

char string_name[100] = "Hello"; 

The length of the string is 6, as the last index stores the NULL character ie. ‘\0’ which helps the compiler to understand the termination of the string.

Character Array Representation

char string_name[100] = {'H','e','l','l','o'};

In this case, the length is 5 as no null character is required in the character array to denote termination.

Stack – The stack data structure simulate the behavior of a real world stack (eg. plate stack, cards stack). The various operations that can be performed on a stack is from top end only like adding (PUSH) or removing (POP) an element from the top. The stack data structure follows LIFO (Last In First Out) structure. The stack data structure can be implemented with the help of an array or linked list.

Stack Representation- 

int peek(){
return stack [top];
}

int isfull(){
if(top==SIZE)
return 1;
else 
return 0;
}

int isempty(){
if(top==-1)
return 1;
else 
return 0;
}

These are the three functions in a stack with the help of an array to demonstrate the checking of the top element in the stack, checking if the stack is full and checking if the stack is empty.

Queue – The queue data structure can be implemented with the help of Array. The queue data structure is similar to stack data structure but the elements in the queue can be inserted and removed from the front and rear end of the array. The various operations performed on the queue are enque (add), deque (remove), peek (checking top element), isfull(checking if array is full), isempty (checking if array is empty).

Queue Representation – 

void insert()
{
if (rear+1 == max)
{ 
print ("overflow");
}
else 
{
if (front==-1&&rear=-1)
{
front = 0;
rear =  0;
}
else
{
rear = rear +1;
}
queue[rear]=item;
}
}

The Non-linear data types store them in random order. nonlinear they will have spread inside the memory in various places and can only be traced by the address.

The example of non-linear data structures is graphs and trees, etc.

Various functions can be performed on both the non-primitive and the primitive data structures they are insertion, deletion, updating, searching, and sorting of the elements in the data structure. They can be performed in both types of data structures whether it is a linked list or a tree; these operations are common to both.

The non-primitive data structures are mostly derived from primitive data structures. For example, the stack

Leave a Comment