Syntax Recap — Data Structures
15 topic(s) — definitions, syntax, and examples.
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
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
Example
Creates a new node with data 10 and sets both next and prev pointers to NULL.