Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced

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.



ADVERTISEMENT
ADVERTISEMENT