×

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define it as a mathematical or logical model of a specific data arrangement. Storage structure refers to a specific data structure's representation in a computer's main memory. Array, Stack, Queue, Tree, Graph, etc. are few examples.

Various operations on different types of Data Structures:

For the purpose of manipulating data in each data structure, many operations can be carried out. Following are explanations and illustrations of some operations:

  • Traversing: Visit an element that is contained in a data structure by traversing it. It does systematic data visits. Any DS type can be used for this.

The program used to demonstrate array traversal is provided below:

C++ Program:

#include <iostream>
using namespace std;


int main ()
{
	int array [] = { 5, 6, 7, 8 };
	int j = sizeof ( array )  / sizeof ( array [0]) ;
	for ( int index = 0; index < j; index++ ) 
	{
		cout << array [ index ] << ' ';
	}
	return 0;
}

The program used to demonstrate stack traversal is provided below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;


void print_Stack( stack<int>& Stc )
{
	while ( !Stc.empty () ) 
	{
		cout << Stc.top () << ' ';
		Stc.pop ();
	}
}
int main ()
{
	stack <int> Stc;
	Stc.push (8);
	Stc.push (7);
	Stc.push (6);
	Stc.push (5);
	print_Stack (Stc);
	return 0;
}

The program used to demonstrate Queue traversal is provided below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;


void print_Queue( queue<int>& Qe )
{


	while (!Qe.empty()) 
	{
		cout << Qe.front() << ' ';
		Qe.pop();
	}
}
int main ()
{
	queue<int> Qe;


	Qe.push (5);
	Qe.push (6);
	Qe.push (7);
	Qe.push (8);


	print_Queue (Qe);
	return 0;
}

The program used to demonstrate LinkedList traversal is provided below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;
struct Node 
{
	int dt;
	Node* next;
};
Node* newNode(int dt)
{
	Node* new_node1 = new Node;
	new_node1->dt = dt;
	new_node1->next = NULL;
	return new_node1;
}
Node* insertEnd( Node* hd, int dt )
{
	if ( hd == NULL )
		return newNode(dt);
	else
		hd->next = insertEnd ( hd->next, dt );
	return hd;
}
void traverse ( Node* hd )
{
	if ( hd == NULL )
		return;
	cout << hd->dt << " ";


	traverse (hd->next);
}
int main()
{
	Node* hd = NULL;
	hd = insertEnd ( hd, 5 );
	hd = insertEnd ( hd, 6 );
	hd = insertEnd ( hd, 7 );
	hd = insertEnd ( hd, 8 );
	traverse (hd);
}

Output:

5 6 7 8
  • Searching: Searching is the process of locating a specific element within a given data structure. When the necessary element is located, the effort is deemed successful. We may execute searches on a variety of data structures, including arrays, linked lists, trees, graphs, etc.

The program used to demonstrate searching an array element is shown below:

C++ Program:

#include <iostream>
using namespace std;


void findElement( int array[], int p, int j )
{
	for (int index = 0; index < p; index++) 
	{
		if ( array[index] == j )
	    {
			cout << "Element detected!";
			return;
		}
	}
	cout << "Element Not detected!";
}
int main ()
{
	int array[] = { 5, 6, 7, 8 };
	int j = 7;
	int p = sizeof (array) / sizeof (array[0]);
	findElement ( array, p, j ) ;
	return 0;
}

The program used to demonstrate searching a stack element is shown below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;


void findanElement(stack<int>& Stc, int j)
{
	while (!Stc.empty()) 
	{
		if (Stc.top() == j)
		{
			cout << "Element detected!";
			return;
		}
		Stc.pop();
	}
	cout << "Element Not detected!";
}
int main ()
{
	stack <int> Stc;
	Stc.push (8);
	Stc.push (7);
	Stc.push (6);
	Stc.push (5);
	int j = 7;
	findanElement(Stc, j);
	return 0;
}

The program used to demonstrate searching a Queue element is shown below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;


void findanElement(queue<int>& Qe, int j)
{
	while (!Qe.empty()) 
	{
		if (Qe.front() == j)
		 {
			cout << "Element detected!";
			return;
		}
		Qe.pop();
	}


	cout << "Element Not detected!";
 }
int main ()
{
	queue<int> Qe;
	Qe.push (5);
	Qe.push (6);
	Qe.push (7);
	Qe.push (8);
	int j = 7;
	findanElement(Qe, j);
	return 0;
}

The program used to demonstrate searching a LinkedList element is shown below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;
struct Node 
{
	int dt;
	Node* next;
};
Node* newNode ( int dt ) 
{
	Node* new_node1 = new Node;
	new_node1->dt = dt;
	new_node1->next = NULL;
	return new_node1;
}
Node* insertEnd ( Node* hd, int dt )
{
	if ( hd == NULL )
		return newNode(dt);
	else
		hd->next = insertEnd(hd->next, dt);
	return hd;
}
bool traverse(Node* hd, int j)
{
	if ( hd == NULL )
		return false;
	if ( hd->dt == j )
		return true;
	return traverse ( hd->next, j );
}
int main ()
{
	Node* hd = NULL;
	hd = insertEnd (hd, 5);
	hd = insertEnd (hd, 6);
	hd = insertEnd (hd, 7);
	hd = insertEnd (hd, 8);
	int j = 7;
	if ( traverse (hd, j) ) 
	{
		cout << "Element detected!";
	}
	else {
		cout << "Element Not detected!";
	}
}

Output:

Element detected!
  • Insertion: We do this operation on all data structures. Insertion simply means adding a new element to the existing data structure. The necessary element must be added to the necessary data-structure for the insertion operation to be successful. When the data structure is too large and there is no place to add any more elements, it can fail in specific circumstances. The insertion is referred to by the same term as an insertion in a data structure such as an array, linked list, graph, or tree. Push is the term for this stack operation. This procedure is known as Enqueue in the queue.

The program used to demonstrate array insertion is shown below:

C++ Program:

#include <iostream>
using namespace std;


void printanArray ( int array [], int num )
{
	for (int index = 0; index < num; index++) 
	{
		cout << array[ index ] << ' ';
	}
}
int main ()
{
	int num =4;
		
	int array [num];	
	for (int index = 1; index <= num; index++)
    {
		array[index-1]=index+4;
	}
	printanArray ( array, num );
	return 0;
}

The program used to demonstrate stack insertion is shown below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;


void printaStack( stack<int>& Stc )
{
	while ( !Stc.empty () ) 
	{
		cout << Stc.top () << ' ';


		Stc.pop ();
	}
}
int main ()
{
	stack<int> Stc;


	Stc.push (8);
	Stc.push (7);
	Stc.push (6);
	Stc.push (5);


	printaStack (Stc);
	return 0;
}

The program used to demonstrate Queue insertion is shown below:

C++ Program:

 #include <bits/stdc++.h>
using namespace std;


void printaQueue( queue<int>& Qe )
{
	while ( !Qe.empty () )
	 {
		cout << Qe.front() << ' ';
		Qe.pop ();
	}
}
int main ()
{
	queue <int> Qe;


	Qe.push (5);
	Qe.push (6);
	Qe.push (7);
	Qe.push (8);


	printaQueue (Qe);
	return 0;
}

The program used to demonstrate LinkedList insertion is shown below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;
struct Node
{
	int dt;
	Node* next;
};
Node* newNode(int dt)
{
	Node* new_node1 = new Node;
	new_node1->dt = dt;
	new_node1->next = NULL;
	return new_node1;
}
Node* insertEnd (Node* hd, int dt)
{
	if (hd == NULL)
		return newNode (dt);
	else
		hd->next = insertEnd (hd->next, dt);
	return hd;
}
void traverse (Node* hd)
{
	if (hd == NULL)
		return;
	cout << hd->dt << " ";


	traverse (hd->next);
}
int main ()
{
	Node* hd = NULL;
	hd = insertEnd (hd, 5);
	hd = insertEnd (hd, 6);
	hd = insertEnd (hd, 7);
	hd = insertEnd (hd, 8);
	traverse (hd);
}

Output:

5 6 7 8
  • Deletion: We do this operation on all data structures. In the provided data structure, deletion refers to removing a particular element. The required element must be removed from the data structure in order for the deletion operation to be successful. In a data structure such an array, linked list, graph, tree, etc., the deletion has the same name as a deletion. Pop is the term for this stack operation. This procedure is known as Dequeue in Queue.

The program used below to demonstrate pop in stack.

C++ Program:

#include <bits/stdc++.h>
using namespace std;


void printaStack( stack<int> Stc )
{
	while ( !Stc.empty () )
	 {
		cout << Stc.top() << ' ';
		Stc.pop();
	}
}
int main ()
{
	stack<int> Stc;


	Stc.push (8);
	Stc.push (7);
	Stc.push (6);
	Stc.push (5);


	printaStack (Stc);
	cout << endl;
	Stc.pop ();
	printaStack (Stc);
	return 0;
}

An example program to demonstrate dequeue in queue is provided below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;


void printaQueue(queue<int> qe)
{
	while (!qe.empty())
	{
		cout << qe.front() << ' ';
		qe.pop();
	}
}
int main()
{
	queue<int> qe;
	for ( int index = 1; index < 5; index++ ) 
	{
		qe.push(index+4);
	}
	printaQueue (qe);


	cout << endl;
	qe.pop ();
	printaQueue (qe);
	return 0;
}

An example program to demonstrate deletion in LinkedList is provided below:

C++ Program:

#include <bits/stdc++.h>
using namespace std;
struct Node 
{
	int dt;
	Node* next;
};
Node* newNode(int dt)
{
	Node* new_node = new Node;
	new_node->dt = dt;
	new_node->next = NULL;
	return new_node;
}
Node* insertEnd(Node* hd, int dt)
{
	if (hd== NULL)
		return newNode(dt);
	else
		hd->next = insertEnd(hd->next, dt);
	return hd;
}
void traverse(Node* hd)
{
	if (hd == NULL)
		return;
	cout << hd->dt << " ";


	traverse(hd->next);
}
int main()
{
	Node* hd = NULL;
	hd = insertEnd(hd, 5);
	hd = insertEnd(hd, 6);
	hd = insertEnd(hd, 7);
	hd = insertEnd(hd, 8);
	traverse(hd);
	if (hd->next != NULL)
	 {
		hd = hd->next;
	}
	else {
		hd = NULL;
	}


	cout << endl;
	traverse(hd);
}

Output:

5 6 7 8
6 7 8

Another Approach:

  • Create: By specifying program elements, reversibly flips their memory. building a data structure is to be done throughout,
  1. Compile-time
  2.  Runtime 

The malloc() application is accessible.

  • Selection: It chooses particular data from the available data. Any specific data can be chosen by adding a condition to the loop.
  • Update: The data in the data structure is updated. By including a condition in the loop, similar to the select approach, you may also update any particular data.
  • Sort: Arranging data in a specific manner. similar to climbing or lowering. To sort data quickly, we can use a variety of sorting methods. Consider the bubble sort, which sorts data in o(n) time. There are numerous algorithms, including rapid sort, insertion sort, merge sort, and selection sort.
  • Merge: It is possible to merge data from two different orders in an ascending or descending order. To combine data, we utilise merge sort.

Related Topics

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 minutes read.

Tree terminology in Data structures

Data structures The storage used to organize and store data is known as a data structure, and it is a method where data can be arranged on a computer to be...

6 minutes read.

Semi-Structured data

In this article, we will discuss the semi-structured data. Data can be defined as the distinct piece of information that is gathered and translated for some purpose. It can be...

5 minutes read.

Finding the Sum of All Paths in a Binary Tree

Implementation // Writing the C++ program to implement the below approach.  #include <bits/stdc++.h> using namespace std; // creating the new tree node structure. struct Tree__nod { int val; Tree__nod *Lft, *Rt; }; // creating a new function that will...

8 minutes read.

Does Overloading Work with Inheritance

This is a question that occasionally comes to many programmers. Who are curious to know more now has a complete explanation and a solution through this tutorial! Inheritance: The functions of...

3 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Find Bridges in a Graph

You have been given a graph. You have to find out the bridges in that graph. Graph may be connected or disconnected. You have to print vertices of particular edge...

4 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.

Quick Sort vs Merge Sort

In this article, we will take an overview of Quick Sort and Merge Sort and then discuss the differences between them. What is Quick Sort? Quick Sort – The idea behind the...

7 minutes read.

Asymptotic Notation

Asymptotic notation is expressions that are used to represent the complexity of algorithms. The complexity of the algorithm is analyzed from two perspectives:  Time complexitySpace complexity Time complexity The time complexity of an algorithm is the...

3 minutes read.

Treap data structure

In this article, we will discuss the treap data structure. The word treap is a combination of 'tree' and 'heap'. So, treap data structure is a combination of a heap...

8 minutes read.

Types of Linked list

Single linked list  A single linked list is a linked list in which all nodes are connected with each other in sequence. Each node of a singly linked list has two...

7 minutes read.

Boruvkas algorithm

This algorithm is used for finding minimum spanning tree from a weighted graph. Like prim’s and kruskal’s algorithm it is also a greedy algorithm. Note:What is the minimum spanning tree?We know...

4 minutes read.

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the...

3 minutes read.

Implementation of Queue

Implementation of queue: We can implement the queue through the array and linked list. An array is the easiest way to implement the queue. When a queue is created with the...

7 minutes read.

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique. Hash table We...

4 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

Given a Binary Tree Swap Nodes at K Height

Implementation // Writing a C++ program that will help us exchange the nodes.  #include<bits/stdc++.h> using namespace std; // Creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a function that will help...

8 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.