### Array Insertion Example in C Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm Demonstrates the insertion of elements into an integer array in C. It shows the array's state before and after insertion, printing the values at each index. ```c #include int main(){ int LA[3] = {}, i; printf("Array Before Insertion:\n"); for(i = 0; i < 3; i++) printf("LA[%d] = %d \n", i, LA[i]); printf("Inserting Elements.. \n"); printf("The array elements after insertion :\n"); // prints array values for(i = 0; i < 3; i++) { LA[i] = i + 2; printf("LA[%d] = %d \n", i, LA[i]); } return 0; } ``` -------------------------------- ### Sorting Algorithm Output Example Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm This section shows the step-by-step output of a sorting algorithm, illustrating how an array is transformed through comparisons and swaps until it is fully sorted. It details the state of the array after each iteration. ```text Input Array: [1 8 4 6 0 3 5 2 7 9 ] Items compared: [ 1 , 8 ] => not swapped Items compared: [ 8 , 4 ] => swapped [ 4 , 8 ] Items compared: [ 8 , 6 ] => swapped [ 6 , 8 ] Items compared: [ 8 , 0 ] => swapped [ 0 , 8 ] Items compared: [ 8 , 3 ] => swapped [ 3 , 8 ] Items compared: [ 8 , 5 ] => swapped [ 5 , 8 ] Items compared: [ 8 , 2 ] => swapped [ 2 , 8 ] Items compared: [ 8 , 7 ] => swapped [ 7 , 8 ] Items compared: [ 8 , 9 ] => not swapped Iteration 1 #: [1 4 6 0 3 5 2 7 8 9 ] Items compared: [ 1 , 4 ] => not swapped Items compared: [ 4 , 6 ] => not swapped Items compared: [ 6 , 0 ] => swapped [ 0 , 6 ] Items compared: [ 6 , 3 ] => swapped [ 3 , 6 ] Items compared: [ 6 , 5 ] => swapped [ 5 , 6 ] Items compared: [ 6 , 2 ] => swapped [ 2 , 6 ] Items compared: [ 6 , 7 ] => not swapped Items compared: [ 7 , 8 ] => not swapped Iteration 2 #: [1 4 0 3 5 2 6 7 8 9 ] Items compared: [ 1 , 4 ] => not swapped Items compared: [ 4 , 0 ] => swapped [ 0 , 4 ] Items compared: [ 4 , 3 ] => swapped [ 3 , 4 ] Items compared: [ 4 , 5 ] => not swapped Items compared: [ 5 , 2 ] => swapped [ 2 , 5 ] Items compared: [ 5 , 6 ] => not swapped Items compared: [ 6 , 7 ] => not swapped Iteration 3 #: [1 0 3 4 2 5 6 7 8 9 ] Items compared: [ 1 , 0 ] => swapped [ 0 , 1 ] Items compared: [ 1 , 3 ] => not swapped Items compared: [ 3 , 4 ] => not swapped Items compared: [ 4 , 2 ] => swapped [ 2 , 4 ] Items compared: [ 4 , 5 ] => not swapped Items compared: [ 5 , 6 ] => not swapped Iteration 4 #: [0 1 3 2 4 5 6 7 8 9 ] Items compared: [ 0 , 1 ] => not swapped Items compared: [ 1 , 3 ] => not swapped Items compared: [ 3 , 2 ] => swapped [ 2 , 3 ] Items compared: [ 3 , 4 ] => not swapped Items compared: [ 4 , 5 ] => not swapped Iteration 5 #: [0 1 2 3 4 5 6 7 8 9 ] Items compared: [ 0 , 1 ] => not swapped Items compared: [ 1 , 2 ] => not swapped Items compared: [ 2 , 3 ] => not swapped Items compared: [ 3 , 4 ] => not swapped Output Array: [0 1 2 3 4 5 6 7 8 9 ] ``` -------------------------------- ### B+ Tree Output Example Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm This snippet shows the expected output of a B+ Tree after inserting specific values. It demonstrates how the data is organized and presented in a B+ Tree structure. ```text B+ tree: 10 20 30 40 50 ``` -------------------------------- ### Array Insertion Example in C++ Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm Illustrates inserting elements into an integer array using C++. It prints the array's contents before and after the insertion process, showing the updated values. ```cpp #include using namespace std; int main(){ int LA[3] = {}, i; cout << "Array Before Insertion:" << endl; for(i = 0; i < 3; i++) cout << "LA[" << i <<"] = " << LA[i] << endl; //prints garbage values cout << "Inserting elements.." < using namespace std; int main(){ int LA[] = {1,3,5}; int i, n = 3; cout << "The original array elements are :"< q = new LinkedList<>(); q.add(6); q.add(1); q.add(8); q.add(4); q.add(7); System.out.println("The queue is: " + q); } } ``` -------------------------------- ### Traversal Operation in Arrays (C++) Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm A C++ example showcasing array traversal. It initializes an array and uses a loop to print each element. This code illustrates how to iterate over all elements in a C++ array. ```C++ #include using namespace std; int main(){ int LA[] = {1,3,5,7,8}; int item = 10, k = 3, n = 5; int i = 0, j = n; cout << "The original array elements are:\n"; for(i = 0; i #include using namespace std; struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; // display the list void printList(){ struct node *p = head; cout << "\n["; //start from the beginning while(p != NULL) { cout << " " << p->data << " "; p = p->next; } cout << "]"; } //insertion at the beginning void insertatbegin(int data){ //create a link struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; // point it to old first node lk->next = head; //point first to new first node head = lk; } void insertafternode(struct node *list, int data){ struct node *lk = (struct node*) malloc(sizeof(struct node)); lk->data = data; lk->next = list->next; list->next = lk; } int main(){ insertatbegin(12); insertatbegin(22); insertatbegin(30); insertafternode(head->next,44); insertafternode(head->next->next, 50); cout << "Linked List: "; // print list printList(); } ``` -------------------------------- ### C++ Queue Implementation with peek and isFull Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm Presents a C++ implementation of a queue using an array, showcasing the peek operation to inspect the front element and the isFull operation to determine if the queue is full. The insert function is also included. ```cpp #include #include #define MAX 6 int intArray[MAX]; int front = 0; int rear = -1; int itemCount = 0; int peek(){ return intArray[front]; } bool isFull(){ return itemCount == MAX; } void insert(int data){ if(!isFull()) { if(rear == MAX-1) { rear = -1; } intArray[++rear] = data; itemCount++; } } int main(){ int i; /* insert 5 items */ insert(3); insert(5); insert(9); insert(1); insert(12); insert(15); printf("Queue: "); for(i = 0; i < MAX; i++) printf("%d ", intArray[i]); printf("\nElement at front: %d\n",peek()); } ``` -------------------------------- ### Traversal Operation in Arrays (Java) Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm This Java code demonstrates how to traverse an array. It creates an array, assigns values to its elements, and then prints each element. This is a basic example of iterating through array contents in Java. ```Java public class ArrayDemo { public static void main(String []args) { int LA[] = new int[5]; System.out.println("The array elements are: "); for(int i = 0; i < 5; i++) { LA[i] = i + 2; System.out.println("LA[" + i + "] = " + LA[i]); } } } ``` -------------------------------- ### Implement Queue in Java Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm This Java code demonstrates the use of the `java.util.Queue` interface, specifically with a `LinkedList` implementation. It shows how to add elements, remove elements, check the size, and print the queue's contents. ```java import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queue q = new LinkedList<>(); q.add(6); q.add(1); q.add(8); q.add(4); q.add(7); System.out.println("The queue is: " + q); int n = q.remove(); System.out.println("The element deleted is: " + n); System.out.println("Queue after deletion: " + q); int size = q.size(); System.out.println("Size of the queue is: " + size); } } ``` -------------------------------- ### Java BST Insertion, Search, and Print Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm Implements a Binary Search Tree in Java, demonstrating node creation, recursive insertion, iterative search, and a pre-order traversal for printing the tree structure. It includes a main method to build and query a sample BST. ```java import java.util.Scanner; class BSTNode { BSTNode left, right; int data; public BSTNode(int n) { left = null; right = null; data = n; } } public class BST { static BSTNode root; public BST() { root = null; } private BSTNode insert(BSTNode node, int data) { if(node == null) node = new BSTNode(data); else { if(data <= node.data) node.left = insert(node.left, data); else node.right = insert(node.right, data); } return node; } private boolean search(BSTNode r, int val) { boolean found = false; while ((r != null) && !found) { int rval = r.data; if(val < rval) r = r.left; else if (val > rval) r = r.right; else { found = true; break; } found = search(r, val); } return found; } void printTree(BSTNode node, String prefix) { if(node == null) return; printTree(node.left , " " + prefix); System.out.println(prefix + "--" + node.data); printTree(node.right , prefix + " "); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); BST bst = new BST(); root = bst.insert(root, 55); root = bst.insert(root, 20); root = bst.insert(root, 90); root = bst.insert(root, 80); root = bst.insert(root, 50); root = bst.insert(root, 35); root = bst.insert(root, 15); root = bst.insert(root, 65); bst.printTree(root, " "); System.out.println("Element found = " + bst.search(root, 80)); } } ``` -------------------------------- ### B+ Tree Main Function and Example Output in C++ Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm The main function demonstrates the usage of the B+ Tree insertion and traversal functions. It inserts several values and then prints the resulting tree structure. ```C++ int main() { int i, n, t; insert(10); insert(20); insert(30); insert(40); insert(50); cout<<"B+ tree:\n"; traverse(r); } ``` -------------------------------- ### Max Heap Construction Algorithm in C Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm Implements the Max Heap construction algorithm in C. It includes functions for creating a heap, swapping elements, heapifying a subtree, inserting new elements, extracting the maximum element, and printing the heap. The code demonstrates how to build a Max Heap from a given set of input values. ```c #include #include // Structure to represent a heap typedef struct { int* array; // Array to store heap elements int capacity; // Maximum capacity of the heap int size; // Current size of the heap } Heap; // Function to create a new heap Heap* createHeap(int capacity) { Heap* heap = (Heap*)malloc(sizeof(Heap)); heap->array = (int*)malloc(capacity * sizeof(int)); heap->capacity = capacity; heap->size = 0; return heap; } // Function to swap two elements in the heap void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } // Function to heapify a subtree rooted at index i void heapify(Heap* heap, int i) { int largest = i; int left = 2 * i + 1; int right = 2 * i + 2; // Check if the left child is larger than the root if (left < heap->size && heap->array[left] > heap->array[largest]) largest = left; // Check if the right child is larger than the largest so far if (right < heap->size && heap->array[right] > heap->array[largest]) largest = right; // If the largest is not the root, swap the root with the largest if (largest != i) { swap(&heap->array[i], &heap->array[largest]); heapify(heap, largest); } } // Function to insert a new element into the heap void insert(Heap* heap, int value) { if (heap->size == heap->capacity) { printf("Heap is full. Cannot insert more elements.\n"); return; } // Insert the new element at the end int i = heap->size++; heap->array[i] = value; // Fix the heap property if it is violated while (i != 0 && heap->array[(i - 1) / 2] < heap->array[i]) { swap(&heap->array[i], &heap->array[(i - 1) / 2]); i = (i - 1) / 2; } } // Function to extract the maximum element from the heap int extractMax(Heap* heap) { if (heap->size == 0) { printf("Heap is empty. Cannot extract maximum element.\n"); return -1; } // Store the root element int max = heap->array[0]; // Replace the root with the last element heap->array[0] = heap->array[heap->size - 1]; heap->size--; // Heapify the root heapify(heap, 0); return max; } // Function to print the elements of the heap void printHeap(Heap* heap) { printf("Heap elements: "); for (int i = 0; i < heap->size; i++) { printf("%d ", heap->array[i]); } printf("\n"); } // Example usage of the heap int main() { Heap* heap = createHeap(10); insert(heap, 35); insert(heap, 33); insert(heap, 42); insert(heap, 10); insert(heap, 14); insert(heap, 19); insert(heap, 27); insert(heap, 44); insert(heap, 26); insert(heap, 31); printHeap(heap); int max = extractMax(heap); printf("Maximum element: %d\n", max); return 0; } ``` -------------------------------- ### Graph Initialization and Breadth-First Search Example Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm Initializes an adjacency matrix for a graph and performs a Breadth-First Search (BFS) traversal. Assumes MAX, adjMatrix, addVertex, addEdge, and breadthFirstSearch functions are defined elsewhere. ```python if __name__ == "__main__": #set adjacency for i in range(MAX): #matrix to 0 for j in range(MAX): adjMatrix[i][j] = 0 addVertex('S') addVertex('A') addVertex('B') addVertex('C') addVertex('D') addEdge(0, 1) addEdge(0, 2) addEdge(0, 3) addEdge(1, 4) addEdge(2, 4) addEdge(3, 4) print("Breadth First Search: ", end="") breadthFirstSearch() ``` -------------------------------- ### Python Queue Implementation and Operations Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm Demonstrates the implementation of a Queue data structure in Python using a list. It includes methods for adding elements, checking the size, and removing elements from the queue. The example shows how to initialize a queue, add items, and perform deletion operations, along with their output. ```python class Queue: def __init__(self): self.queue = list() def addtoqueue(self,data): # Insert method to add element if data not in self.queue: self.queue.insert(0,data) return True return False def size(self): return len(self.queue) def removefromqueue(self): if len(self.queue)>0: return self.queue.pop() return ("Queue is empty") q = Queue() q.addtoqueue("36") q.addtoqueue("24") q.addtoqueue("48") q.addtoqueue("12") q.addtoqueue("66") print("size of the queue: ",q.size()) print("Element deleted from queue: ",q.removefromqueue()) print("size of the queue after deletion: ",q.size()) ``` -------------------------------- ### Linked List Insertion and Display in Java Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm Demonstrates linked list creation and manipulation in Java, including insertion at the beginning and end, and printing the list. It defines a static inner class for the node and static methods for list operations. The `main` method orchestrates the insertion and display process. ```java public class Linked_List { static class node { int data; node next; node (int value) { data = value; next = null; } } static node head; // display the list static void printList() { node p = head; System.out.print("\n["); //start from the beginning while(p != null) { System.out.print(" " + p.data + " "); p = p->next; } System.out.print("]"); } //insertion at the beginning static void insertatbegin(int data) { //create a link node lk = new node(data); // point it to old first node lk.next = head; //point first to new first node head = lk; } static void insertatend(int data) { //create a link node lk = new node(data); node linkedlist = head; // point it to old first node while(linkedlist.next != null) linkedlist = linkedlist.next; //point first to new first node linkedlist.next = lk; } public static void main(String args[]) { int k=0; insertatbegin(12); insertatbegin(22); insertatbegin(30); insertatend(44); insertatend(50); insertatend(33); System.out.println("Linked List: "); // print list printList(); } } ``` -------------------------------- ### Pre-order Traversal Implementation in Java Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm This Java code implements pre-order traversal for a binary tree. It defines a Node class and a TreeDataStructure class with a pre-order traversal method. The example demonstrates creating a tree and traversing it. ```java class Node { int data; Node leftChild; Node rightChild; public Node(int key) { data = key; leftChild = rightChild = null; } } public class TreeDataStructure { Node root = null; void pre_order_traversal(Node node) { if(node != null) { System.out.print(node.data + " "); pre_order_traversal(node.leftChild); pre_order_traversal(node.rightChild); } } public static void main(String args[]) { TreeDataStructure tree = new TreeDataStructure(); tree.root = new Node(27); tree.root.leftChild = new Node(12); tree.root.rightChild = new Node(3); tree.root.leftChild.leftChild = new Node(44); tree.root.leftChild.rightChild = new Node(17); tree.root.rightChild.leftChild = new Node(56); System.out.println("\nPreorder traversal: "); tree.pre_order_traversal(tree.root); } } ``` -------------------------------- ### Postorder Traversal of Binary Search Tree in Java Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm Implements postorder traversal for a Binary Search Tree in Java. It defines a Node class and a TreeDataStructure class with a postorder_traversal method. The example demonstrates creating a tree and performing the traversal. ```java class Node { int data; Node leftChild; Node rightChild; public Node(int key) { data = key; leftChild = rightChild = null; } } public class TreeDataStructure { Node root = null; void postorder_traversal(Node node) { if(node != null) { postorder_traversal(node.leftChild); postorder_traversal(node.rightChild); System.out.print(node.data + " "); } } public static void main(String args[]) { TreeDataStructure tree = new TreeDataStructure(); tree.root = new Node(27); tree.root.leftChild = new Node(12); tree.root.rightChild = new Node(30); tree.root.leftChild.leftChild = new Node(4); tree.root.leftChild.rightChild = new Node(17); tree.root.rightChild.leftChild = new Node(56); System.out.println("\nPostorder traversal: "); tree.postorder_traversal(tree.root); } ``` -------------------------------- ### Complete Stack Implementation in C Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm This C code provides a complete implementation of a stack data structure, including `push`, `pop`, `peek`, `isEmpty`, and `isFull` operations. It demonstrates inserting elements, retrieving the top element, and checking the stack's status. ```c #include int MAXSIZE = 8; int stack[8]; int top = -1; /* Check if the stack is empty */ int isempty(){ if(top == -1) return 1; else return 0; } /* Check if the stack is full */ int isfull(){ if(top == MAXSIZE) return 1; else return 0; } /* Function to return the topmost element in the stack */ int peek(){ return stack[top]; } /* Function to delete from the stack */ int pop(){ int data; if(!isempty()) { data = stack[top]; top = top - 1; return data; } else { printf("Could not retrieve data, Stack is empty.\n"); } } /* Function to insert into the stack */ int push(int data){ if(!isfull()) { top = top + 1; stack[top] = data; } else { printf("Could not insert data, Stack is full.\n"); } } /* Main function */ int main(){ push(44); push(10); push(62); push(123); push(15); printf("Element at top of the stack: %d\n" ,peek()); printf("Elements: \n"); // print stack data while(!isempty()) { int data = pop(); printf("%d\n",data); } printf("Stack full: %s\n" , isfull() ?"true":"false"); printf("Stack empty: %s\n" , isempty() ?"true":"false"); return 0; } ``` -------------------------------- ### Implement Queue in C Source: https://www.tutorialspoint.com/data_structures_algorithms/dsa_quick_guide.htm This C code implements a circular queue using an array. It includes functions for insertion, deletion, checking if full/empty, peeking at the front element, and getting the size. The implementation handles wrap-around for the circular buffer. ```c #include #include #include #include #define MAX 6 int intArray[MAX]; int front = 0; int rear = -1; int itemCount = 0; int peek(){ return intArray[front]; } bool isEmpty(){ return itemCount == 0; } bool isFull(){ return itemCount == MAX; } int size(){ return itemCount; } void insert(int data){ if(!isFull()) { if(rear == MAX-1) { rear = -1; } intArray[++rear] = data; itemCount++; } } int removeData(){ int data = intArray[front++]; if(front == MAX) { front = 0; } itemCount--; return data; } int main(){ /* insert 5 items */ insert(3); insert(5); insert(9); insert(1); insert(12); // front : 0 // rear : 4 // ------------------ // index : 0 1 2 3 4 // ------------------ // queue : 3 5 9 1 12 insert(15); // front : 0 // rear : 5 // --------------------- // index : 0 1 2 3 4 5 // --------------------- // queue : 3 5 9 1 12 15 if(isFull()) { printf("Queue is full!\n"); } // remove one item int num = removeData(); printf("Element removed: %d\n",num); // front : 1 // rear : 5 // ------------------- // index : 1 2 3 4 5 // ------------------- // queue : 5 9 1 12 15 // insert more items insert(16); // front : 1 // rear : -1 // ---------------------- // index : 0 1 2 3 4 5 // ---------------------- // queue : 16 5 9 1 12 15 // As queue is full, elements will not be inserted. insert(17); insert(18); // ---------------------- // index : 0 1 2 3 4 5 // ---------------------- // queue : 16 5 9 1 12 15 printf("Element at front: %d\n",peek()); printf("----------------------\n"); printf("index : 5 4 3 2 1 0\n"); printf("----------------------\n"); printf("Queue: "); while(!isEmpty()) { int n = removeData(); printf("%d ",n); } } ```