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 defined 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. It visits data in a systematic manner. This can be done with any type of DS.Â
Below is the program to illustrate traversal in an array, stack, queue and linkedlist:
Array
// C++ program to traversal in an array#include <iostream>using namespace std;Â
// Driver Codeint main(){    // Initialise array    int arr[] = { 1, 2, 3, 4 };Â
    // size of array    int N = sizeof(arr) / sizeof(arr[0]);Â
    // Traverse the element of arr[]    for (int i = 0; i < N; i++) {Â
        // Print the element        cout << arr[i] << ' ';    }Â
    return 0;} |
Stack
// C++ program to traversal in an stack#include <bits/stdc++.h>using namespace std;Â
// Function to print the element in stackvoid printStack(stack<int>& St){Â
    // Traverse the stack    while (!St.empty()) {Â
        // Print top element        cout << St.top() << ' ';Â
        // Pop top element        St.pop();    }}Â
// Driver Codeint main(){    // Initialise stack    stack<int> St;Â
    // Insert Element in stack    St.push(4);    St.push(3);    St.push(2);    St.push(1);Â
    // Print elements in stack    printStack(St);    return 0;} |
Queue
// C++ program to traversal// in an queue#include <bits/stdc++.h>using namespace std;Â
// Function to print the// element in queuevoid printQueue(queue<int>& Q){    // Traverse the stack    while (!Q.empty()) {Â
        // Print top element        cout << Q.front() << ' ';Â
        // Pop top element        Q.pop();    }}Â
// Driver Codeint main(){    // Initialise queue    queue<int> Q;Â
    // Insert element    Q.push(1);    Q.push(2);    Q.push(3);    Q.push(4);Â
    // Print elements    printQueue(Q);    return 0;} |
LinkedList
// C++ program to traverse the// given linked list#include <bits/stdc++.h>using namespace std;struct Node {Â Â Â Â int data;Â Â Â Â Node* next;};Â
// Function that allocates a new// node with given dataNode* newNode(int data){Â Â Â Â Node* new_node = new Node;Â Â Â Â new_node->data = data;Â Â Â Â new_node->next = NULL;Â Â Â Â return new_node;}Â
// Function to insert a new node// at the end of linked listNode* insertEnd(Node* head, int data){    // If linked list is empty,    // Create a new node    if (head == NULL)        return newNode(data);Â
    // If we have not reached the end    // Keep traversing recursively    else        head->next = insertEnd(head->next, data);    return head;}Â
/// Function to traverse given LLvoid traverse(Node* head){Â Â Â Â if (head == NULL)Â Â Â Â Â Â Â Â return;Â
    // If head is not NULL,    // print current node and    // recur for remaining list    cout << head->data << " ";Â
    traverse(head->next);}Â
// Driver Codeint main(){    // Given Linked List    Node* head = NULL;    head = insertEnd(head, 1);    head = insertEnd(head, 2);    head = insertEnd(head, 3);    head = insertEnd(head, 4);Â
    // Function Call to traverse LL    traverse(head);} |
1 2 3 4
Time Complexity: O(N)
Auxiliary Space: O(1)
Below is the program to illustrate traversal in an array:
C++
// C++ program to traversal in an array#include <bits/stdc++.h>using namespace std;Â
// Driver Codeint main(){    // Initialise array    int arr[] = { 1, 2, 3, 4 };Â
    // size of array    int N = sizeof(arr)/sizeof(arr[0]);Â
    // Traverse the element of arr[]    for (int i = 0; i < N; i++) {Â
        // Print the element        cout << arr[i] << " ";    }    return 0;}Â
// This code is contributed by jana_sayantan. |
Java
// Java program to traversal in an arrayÂ
import java.util.*;Â
class GFG{Â
// Driver Codepublic static void main(String[] args){    // Initialise array    int arr[] = { 1, 2, 3, 4 };Â
    // size of array    int N = arr.length;Â
    // Traverse the element of arr[]    for (int i = 0; i < N; i++) {Â
        // Print the element        System.out.print(arr[i] + " ");    }Â
}}Â
// This code contributed by Rajput-Ji |
Python3
# Python program to traversal in an arrayÂ
# Driver Codeif __name__ == '__main__':       # Initialise array    arr = [ 1, 2, 3, 4 ];Â
    # size of array    N = len(arr);Â
    # Traverse the element of arr    for i in range(N):Â
        # Print element        print(arr[i], end=" ");     # This code is contributed by Rajput-Ji |
C#
// C# program to traversal in an arrayÂ
using System;Â
public class GFG {Â
    // Driver Code    public static void Main(String[] args) {        // Initialise array        int []arr = { 1, 2, 3, 4 };Â
        // size of array        int N = arr.Length;Â
        // Traverse the element of []arr        for (int i = 0; i < N; i++) {Â
            // Print the element            Console.Write(arr[i] + " ");        }Â
    }}Â
Â
Â
// This code contributed by Rajput-Ji |
Javascript
<script>// javascript program to traversal in an array   // Driver Code             // Initialise array        var arr = [ 1, 2, 3, 4 ];Â
        // size of array        var N = arr.length;Â
        // Traverse the element of arr        for (i = 0; i < N; i++) {Â
            // Print the element            document.write(arr[i] + " ");        }Â
// This code is contributed by Rajput-Ji </script> |
1 2 3 4
Time Complexity: O(N)
Auxiliary Space: O(1)
Below is the program to illustrate traversal in a Stack:
C++
#include <iostream>#include <stack>using namespace std;Â
// Function to print the element in stackvoid printStack(stack<int> St){         // Traverse the stack    while (!St.empty()) {                 // Print top element        cout << St.top() <<" ";                 // Pop top element        St.pop();    }}Â
int main() {Â
      // Initialise stack    stack<int> St;         // Insert Element in stack    St.push(4);    St.push(3);    St.push(2);    St.push(1);         // Print elements in stack    printStack(St);           return 0;}Â
// This code is contributed by lokesh. |
Java
// Java program to traversal in an stackÂ
import java.util.*;Â
class GFG{Â
// Function to print the element in stackstatic void printStack(Stack<Integer> St){Â
    // Traverse the stack    while (!St.isEmpty()) {Â
        // Print top element        System.out.print(St.peek() +" ");Â
        // Pop top element        St.pop();    }}Â
// Driver Codepublic static void main(String[] args){    // Initialise stack    Stack<Integer> St = new Stack<>() ;Â
    // Insert Element in stack    St.add(4);    St.add(3);    St.add(2);    St.add(1);Â
    // Print elements in stack    printStack(St);}}Â
// This code contributed by Rajput-Ji |
Python3
# Function to print the element in stackdef print_stack(St):    # Traverse the stack    while St:        # Print top element        print(St.pop(), end=' ')Â
Â
# Test function with sample inputSt = []St.append(4)St.append(3)St.append(2)St.append(1)print_stack(St)# This code is contributed by Potta Lokesh |
C#
// C# program to traversal in an stackusing System;using System.Collections.Generic;Â
public class GFG {Â
  // Function to print the element in stack  static void printStack(Stack<int> St) {Â
    // Traverse the stack    while (St.Count != 0) {Â
      // Print top element      Console.Write(St.Peek() + " ");Â
      // Pop top element      St.Pop();    }  }Â
  // Driver Code  public static void Main(String[] args)   {Â
    // Initialise stack    Stack<int> St = new Stack<int>();Â
    // Insert Element in stack    St.Push(4);    St.Push(3);    St.Push(2);    St.Push(1);Â
    // Print elements in stack    printStack(St);  }}Â
// This code is contributed by Rajput-Ji |
Javascript
<script>// javascript program to traversal in an stack   // Function to print the element in stack    function printStack(St)    {Â
        // Traverse the stack        while (St.length != 0)        {Â
            // Print top element            document.write(St.pop() + " ");Â
        }    }Â
    // Driver Code             // Initialise stack        var St = [];Â
        // Insert Element in stack        St.push(4);        St.push(3);        St.push(2);        St.push(1);Â
        // Print elements in stack        printStack(St);         // This code is contributed by Rajput-Ji</script> |
1 2 3 4
Time Complexity: O(N)
Auxiliary Space: O(1)
- 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, stack, queue and linkedlist:
Array
// C++ program to searching in an array#include <iostream>using namespace std;Â
// Function that finds element K in the// arrayvoid findElement(int arr[], int N, int K){Â
    // Traverse the element of arr[]    // to find element K    for (int i = 0; i < N; i++) {Â
        // If Element is present then        // print the index and return        if (arr[i] == K) {            cout << "Element found!";            return;        }    }Â
    cout << "Element Not found!";}Â
// Driver Codeint main(){    // Initialise array    int arr[] = { 1, 2, 3, 4 };Â
    // Element to be found    int K = 3;Â
    // size of array    int N = sizeof(arr) / sizeof(arr[0]);Â
    // Function Call    findElement(arr, N, K);    return 0;} |
Stack
// C++ program to find element in stack#include <bits/stdc++.h>using namespace std;Â
// Function to find element in stackvoid findElement(stack<int>& St, int K){Â
    // Traverse the stack    while (!St.empty()) {Â
        // Check if top is K        if (St.top() == K) {            cout << "Element found!";            return;        }Â
        // Pop top element        St.pop();    }Â
    cout << "Element Not found!";}Â
// Driver Codeint main(){    // Initialise stack    stack<int> St;Â
    // Insert Element in stack    St.push(4);    St.push(3);    St.push(2);    St.push(1);Â
    // Element to be found    int K = 3;Â
    // Function Call    findElement(St, K);    return 0;} |
Queue
// C++ program to find given element// in an queue#include <bits/stdc++.h>using namespace std;Â
// Function to find element in queuevoid findElement(queue<int>& Q, int K){Â
    // Traverse the stack    while (!Q.empty()) {Â
        // Check if top is K        if (Q.front() == K) {            cout << "Element found!";            return;        }Â
        // Pop top element        Q.pop();    }Â
    cout << "Element Not found!";}Â
// Driver Codeint main(){    // Initialise queue    queue<int> Q;Â
    // Insert element    Q.push(1);    Q.push(2);    Q.push(3);    Q.push(4);Â
    // Element to be found    int K = 3;Â
    // Print elements    findElement(Q, K);    return 0;} |
LinkedList
// C++ program to traverse the// given linked list#include <bits/stdc++.h>using namespace std;struct Node {Â Â Â Â int data;Â Â Â Â Node* next;};Â
// Function that allocates a new// node with given dataNode* newNode(int data){Â Â Â Â Node* new_node = new Node;Â Â Â Â new_node->data = data;Â Â Â Â new_node->next = NULL;Â Â Â Â return new_node;}Â
// Function to insert a new node// at the end of linked listNode* insertEnd(Node* head, int data){    // If linked list is empty,    // Create a new node    if (head == NULL)        return newNode(data);Â
    // If we have not reached the end    // Keep traversing recursively    else        head->next = insertEnd(head->next, data);    return head;}Â
/// Function to traverse given LLbool traverse(Node* head, int K){Â Â Â Â if (head == NULL)Â Â Â Â Â Â Â Â return false;Â
    // If node with value K is found    // return true    if (head->data == K)        return true;Â
    return traverse(head->next, K);}Â
// Driver Codeint main(){    // Given Linked List    Node* head = NULL;    head = insertEnd(head, 1);    head = insertEnd(head, 2);    head = insertEnd(head, 3);    head = insertEnd(head, 4);Â
    // Element to be found    int K = 3;Â
    // Function Call to traverse LL    if (traverse(head, K)) {        cout << "Element found!";    }    else {        cout << "Element Not found!";    }} |
Element found!
Time Complexity: O(N)
Auxiliary Space: O(1)
- 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 array, stack, queue and linkedlist :
Array
// C++ program for insertion in array#include <iostream>using namespace std;Â
// Function to print the array elementvoid printArray(int arr[], int N){Â Â Â Â // Traverse the element of arr[]Â Â Â Â for (int i = 0; i < N; i++) {Â
        // Print the element        cout << arr[i] << ' ';    }}Â
// Driver Codeint main(){    // Initialise array    int arr[4];Â
    // size of array    int N = 4;Â
    // Insert elements in array    for (int i = 1; i < 5; i++) {        arr[i - 1] = i;    }Â
    // Print array element    printArray(arr, N);    return 0;} |
Stack
// C++ program for insertion in array#include <bits/stdc++.h>using namespace std;Â
// Function to print the element in stackvoid printStack(stack<int>& St){Â
    // Traverse the stack    while (!St.empty()) {Â
        // Print top element        cout << St.top() << ' ';Â
        // Pop top element        St.pop();    }}Â
// Driver Codeint main(){    // Initialise stack    stack<int> St;Â
    // Insert Element in stack    St.push(4);    St.push(3);    St.push(2);    St.push(1);Â
    // Print elements in stack    printStack(St);    return 0;} |
Queue
// C++ program for insertion in queue#include <bits/stdc++.h>using namespace std;Â
// Function to print the// element in queuevoid printQueue(queue<int>& Q){    // Traverse the stack    while (!Q.empty()) {Â
        // Print top element        cout << Q.front() << ' ';Â
        // Pop top element        Q.pop();    }}Â
// Driver Codeint main(){    // Initialise queue    queue<int> Q;Â
    // Insert element    Q.push(1);    Q.push(2);    Q.push(3);    Q.push(4);Â
    // Print elements    printQueue(Q);    return 0;} |
LinkedList
// C++ program for insertion in LL#include <bits/stdc++.h>using namespace std;struct Node {Â Â Â Â int data;Â Â Â Â Node* next;};Â
// Function that allocates a new// node with given dataNode* newNode(int data){Â Â Â Â Node* new_node = new Node;Â Â Â Â new_node->data = data;Â Â Â Â new_node->next = NULL;Â Â Â Â return new_node;}Â
// Function to insert a new node// at the end of linked listNode* insertEnd(Node* head, int data){    // If linked list is empty,    // Create a new node    if (head == NULL)        return newNode(data);Â
    // If we have not reached the end    // Keep traversing recursively    else        head->next = insertEnd(head->next, data);    return head;}Â
/// Function to traverse given LLvoid traverse(Node* head){Â Â Â Â if (head == NULL)Â Â Â Â Â Â Â Â return;Â
    // If head is not NULL,    // print current node and    // recur for remaining list    cout << head->data << " ";Â
    traverse(head->next);}Â
// Driver Codeint main(){    // Given Linked List    Node* head = NULL;    head = insertEnd(head, 1);    head = insertEnd(head, 2);    head = insertEnd(head, 3);    head = insertEnd(head, 4);Â
    // Function Call to traverse LL    traverse(head);} |
1 2 3 4
Time Complexity: O(N)
Auxiliary Space: O(1)
- 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 Stack, Queue and Linkedlist:
Stack
// C++ program for insertion in array#include <bits/stdc++.h>using namespace std;Â
// Function to print the element in stackvoid printStack(stack<int> St){    // Traverse the stack    while (!St.empty()) {Â
        // Print top element        cout << St.top() << ' ';Â
        // Pop top element        St.pop();    }}Â
// Driver Codeint main(){    // Initialise stack    stack<int> St;Â
    // Insert Element in stack    St.push(4);    St.push(3);    St.push(2);    St.push(1);Â
    // Print elements before pop    // operation on stack    printStack(St);Â
    cout << endl;Â
    // Pop the top element    St.pop();Â
    // Print elements after pop    // operation on stack    printStack(St);    return 0;} |
Queue
// C++ program to illustrate dequeue// in queue#include <bits/stdc++.h>using namespace std;Â
// Function to print the element// of the queuevoid printQueue(queue<int> myqueue){    // Traverse the queue and print    // element at the front of queue    while (!myqueue.empty()) {Â
        // Print the first element        cout << myqueue.front() << ' ';Â
        // Dequeue the element from the        // front of the queue        myqueue.pop();    }}Â
// Driver Codeint main(){    // Declare a queue    queue<int> myqueue;Â
    // Insert element in queue from    // 0 to 5    for (int i = 1; i < 5; i++) {Â
        // Insert element at the        // front of the queue        myqueue.push(i);    }Â
    // Print element beforepop    // from queue    printQueue(myqueue);Â
    cout << endl;Â
    // Pop the front element    myqueue.pop();Â
    // Print element after pop    // from queue    printQueue(myqueue);    return 0;} |
LinkedList
// C++ program for insertion in LL#include <bits/stdc++.h>using namespace std;struct Node {Â Â Â Â int data;Â Â Â Â Node* next;};Â
// Function that allocates a new// node with given dataNode* newNode(int data){Â Â Â Â Node* new_node = new Node;Â Â Â Â new_node->data = data;Â Â Â Â new_node->next = NULL;Â Â Â Â return new_node;}Â
// Function to insert a new node// at the end of linked listNode* insertEnd(Node* head, int data){    // If linked list is empty,    // Create a new node    if (head == NULL)        return newNode(data);Â
    // If we have not reached the end    // Keep traversing recursively    else        head->next = insertEnd(head->next, data);    return head;}Â
/// Function to traverse given LLvoid traverse(Node* head){Â Â Â Â if (head == NULL)Â Â Â Â Â Â Â Â return;Â
    // If head is not NULL,    // print current node and    // recur for remaining list    cout << head->data << " ";Â
    traverse(head->next);}Â
// Driver Codeint main(){    // Given Linked List    Node* 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 LL    traverse(head);Â
    // Move head pointer to forward    // to remove the first elementÂ
    // If LL has more than 1 element    if (head->next != NULL) {        head = head->next;    }    else {        head = NULL;    }Â
    cout << endl;Â
    // Print after deleting the first    // element from LL    traverse(head);} |
1 2 3 4 2 3 4
Time Complexity: O(N)
Auxiliary Space: O(1)
 Some other method :
Create: –Â
It reserves memory for program elements by declaring them. The creation of data structureÂ
Can be done duringÂ
- Compile-time
- Run-time.Â
You can use malloc() function.
Selection:-
It selects specific data from present data. You can select any specific data by giving condition in loop .
Update
It updates the data in the data structure. You can also update any specific data by giving some condition in loop like select approach.Â
SortÂ
Sorting data in a particular order (ascending or descending).
We can take the help of many sorting algorithms to sort data in less time. Example: bubble sort which takes O(n^2)time to sort data. There are many algorithms present like merge sort, insertion sort, selection sort, quick sort, etc.
Merge
Merging data of two different orders in a specific order may ascend or descend. We use merge sort to merge sort data.
Split DataÂ
Dividing data into different sub-parts to make the process complete in less time.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
