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,
- Compile-time
- 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.