Definition

An array is a collection of elements of the same data type stored in contiguous memory locations.

Purpose

Used to store multiple values of the same type under a single variable name.

General Syntax

data_type array_name[size]; int arr[5];

Example

int numbers[5] = {10, 20, 30, 40, 50}; cout << numbers[2];

Declares an integer array of size 5 and prints the third element (index 2) which is 30.

Definition

Push adds an element to the top of a stack data structure.

Purpose

Used to insert data into a stack following the LIFO (Last In, First Out) principle.

General Syntax

stack.push(value); or void push(int value) { top++; stack[top] = value; }

Example

stack<int> s; s.push(10); s.push(20);

Creates a stack and pushes two integers onto it. 20 is now at the top.

Definition

Pop removes the top element from a stack.

Purpose

Used to retrieve and remove the most recently added element from the stack.

General Syntax

stack.pop();

Example

stack<int> s; s.push(10); s.push(20); s.pop(); // removes 20

After pushing 10 and 20, pop removes the top element (20), leaving 10 at the top.

Definition

Enqueue adds an element to the rear of a queue.

Purpose

Used to insert data into a queue following the FIFO (First In, First Out) principle.

General Syntax

queue.push(value); or rear++; queue[rear] = value;

Example

queue<int> q; q.push(10); q.push(20);

Creates a queue and enqueues two elements. 10 is at the front.

Definition

Dequeue removes the front element from a queue.

Purpose

Used to remove the element that has been in the queue the longest.

General Syntax

queue.pop();

Example

queue<int> q; q.push(10); q.push(20); q.pop(); // removes 10

After enqueuing 10 and 20, dequeue removes the front element (10), leaving 20.

Definition

A linked list node contains data and a pointer to the next node in the sequence.

Purpose

Used to implement dynamic data structures that can grow and shrink at runtime.

General Syntax

struct Node { int data; Node* next; };

Example

struct Node { int data; Node* next; }; Node* head = NULL;

Defines a Node structure with an integer data field and a pointer to the next node. head is initialized to NULL.

Definition

Insertion in BST adds a new node while maintaining the BST property: left child < parent < right child.

Purpose

Used to maintain a sorted structure that supports efficient searching, insertion, and deletion.

General Syntax

Node* insert(Node* root, int key) { if (root == NULL) return new Node(key); if (key < root->data) root->left = insert(root->left, key); else root->right = insert(root->right, key); return root; }

Example

root = insert(root, 50); root = insert(root, 30); root = insert(root, 70);

Inserts 50 as root, then 30 as left child, and 70 as right child.

Definition

Inorder traversal visits the left subtree, then the root, then the right subtree.

Purpose

Used to visit nodes of a binary tree in sorted order (for BST).

General Syntax

void inorder(Node* root) { if (root == NULL) return; inorder(root->left); cout << root->data << " "; inorder(root->right); }

Example

inorder(root); // prints nodes in ascending order

For a BST, inorder traversal prints elements in increasing order.

Definition

Preorder traversal visits the root first, then the left subtree, then the right subtree.

Purpose

Used to create a copy of the tree or to get prefix expression.

General Syntax

void preorder(Node* root) { if (root == NULL) return; cout << root->data << " "; preorder(root->left); preorder(root->right); }

Example

preorder(root);

Prints root first, then recursively visits left and right subtrees.

Definition

Linear search sequentially checks each element in a list until the target is found or the list ends.

Purpose

Used to find an element in an unsorted list or when the list is small.

General Syntax

for (int i = 0; i < n; i++) { if (arr[i] == target) return i; } return -1;

Example

int arr[] = {5, 3, 8, 1}; int target = 8; for (int i = 0; i < 4; i++) { if (arr[i] == target) cout << "Found at " << i; }

Searches for 8 in the array by checking each element one by one until found at index 2.

Definition

Binary search finds an element in a sorted array by repeatedly dividing the search interval in half.

Purpose

Used for efficient searching in sorted data with O(log n) time complexity.

General Syntax

int low = 0, high = n-1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == target) return mid; if (arr[mid] < target) low = mid + 1; else high = mid - 1; }

Example

int arr[] = {1, 3, 5, 7, 9}; int target = 7; // Binary search finds 7 at index 3

Binary search checks the middle element (5), then searches the right half, finding 7 at index 3.

Definition

Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

Purpose

Used for sorting small datasets or for educational purposes to understand sorting algorithms.

General Syntax

for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]); } }

Example

int arr[] = {64, 34, 25, 12}; // After bubble sort: {12, 25, 34, 64}

Bubble sort compares and swaps adjacent elements until the array is sorted in ascending order.

Definition

Insertion in a singly linked list adds a new node at the beginning, end, or a specific position.

Purpose

Used to dynamically add elements to a linked list without resizing.

General Syntax

void insertAtBeginning(Node*& head, int data) { Node* newNode = new Node(); newNode->data = data; newNode->next = head; head = newNode; }

Example

insertAtBeginning(head, 10); insertAtBeginning(head, 20);

Inserts 10 at the beginning, then 20 at the beginning. List becomes: 20 -> 10.

Definition

A circular queue connects the last position back to the first position, forming a circle.

Purpose

Used to efficiently utilize memory in queue implementations by reusing empty spaces.

General Syntax

if ((rear + 1) % size == front) // queue is full queue[rear] = value; rear = (rear + 1) % size;

Example

#define SIZE 5 int queue[SIZE]; int front = -1, rear = -1; // Enqueue: rear = (rear + 1) % SIZE;

In a circular queue, rear wraps around to 0 when it reaches SIZE-1, reusing empty slots.

Definition

A doubly linked list has nodes with pointers to both the next and previous nodes.

Purpose

Used when bidirectional traversal is needed, enabling efficient deletion from both ends.

General Syntax

struct Node { int data; Node* next; Node* prev; };

Example

Node* newNode = new Node(); newNode->data = 10; newNode->next = NULL; newNode->prev = NULL;

Creates a new node with data 10 and sets both next and prev pointers to NULL.

← Back to Data Structures