MyCollegeLab Documentation & Transparency

Data Structures Practical Programs – C/C++ Lab Programs with Output

Comprehensive data structures laboratory programs in C and C++ with full source code, expected outputs, and instant online execution.

Last Updated:November 2026
•
Read Time:7 min read
•
Verified for College Computer Labs
DSA Lab

Data Structures Lab Programs — Run Online in C and C++

Data structures practical is a core subject for B.Tech, BCA, MCA, and computer science diploma students. Developing proficiency in array algorithms, dynamic pointer manipulation, linear structures like stacks and queues, and non-linear hierarchies like trees is essential for laboratory grading and technical interviews.

Having verified, working programs with clear expected output is essential for preparing practical files, lab records, and viva assessments. MyCollegeLab provides an instant C and C++ sandbox equipped with dynamic memory allocation (malloc/free), math library support, and interactive standard input testing right inside your browser.

Sorting Algorithms

Bubble Sort, Selection Sort, Insertion Sort, and divide-and-conquer strategies like Merge Sort and Quick Sort with step-by-step element tracking.

Linked Lists

Pointer-based dynamic memory allocation for singly linked lists, doubly linked lists, circular lists, and node insertion/deletion routines.

Stack & Queue

Array and pointer implementations of LIFO stacks, FIFO queues, circular queues, and parenthesis matching applications.

Trees & Graphs

Binary Search Tree (BST) creation, recursive inorder, preorder, postorder traversals, and graph breadth-first/depth-first searches.

Sorting Algorithms

Sorting Algorithm Programs in C

Sorting algorithms reorder elements in ascending or descending sequence. These three programs are fundamental requirements in college DSA lab examinations.

Program 1: Bubble Sort

Repeatedly compares adjacent items and swaps them into correct order over multiple passes.

#include <stdio.h>
void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - i - 1; j++)
            if (arr[j] > arr[j + 1]) {
                int t = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = t;
            }
}
int main() {
    int n;
    scanf("%d", &n);
    int arr[100];
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
    bubbleSort(arr, n);
    printf("Sorted: ");
    for (int i = 0; i < n; i++) printf("%d ", arr[i]);
    return 0;
}
Expected Output (with stdin: 5 \n 64 34 25 12 22):Sorted: 12 22 25 34 64

Program 2: Binary Search

Efficiently searches a sorted array in O(log n) time by repeatedly halving the search space.

#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
    int low = 0, high = n - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (arr[mid] == key) return mid;
        else if (arr[mid] < key) low = mid + 1;
        else high = mid - 1;
    }
    return -1;
}
int main() {
    int n, key;
    scanf("%d", &n);
    int arr[100];
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
    scanf("%d", &key);
    int idx = binarySearch(arr, n, key);
    if (idx != -1) printf("Found at index %d\n", idx);
    else printf("Not found\n");
    return 0;
}
Expected Output (with stdin: 5 \n 10 20 30 40 50 \n 30):Found at index 2

Program 3: Selection Sort

Selects the minimum element from the unsorted sub-array and places it at the current index.

#include <stdio.h>
void selectionSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int minIdx = i;
        for (int j = i + 1; j < n; j++)
            if (arr[j] < arr[minIdx]) minIdx = j;
        int t = arr[i]; arr[i] = arr[minIdx]; arr[minIdx] = t;
    }
}
int main() {
    int n; scanf("%d", &n);
    int arr[100];
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
    selectionSort(arr, n);
    printf("Sorted: ");
    for (int i = 0; i < n; i++) printf("%d ", arr[i]);
    return 0;
}
Expected Output (with stdin: 5 \n 29 10 14 37 13):Sorted: 10 13 14 29 37
Linked Lists

Linked List Programs in C

Linked lists allow dynamic memory growth without contiguous allocation limitations. Here is a singly linked list alongside an array-based stack.

Program 1: Singly Linked List (Create, Insert, Display)

Allocates heap nodes using malloc(), pushes values to the list head, and traverses to display.

#include <stdio.h>
#include <stdlib.h>
struct Node {
    int data;
    struct Node* next;
};
struct Node* insert(struct Node* head, int val) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = val;
    newNode->next = head;
    return newNode;
}
void display(struct Node* head) {
    while (head) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULL\n");
}
int main() {
    struct Node* head = NULL;
    head = insert(head, 30);
    head = insert(head, 20);
    head = insert(head, 10);
    display(head);
    return 0;
}
Expected Output:10 -> 20 -> 30 -> NULL

Program 2: Stack Using Array (Push, Pop, Display)

Implements Last-In-First-Out (LIFO) stack operations with boundary condition guards against overflow and underflow.

#include <stdio.h>
#define MAX 100
int stack[MAX], top = -1;
void push(int x) {
    if (top == MAX - 1) { printf("Stack Overflow\n"); return; }
    stack[++top] = x;
}
int pop() {
    if (top == -1) { printf("Stack Underflow\n"); return -1; }
    return stack[top--];
}
void display() {
    printf("Stack: ");
    for (int i = top; i >= 0; i--) printf("%d ", stack[i]);
    printf("\n");
}
int main() {
    push(10); push(20); push(30);
    display();
    printf("Popped: %d\n", pop());
    display();
    return 0;
}
Expected Output:Stack: 30 20 10
Popped: 30
Stack: 20 10
Stack & Queue

Queue Implementation & Binary Search Tree in C

Queues enforce FIFO ordering for sequential jobs, while Binary Search Trees (BST) organize elements hierarchically for logarithmic lookups.

Program 1: Linear Queue Using Array

Maintains front and rear pointers for enqueue and dequeue operations.

#include <stdio.h>
#define MAX 100
int queue[MAX], front = 0, rear = 0;
void enqueue(int x) {
    if (rear == MAX) { printf("Queue Full\n"); return; }
    queue[rear++] = x;
}
int dequeue() {
    if (front == rear) { printf("Queue Empty\n"); return -1; }
    return queue[front++];
}
void display() {
    printf("Queue: ");
    for (int i = front; i < rear; i++) printf("%d ", queue[i]);
    printf("\n");
}
int main() {
    enqueue(10); enqueue(20); enqueue(30);
    display();
    printf("Dequeued: %d\n", dequeue());
    display();
    return 0;
}
Expected Output:Queue: 10 20 30
Dequeued: 10
Queue: 20 30

Program 2: Binary Search Tree (Insert & Inorder Traversal)

Builds a BST by recursively linking left and right child pointers, then prints values in sorted order via inorder traversal.

#include <stdio.h>
#include <stdlib.h>
struct Node {
    int data;
    struct Node *left, *right;
};
struct Node* newNode(int val) {
    struct Node* n = (struct Node*)malloc(sizeof(struct Node));
    n->data = val; n->left = n->right = NULL;
    return n;
}
struct Node* insert(struct Node* root, int val) {
    if (!root) return newNode(val);
    if (val < root->data) root->left = insert(root->left, val);
    else root->right = insert(root->right, val);
    return root;
}
void inorder(struct Node* root) {
    if (!root) return;
    inorder(root->left);
    printf("%d ", root->data);
    inorder(root->right);
}
int main() {
    struct Node* root = NULL;
    root = insert(root, 50);
    root = insert(root, 30);
    root = insert(root, 70);
    root = insert(root, 20);
    root = insert(root, 40);
    printf("Inorder: ");
    inorder(root);
    return 0;
}
Expected Output:Inorder: 20 30 40 50 70
Frequently Asked Questions

Frequently Asked Questions

Which data structures topics are covered?

Arrays, sorting algorithms (Bubble, Selection, Insertion, Merge Sort), searching (Linear, Binary Search), singly and doubly linked lists, stacks, linear and circular queues, Binary Search Trees (BST), and graph algorithms.

Can I test sorting programs with custom inputs?

Yes, switch to the interactive stdin tab below the editor and enter your custom input array (e.g. array length on line 1 and space-separated integers on line 2) prior to clicking Run.

Are these programs in C or C++?

These programs are implemented in standard C (C99/C11) using pointer and memory allocation conventions (malloc/free), but they are fully compatible with C++ and can be run with either compiler.

Do these programs work for AKTU / VTU / SPPU data structures practicals?

Yes, these programs follow the standard Data Structures and Algorithms (DSA) laboratory syllabus prescribed by major engineering and university boards including AKTU, VTU, SPPU, Anna University, Mumbai University, and others.