Data Structure is the way of storing data in computer’s memory so that it can be used easily and efficiently. There are different data-structures used for the storage of data. It can also be define as a mathematical or logical model of a particular organization of data items. The representation of particular data structure in the main memory of a computer is called as storage structure. For Examples: Array, Stack, Queue, Tree, Graph, etc.
Operations on different Data Structure:
There are different types of operations that can be performed for the manipulation of data in every data structure. Some operations
are explained and illustrated below:
- Traversing: Traversing a Data Structure means to visit the element stored in it. This can be done with any type of DS.
Below is the program to illustrate traversal in an array:Array
// C++ program to traversal in an array#include <iostream>usingnamespacestd;// Driver Codeintmain(){// Initialise arrayintarr[] = { 1, 2, 3, 4 };// size of arrayintN =sizeof(arr) /sizeof(arr[0]);// Traverse the element of arr[]for(inti = 0; i < N; i++) {// Print the elementcout << arr[i] <<' ';}return0;}chevron_rightfilter_noneStack
// C++ program to traversal in an stack#include <bits/stdc++.h>usingnamespacestd;// Function to print the elemen in stackvoidprintStack(stack<int>& St){// Traverse the stackwhile(!St.empty()) {// Print top elementcout << St.top() <<' ';// Pop top elementSt.pop();}}// Driver Codeintmain(){// Initialise stackstack<int> St;// Insert Element in stackSt.push(4);St.push(3);St.push(2);St.push(1);// Print elements in stackprintStack(St);return0;}chevron_rightfilter_noneQueue
// C++ program to traversal// in an queue#include <bits/stdc++.h>usingnamespacestd;// Function to print the// element in queuevoidprintQueue(queue<int>& Q){// Traverse the stackwhile(!Q.empty()) {// Print top elementcout << Q.front() <<' ';// Pop top elementQ.pop();}}// Driver Codeintmain(){// Initialise queuequeue<int> Q;// Insert elementQ.push(1);Q.push(2);Q.push(3);Q.push(4);// Print elementsprintQueue(Q);return0;}chevron_rightfilter_noneLinkedList
// C++ program to traverse the// given linked list#include <bits/stdc++.h>usingnamespacestd;structNode {intdata;Node* next;};// Function that allocates a new// node with given dataNode* newNode(intdata){Node* new_node =newNode;new_node->data = data;new_node->next = NULL;returnnew_node;}// Function to insert a new node// at the end of linked listNode* insertEnd(Node* head,intdata){// If linked list is empty,// Create a new nodeif(head == NULL)returnnewNode(data);// If we have not reached the end// Keep traversing recursivelyelsehead->next = insertEnd(head->next, data);returnhead;}/// Function to traverse given LLvoidtraverse(Node* head){if(head == NULL)return;// If head is not NULL,// print current node and// recur for remaining listcout << head->data <<" ";traverse(head->next);}// Driver Codeintmain(){// Given Linked ListNode* head = NULL;head = insertEnd(head, 1);head = insertEnd(head, 2);head = insertEnd(head, 3);head = insertEnd(head, 4);// Function Call to traverse LLtraverse(head);}chevron_rightfilter_noneOutput:1 2 3 4
- Searching: Searching means to find a particular element in the given data-structure. It is considered as successful when the required element is found. Searching is the operation which we can performed on data-structures like array, linked-list, tree, graph, etc.
Below is the program to illustrate searching an element in an array:
Array
// C++ program to searching in an array#include <iostream>usingnamespacestd;// Function that finds element K in the// arrayvoidfindElement(intarr[],intN,intK){// Traverse the element of arr[]// to find element Kfor(inti = 0; i < N; i++) {// If Element is present then// print the index and returnif(arr[i] == K) {cout <<"Element found!";return;}}cout <<"Element Not found!";}// Driver Codeintmain(){// Initialise arrayintarr[] = { 1, 2, 3, 4 };// Element to be foundintK = 3;// size of arrayintN =sizeof(arr) /sizeof(arr[0]);// Function CallfindElement(arr, N, K);return0;}chevron_rightfilter_noneStack
// C++ program to find element in stack#include <bits/stdc++.h>usingnamespacestd;// Function to find element in stackvoidfindElement(stack<int>& St,intK){// Traverse the stackwhile(!St.empty()) {// Check if top is Kif(St.top() == K) {cout <<"Element found!";return;}// Pop top elementSt.pop();}cout <<"Element Not found!";}// Driver Codeintmain(){// Initialise stackstack<int> St;// Insert Element in stackSt.push(4);St.push(3);St.push(2);St.push(1);// Element to be foundintK = 3;// Function CallfindElement(St, K);return0;}chevron_rightfilter_noneQueue
// C++ program to find given element// in an queue#include <bits/stdc++.h>usingnamespacestd;// Function to find element in queuevoidfindElement(queue<int>& Q,intK){// Traverse the stackwhile(!Q.empty()) {// Check if top is Kif(Q.front() == K) {cout <<"Element found!";return;}// Pop top elementQ.pop();}cout <<"Element Not found!";}// Driver Codeintmain(){// Initialise queuequeue<int> Q;// Insert elementQ.push(1);Q.push(2);Q.push(3);Q.push(4);// Element to be foundintK = 3;// Print elementsfindElement(Q, K);return0;}chevron_rightfilter_noneLinkedList
// C++ program to traverse the// given linked list#include <bits/stdc++.h>usingnamespacestd;structNode {intdata;Node* next;};// Function that allocates a new// node with given dataNode* newNode(intdata){Node* new_node =newNode;new_node->data = data;new_node->next = NULL;returnnew_node;}// Function to insert a new node// at the end of linked listNode* insertEnd(Node* head,intdata){// If linked list is empty,// Create a new nodeif(head == NULL)returnnewNode(data);// If we have not reached the end// Keep traversing recursivelyelsehead->next = insertEnd(head->next, data);returnhead;}/// Function to traverse given LLbooltraverse(Node* head,intK){if(head == NULL)returnfalse;// If node with value K is found// return trueif(head->data == K)returntrue;returntraverse(head->next, K);}// Driver Codeintmain(){// Given Linked ListNode* head = NULL;head = insertEnd(head, 1);head = insertEnd(head, 2);head = insertEnd(head, 3);head = insertEnd(head, 4);// Element to be foundintK = 3;// Function Call to traverse LLif(traverse(head, K)) {cout <<"Element found!";}else{cout <<"Element Not found!";}}chevron_rightfilter_noneOutput:Element found!
- Insertion: It is the operation which we apply on all the data-structures. Insertion means to add an element in the given data structure. The operation of insertion is successful when the required element is added to the required data-structure. It is unsuccessful in some cases when the size of the data structure is full and when there is no space in the data-structure to add any additional element. The insertion has the same name as an insertion in the data-structure as an array, linked-list, graph, tree. In stack, this operation is called Push. In the queue, this operation is called Enqueue.
Below is the program to illustrate insertion in stack:
Array
// C++ program for insertion in array#include <iostream>usingnamespacestd;// Function to print the array elementvoidprintArray(intarr[],intN){// Traverse the element of arr[]for(inti = 0; i < N; i++) {// Print the elementcout << arr[i] <<' ';}}// Driver Codeintmain(){// Initialise arrayintarr[4];// size of arrayintN = 4;// Insert elements in arrayfor(inti = 1; i < 5; i++) {arr[i - 1] = i;}// Print array elementprintArray(arr, N);return0;}chevron_rightfilter_noneStack
// C++ program for insertion in array#include <bits/stdc++.h>usingnamespacestd;// Function to print the element in stackvoidprintStack(stack<int>& St){// Traverse the stackwhile(!St.empty()) {// Print top elementcout << St.top() <<' ';// Pop top elementSt.pop();}}// Driver Codeintmain(){// Initialise stackstack<int> St;// Insert Element in stackSt.push(4);St.push(3);St.push(2);St.push(1);// Print elements in stackprintStack(St);return0;}chevron_rightfilter_noneQueue
// C++ program for insertion in queue#include <bits/stdc++.h>usingnamespacestd;// Function to print the// element in queuevoidprintQueue(queue<int>& Q){// Traverse the stackwhile(!Q.empty()) {// Print top elementcout << Q.front() <<' ';// Pop top elementQ.pop();}}// Driver Codeintmain(){// Initialise queuequeue<int> Q;// Insert elementQ.push(1);Q.push(2);Q.push(3);Q.push(4);// Print elementsprintQueue(Q);return0;}chevron_rightfilter_noneLinkedList
// C++ program for insertion in LL#include <bits/stdc++.h>usingnamespacestd;structNode {intdata;Node* next;};// Function that allocates a new// node with given dataNode* newNode(intdata){Node* new_node =newNode;new_node->data = data;new_node->next = NULL;returnnew_node;}// Function to insert a new node// at the end of linked listNode* insertEnd(Node* head,intdata){// If linked list is empty,// Create a new nodeif(head == NULL)returnnewNode(data);// If we have not reached the end// Keep traversing recursivelyelsehead->next = insertEnd(head->next, data);returnhead;}/// Function to traverse given LLvoidtraverse(Node* head){if(head == NULL)return;// If head is not NULL,// print current node and// recur for remaining listcout << head->data <<" ";traverse(head->next);}// Driver Codeintmain(){// Given Linked ListNode* head = NULL;head = insertEnd(head, 1);head = insertEnd(head, 2);head = insertEnd(head, 3);head = insertEnd(head, 4);// Function Call to traverse LLtraverse(head);}chevron_rightfilter_noneOutput:1 2 3 4
- Deletion: It is the operation which we apply on all the data-structures. Deletion means to delete an element in the given data structure. The operation of deletion is successful when the required element is deleted from the data structure. The deletion has the same name as a deletion in the data-structure as an array, linked-list, graph, tree, etc. In stack, this operation is called Pop. In Queue this operation is called Dequeue.
Below is the program to illustrate dequeue in Queue:
Stack
// C++ program for insertion in array#include <bits/stdc++.h>usingnamespacestd;// Function to print the element in stackvoidprintStack(stack<int> St){// Traverse the stackwhile(!St.empty()) {// Print top elementcout << St.top() <<' ';// Pop top elementSt.pop();}}// Driver Codeintmain(){// Initialise stackstack<int> St;// Insert Element in stackSt.push(4);St.push(3);St.push(2);St.push(1);// Print elements before pop// operation on stackprintStack(St);cout << endl;// Pop the top elementSt.pop();// Print elements after pop// operation on stackprintStack(St);return0;}chevron_rightfilter_noneQueue
// C++ program to illustrate dequeue// in queue#include <bits/stdc++.h>usingnamespacestd;// Function to print the element// of the queuevoidprintQueue(queue<int> myqueue){// Traverse the queue and print// element at the front of queuewhile(!myqueue.empty()) {// Print the first elementcout << myqueue.front() <<' ';// Dequeue the element from the// front of the queuemyqueue.pop();}}// Driver Codeintmain(){// Declare a queuequeue<int> myqueue;// Insert element in queue from// 0 to 5for(inti = 1; i < 5; i++) {// Insert element at the// front of the queuemyqueue.push(i);}// Print element beforepop// from queueprintQueue(myqueue);cout << endl;// Pop the front elementmyqueue.pop();// Print element after pop// from queueprintQueue(myqueue);return0;}chevron_rightfilter_noneLinkedList
// C++ program for insertion in LL#include <bits/stdc++.h>usingnamespacestd;structNode {intdata;Node* next;};// Function that allocates a new// node with given dataNode* newNode(intdata){Node* new_node =newNode;new_node->data = data;new_node->next = NULL;returnnew_node;}// Function to insert a new node// at the end of linked listNode* insertEnd(Node* head,intdata){// If linked list is empty,// Create a new nodeif(head == NULL)returnnewNode(data);// If we have not reached the end// Keep traversing recursivelyelsehead->next = insertEnd(head->next, data);returnhead;}/// Function to traverse given LLvoidtraverse(Node* head){if(head == NULL)return;// If head is not NULL,// print current node and// recur for remaining listcout << head->data <<" ";traverse(head->next);}// Driver Codeintmain(){// Given Linked ListNode* head = NULL;head = insertEnd(head, 1);head = insertEnd(head, 2);head = insertEnd(head, 3);head = insertEnd(head, 4);// Print before deleting the first// element from LLtraverse(head);// Move head pointer to forward// to remove the first element// If LL has more than 1 elementif(head->next != NULL) {head = head->next;}else{head = NULL;}cout << endl;// Print after deleting the first// element from LLtraverse(head);}chevron_rightfilter_noneOutput:1 2 3 4 2 3 4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

