×

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

Linear Search

Searching: In the data structure, searching is the process in which an element is searched in a list that satisfies one or more than one condition. Types of searching There are two...

4 minutes read.

Data Structures Algorithms

What is an Algorithm? An algorithm is a sequence of steps used to complete a job or get a desired result. It is similar to programming building elements that let cell...

4 minutes read.

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 minutes read.

Binary search tree traversal in-order pre-order post-order examples

A binary search tree is a type of non-linear tree in which the tree contains at least two nods. It is called binary because of its nature that states bi...

8 minutes read.

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position...

4 minutes read.

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find...

2 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.

Selection Sort

In each iteration of the selection sort algorithm, the smallest item from an unsorted list is chosen and placed at the top of the unsorted list. Algorithm of Selection Sorting In order...

3 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

3 minutes read.

FIFO approach

FIFO is first in first out approach. It is done for the list of elements in data structures where first element will be deleted after another element ia added to it Here,...

6 minutes read.

Flattening a Linked List

In this article, we are going to study about the logic behind the flattening of linked list and we also going to build a code in the C++ to flatten...

3 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.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.

Symmetric binary tree

Implementation // writing a C++ program to check whether a given binary tree is symmetric or not. #include <bits/stdc++.h> using namespace std; // creating a binary tree node. struct __Nod { int ky; struct __Nod *Lft, *Rt; }; //...

4 minutes read.

All About Minimum Cost Spanning Trees in Data Structure

Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system that stores, manages, and optimizes...

7 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Find Number of Minimum Insertion to Make a String Palindrome

You have been given a string. You have to find out the number of minimum insertions to make this string palindrome. The string will contain only lower case alphabets. Note:What is...

4 minutes read.