You are on page 1of 38

#include <stdio.

h> printf("Num1 and Num2 are equal\n");


}
int main() {
int num1 = 42; for (int i = 0; i < 10; i++) {
int num2 = 69; printf("Iteration %d\n", i);
int sum = num1 + num2; }

printf("The sum of %d and %d is %d\n", // Function calls


num1, num2, sum);
someFunction(num1, 'A');
float result = anotherFunction(3.14);
return 0;
}
// Pointers
int* ptr = &num1;
#include <stdio.h>
*ptr = 99;

// Function prototypes
// Arrays
void someFunction(int arg1, char arg2);
int numbers[5] = {1, 2, 3, 4, 5};
int anotherFunction(float arg3);
char name[] = "John";

// Global variables
// Structures
int globalVar = 42;
struct Person {
char name[50];
int main() {
int age;
// Local variables
};
int num1 = 42;
int num2 = 69;
struct Person person1;
strcpy(person1.name, "Alice");
// Control flow
person1.age = 30;
if (num1 > num2) {
printf("Num1 is greater than Num2\n");
// Memory allocation
} else if (num1 < num2) {
int* dynamicArray = (int*)malloc(10 *
printf("Num2 is greater than Num1\n"); sizeof(int));
} else { free(dynamicArray);
} else if (num1 < num2) { // Else if
statement.
// Preprocessor directives
printf("Num2 is greater than Num1\n");
#define PI 3.14159
} else { // Else statement.
#ifdef DEBUG
printf("Num1 and Num2 are equal\n");
printf("Debug mode is on!\n");
}
#endif

for (int i = 0; i < 10; i++) { // For loop.


return 0;
printf("Iteration %d\n", i);
}
}
#include <stdio.h> // Standard input-output
library for basic I/O functions.
// Function calls
// Function prototypes someFunction(num1, 'A'); // Calling the
function "someFunction" with two arguments.
void someFunction(int arg1, char arg2); //
Declaring a function with parameters but no float result = anotherFunction(3.14); //
return value. Calling the function "anotherFunction" with a
float argument.
int anotherFunction(float arg3); // Declaring a
function with a float parameter and an int
return value.
// Pointers
int* ptr = &num1; // Declaring a pointer to
// Global variables (not recommended, but an integer and assigning the address of num1
hey, you do you) to it.
int globalVar = 42; // A global variable *ptr = 99; // Dereferencing the pointer to
accessible throughout the entire program. modify the value of num1.

int main() { // Arrays


// Local variables (limited to this block of int numbers[5] = {1, 2, 3, 4, 5}; // Declaring
code) and initializing an array of integers.
int num1 = 42; // Declaring and initializing char name[] = "John"; // Declaring and
an integer variable. initializing an array of characters.
int num2 = 69; // Declaring and initializing
another integer variable.
// Structures
struct Person { // Defining a structure called
// Control flow "Person."
if (num1 > num2) { // Simple if statement. char name[50];
printf("Num1 is greater than Num2\n"); int age;
}; int anotherFunction(float arg3); // Function
that takes a float as an argument and returns
an integer.
struct Person person1; // Declaring a
variable of type "Person."
// Global variables (usually avoid these, but
strcpy(person1.name, "Alice"); // Copying a hey, it's your show)
string into the "name" field of person1.
int globalVar = 42; // A global variable
person1.age = 30; // Assigning a value to accessible throughout the entire program.
the "age" field of person1.

int main() {
// Memory allocation
// Local variables (limited to this block of
int* dynamicArray = (int*)malloc(10 * code)
sizeof(int)); // Dynamically allocating memory
for an array. int num1 = 42; // Declaring and initializing
an integer variable "num1".
free(dynamicArray); // Freeing the memory
when done with the dynamically allocated int num2 = 69; // Declaring and initializing
array. another integer variable "num2".

// Preprocessor directives // Control flow


#define PI 3.14159 // Defining a constant if (num1 > num2) { // Simple if statement
using the preprocessor. with a condition.
#ifdef DEBUG // Conditional compilation printf("Num1 is greater than Num2\n");
based on the DEBUG macro.
} else if (num1 < num2) { // Else if statement
printf("Debug mode is on!\n"); with a different condition.
#endif printf("Num2 is greater than Num1\n");
} else { // Else statement when none of the
conditions are met.
return 0; // Returning 0 to indicate
successful program execution. printf("Num1 and Num2 are equal\n");
} }

#include <stdio.h> // Standard input-output for (int i = 0; i < 10; i++) { // For loop with
library for basic I/O functions. initialization, condition, and iteration
expression.
printf("Iteration %d\n", i);
// Function prototypes
}
void someFunction(int arg1, char arg2); //
Function that takes an integer and a character
as arguments but doesn't return anything.
// Function calls
someFunction(num1, 'A'); // Calling the int* dynamicArray = (int*)malloc(10 *
function "someFunction" with the arguments sizeof(int)); // Dynamically allocating memory
"num1" and the character 'A'. for an array of 10 integers.
float result = anotherFunction(3.14); // if (dynamicArray != NULL) { // Checking if
Calling the function "anotherFunction" with memory allocation was successful.
the argument 3.14 (a float).
for (int i = 0; i < 10; i++) { // Loop to assign
values to the dynamically allocated array.
// Pointers dynamicArray[i] = i * 2;
int* ptr = &num1; // Declaring a pointer }
"ptr" to an integer and assigning the address
of "num1" to it. free(dynamicArray); // Freeing the
dynamically allocated memory when done
*ptr = 99; // Dereferencing the pointer to using it.
modify the value of "num1" through "ptr".
} else {
printf("Memory allocation failed!\n");
// Arrays
}
int numbers[5] = {1, 2, 3, 4, 5}; // Declaring
and initializing an array of integers with 5
elements. // Preprocessor directives
char name[] = "John"; // Declaring and #define PI 3.14159 // Defining a constant
initializing an array of characters for a name. named "PI" with the value 3.14159.
#ifdef DEBUG // Conditional compilation
// Structures based on the "DEBUG" macro.

struct Person { // Defining a structure called printf("Debug mode is on!\n");


"Person." #endif
char name[50]; // Character array field for
the name.
return 0; // Returning 0 to indicate
int age; // Integer field for the age. successful program execution.
}; }
#include <stdio.h> // Standard input-output
struct Person person1; // Declaring a library for basic I/O functions.
variable "person1" of type "Person." #include <stdlib.h> // Standard library for
strcpy(person1.name, "Alice"); // Copying functions like malloc and free.
the string "Alice" into the "name" field of
"person1".
// Function prototypes
person1.age = 30; // Assigning the value 30
to the "age" field of "person1". int addNumbers(int num1, int num2); //
Function with two integer parameters and an
integer return value.
// Memory allocation
float divideNumbers(float num1, float num2); printf("While loop iteration %d\n", i);
// Function with two float parameters and a
float return value. i++;
}

// Global variables (don't go overboard with


these, seriously) do {
int globalVar = 42; // A global variable printf("Do-while loop iteration %d\n", i);
accessible throughout the entire program.
i--;
} while (i > 0);
int main() {
// Local variables (keep 'em tidy and
localized) for (int j = 0; j < 5; j++) {

int num1 = 42; // Declaring and initializing printf("For loop iteration %d\n", j);
an integer variable "num1". }
int num2 = 69; // Declaring and initializing
another integer variable "num2".
// Function calls
int sum = addNumbers(num1, num2);
// Basic input-output
float divisionResult = divideNumbers(10.5,
printf("Enter your name: "); 2.5);
char name[50];
scanf("%s", name); // Pointers and dynamic memory allocation
printf("Hello, %s!\n", name); int* ptr = &num1;
*ptr = 99;
// Control flow
if (num1 > num2) { int* dynamicArray = (int*)malloc(5 *
printf("Num1 is greater than Num2\n"); sizeof(int));

} else if (num1 < num2) { for (int k = 0; k < 5; k++) {

printf("Num2 is greater than Num1\n"); dynamicArray[k] = k * 3;

} else { }

printf("Num1 and Num2 are equal\n");


} // Arrays and strings
int numbers[5] = {1, 2, 3, 4, 5};

// Loops (while, do-while, for) char message[] = "Hello, World!";

int i = 0;
while (i < 5) { // Structures (getting fancy now)
struct Person {
char name[50]; // Nested structures (a touch of complexity)
int age; struct Address {
}; char street[100];
char city[50];
struct Person person1; char country[50];
strcpy(person1.name, "Alice"); };
person1.age = 30;
struct Contact {
// Enums (because why not) char name[50];
enum Color { RED, GREEN, BLUE }; int age;
enum Color favoriteColor = GREEN; struct Address address;
};
// Preprocessor directives (tread carefully)
#define PI 3.14159 struct Contact contact1;
#ifdef DEBUG strcpy(contact1.name, "Bob");
printf("Debug mode is on!\n"); contact1.age = 25;
#endif strcpy(contact1.address.street, "123 Main
St");
strcpy(contact1.address.city, "New York");
// More functions for good measure
strcpy(contact1.address.country, "USA");
int square(int x) {
return x * x;
// Back to basics, file I/O
}
FILE* filePointer = fopen("data.txt", "w");
if (filePointer != NULL) {
// Recursive function (going deeper)
fprintf(filePointer, "Hello, File I/O!");
int factorial(int n) {
fclose(filePointer);
if (n == 0 || n == 1) {
}
return 1;
} else {
// Bitwise operations (a dash of geekiness)
return n * factorial(n - 1);
unsigned int a = 60; // Binary: 0011 1100
}
unsigned int b = 13; // Binary: 0000 1101
}
unsigned int resultAnd = a & b; // Binary: // Function definitions
0000 1100
int addNumbers(int num1, int num2) {
unsigned int resultOr = a | b; // Binary:
0011 1101 return num1 + num2;

unsigned int resultXor = a ^ b; // Binary: }


0011 0001
unsigned int resultShiftLeft = a << 2; // float divideNumbers(float num1, float num2) {
Binary: 1111 0000
if (num2 != 0) {
unsigned int resultShiftRight = a >> 2; //
Binary: 0000 1111 return num1 / num2;
} else {

// Sizeof operator (know your data) printf("Error: Division by zero!\n");

int sizeOfInt = sizeof(int); return 0.0;

int sizeOfFloat = sizeof(float); }


}

// Typedef (giving names to your types)


typedef struct { #include <stdio.h> // Standard input-output
library for basic I/O functions.
char firstName[50];
#include <stdlib.h> // Standard library for
char lastName[50]; functions like malloc and free.
int age;
} PersonInfo; // Function prototypes
int addNumbers(int num1, int num2); //
PersonInfo person2; Function with two integer parameters and an
integer return value.
strcpy(person2.firstName, "Eve");
float divideNumbers(float num1, float num2);
strcpy(person2.lastName, "Johnson"); // Function with two float parameters and a
float return value.
person2.age = 28;

// Global variables (not recommended, but


// And finally, let's not forget our heroes! hey, you're the boss)
printf("In this program, %d and %d are the int globalVar = 42; // A global variable
main heroes of this story!\n", num1, num2); accessible throughout the entire program.

return 0; // Returning 0 to indicate // Data Structures and Algorithms


successful program execution.
}
// Linked List
struct Node { }
int data;
struct Node* next; int isStackEmpty(struct Stack* stack) {
}; return stack->top == -1;
}
void insertAtBeginning(struct Node** head,
int value) {
int isStackFull(struct Stack* stack) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node)); return stack->top == MAX_STACK_SIZE - 1;

newNode->data = value; }

newNode->next = *head;
*head = newNode; void push(struct Stack* stack, int value) {

} if (!isStackFull(stack)) {
stack->array[++stack->top] = value;

void printLinkedList(struct Node* head) { } else {

struct Node* current = head; printf("Stack Overflow!\n");

while (current != NULL) { }

printf("%d -> ", current->data); }

current = current->next;
} int pop(struct Stack* stack) {

printf("NULL\n"); if (!isStackEmpty(stack)) {

} return stack->array[stack->top--];
} else {
printf("Stack Underflow!\n");
// Stack
#define MAX_STACK_SIZE 100 return -1; // Assuming -1 is not a valid
element in the stack.
}
struct Stack {
}
int top;
int array[MAX_STACK_SIZE];
// Binary Search Tree
};
struct TreeNode {
int data;
void initializeStack(struct Stack* stack) {
struct TreeNode* left;
stack->top = -1;
struct TreeNode* right;
};
// Let's tie it all together in the main function!
struct TreeNode* createNewNode(int value) { int main() {
struct TreeNode* newNode = (struct // ... (previous code)
TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = value;
// Linked List
newNode->left = NULL;
struct Node* linkedList = NULL;
newNode->right = NULL;
insertAtBeginning(&linkedList, 10);
return newNode;
insertAtBeginning(&linkedList, 20);
}
insertAtBeginning(&linkedList, 30);
printf("Linked List: ");
struct TreeNode* insertInBST(struct
TreeNode* root, int value) { printLinkedList(linkedList);

if (root == NULL) {
return createNewNode(value); // Stack

} struct Stack stack;


initializeStack(&stack);

if (value < root->data) { push(&stack, 5);

root->left = insertInBST(root->left, value); push(&stack, 10);

} else if (value > root->data) { push(&stack, 15);

root->right = insertInBST(root->right, printf("Stack: ");


value); while (!isStackEmpty(&stack)) {
} printf("%d -> ", pop(&stack));
}
return root; printf("NULL\n");
}

// Binary Search Tree


void inOrderTraversal(struct TreeNode* root) { struct TreeNode* root = NULL;
if (root != NULL) { root = insertInBST(root, 50);
inOrderTraversal(root->left); root = insertInBST(root, 30);
printf("%d -> ", root->data); root = insertInBST(root, 20);
inOrderTraversal(root->right); root = insertInBST(root, 40);
} root = insertInBST(root, 70);
}
root = insertInBST(root, 60); int addNumbers(int num1, int num2); //
Function with two integer parameters and an
root = insertInBST(root, 80); integer return value.
printf("Binary Search Tree (In-Order float divideNumbers(float num1, float num2);
Traversal): "); // Function with two float parameters and a
inOrderTraversal(root); float return value.

printf("NULL\n");
// Global variables (still not recommended,
but I'm at your service)
// ... (remaining code)
int globalVar = 42; // A global variable
accessible throughout the entire program.
return 0; // Returning 0 to indicate
successful program execution.
// Data Structures and Algorithms
}

// Linked List
// Function definitions
struct Node {
int addNumbers(int num1, int num2) {
int data;
return num1 + num2;
struct Node* next;
}
};

float divideNumbers(float num1, float num2) {


void insertAtBeginning(struct Node** head,
if (num2 != 0) { int value) {
return num1 / num2; struct Node* newNode = (struct
} else { Node*)malloc(sizeof(struct Node));

printf("Error: Division by zero!\n"); newNode->data = value;

return 0.0; newNode->next = *head;

} *head = newNode;

} }

#include <stdio.h> // Standard input-output void printLinkedList(struct Node* head) {


library for basic I/O functions. struct Node* current = head;
#include <stdlib.h> // Standard library for while (current != NULL) {
functions like malloc and free.
printf("%d -> ", current->data);
current = current->next;
// Function prototypes
}
printf("NULL\n"); if (!isStackEmpty(stack)) {
} return stack->array[stack->top--];
} else {
// Stack printf("Stack Underflow!\n");
#define MAX_STACK_SIZE 100 return -1; // Assuming -1 is not a valid
element in the stack.
}
struct Stack {
}
int top;
int array[MAX_STACK_SIZE];
// Binary Search Tree
};
struct TreeNode {
int data;
void initializeStack(struct Stack* stack) {
struct TreeNode* left;
stack->top = -1;
struct TreeNode* right;
}
};

int isStackEmpty(struct Stack* stack) {


struct TreeNode* createNewNode(int value) {
return stack->top == -1;
struct TreeNode* newNode = (struct
} TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = value;
int isStackFull(struct Stack* stack) { newNode->left = NULL;
return stack->top == MAX_STACK_SIZE - 1; newNode->right = NULL;
} return newNode;
}
void push(struct Stack* stack, int value) {
if (!isStackFull(stack)) { struct TreeNode* insertInBST(struct
stack->array[++stack->top] = value; TreeNode* root, int value) {

} else { if (root == NULL) {

printf("Stack Overflow!\n"); return createNewNode(value);

} }

}
if (value < root->data) {

int pop(struct Stack* stack) { root->left = insertInBST(root->left, value);


} else if (value > root->data) {
root->right = insertInBST(root->right, return queue->size == MAX_QUEUE_SIZE;
value);
}
}

void enqueue(struct Queue* queue, int value)


return root; {
} if (!isQueueFull(queue)) {
queue->rear = (queue->rear + 1) %
MAX_QUEUE_SIZE;
void inOrderTraversal(struct TreeNode* root) {
queue->array[queue->rear] = value;
if (root != NULL) {
queue->size++;
inOrderTraversal(root->left);
} else {
printf("%d -> ", root->data);
printf("Queue is Full!\n");
inOrderTraversal(root->right);
}
}
}
}

int dequeue(struct Queue* queue) {


// Queue
if (!isQueueEmpty(queue)) {
#define MAX_QUEUE_SIZE 100
int item = queue->array[queue->front];
queue->front = (queue->front + 1) %
struct Queue {
MAX_QUEUE_SIZE;
int front, rear, size; queue->size--;
int array[MAX_QUEUE_SIZE]; return item;
}; } else {
printf("Queue is Empty!\n");
void initializeQueue(struct Queue* queue) { return -1; // Assuming -1 is not a valid
queue->front = queue->size = 0; element in the queue.

queue->rear = MAX_QUEUE_SIZE - 1; }

} }

int isQueueEmpty(struct Queue* queue) { // And there's more! Merge Sort

return queue->size == 0; void merge(int arr[], int left, int mid, int right)
{
}
int i, j, k;
int n1 = mid - left + 1;
int isQueueFull(struct Queue* queue) {
int n2 = right - mid; k++;
}
int L[n1], R[n2]; }

for (i = 0; i < n1; i++) void mergeSort(int arr[], int left, int right) {
L[i] = arr[left + i]; if (left < right) {
for (j = 0; j < n2; j++) int mid = left + (right - left) / 2;
R[j] = arr[mid + 1 + j];
mergeSort(arr, left, mid);
i = 0; mergeSort(arr, mid + 1, right);
j = 0;
k = left; merge(arr, left, mid, right);
while (i < n1 && j < n2) { }
if (L[i] <= R[j]) { }
arr[k] = L[i];
i++; // Let's tie it all together in the main function!
} else { int main() {
arr[k] = R[j]; // ... (previous code)
j++;
} // Queue
k++; struct Queue queue;
} initializeQueue(&queue);
enqueue(&queue, 5);
while (i < n1) { enqueue(&queue, 10);
arr[k] = L[i]; enqueue(&queue, 15);
i++; printf("Queue: ");
k++; while (!isQueueEmpty(&queue)) {
} printf("%d -> ", dequeue(&queue));
}
while (j < n2) { printf("NULL\n");
arr[k] = R[j];
j++; // Merge Sort
int arr[] = {38, 27, 43, 3, 9, 82, 10}; int addNumbers(int num1, int num2); //
Function with two integer parameters and an
int n = sizeof(arr) / sizeof(arr[0]); integer return value.
printf("Before Merge Sort: "); float divideNumbers(float num1, float num2);
for (int i = 0; i < n; i++) { // Function with two float parameters and a
float return value.
printf("%d ", arr[i]);
}
// Global variables (still not recommended,
printf("\n"); but it's your world)
int globalVar = 42; // A global variable
mergeSort(arr, 0, n - 1); accessible throughout the entire program.

printf("After Merge Sort: ");


for (int i = 0; i < n; i++) { // Data Structures and Algorithms

printf("%d ", arr[i]);


} // Linked List

printf("\n"); struct Node {


int data;

// ... (remaining code) struct Node* next;


};

return 0; // Returning 0 to indicate


successful program execution. void insertAtBeginning(struct Node** head,
} int value) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
// Function definitions
newNode->data = value;
int addNumbers(int num1, int num2
newNode->next = *head;
*head = newNode;
}

#include <stdio.h> // Standard input-output


library for basic I/O functions. void printLinkedList(struct Node* head) {

#include <stdlib.h> // Standard library for struct Node* current = head;


functions like malloc and free. while (current != NULL) {
printf("%d -> ", current->data);
// Function prototypes current = current->next;
}
printf("NULL\n"); if (!isStackEmpty(stack)) {
} return stack->array[stack->top--];
} else {
// Stack printf("Stack Underflow!\n");
#define MAX_STACK_SIZE 100 return -1; // Assuming -1 is not a valid
element in the stack.
}
struct Stack {
}
int top;
int array[MAX_STACK_SIZE];
// Binary Search Tree
};
struct TreeNode {
int data;
void initializeStack(struct Stack* stack) {
struct TreeNode* left;
stack->top = -1;
struct TreeNode* right;
}
};

int isStackEmpty(struct Stack* stack) {


struct TreeNode* createNewNode(int value) {
return stack->top == -1;
struct TreeNode* newNode = (struct
} TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = value;
int isStackFull(struct Stack* stack) { newNode->left = NULL;
return stack->top == MAX_STACK_SIZE - 1; newNode->right = NULL;
} return newNode;
}
void push(struct Stack* stack, int value) {
if (!isStackFull(stack)) { struct TreeNode* insertInBST(struct
stack->array[++stack->top] = value; TreeNode* root, int value) {

} else { if (root == NULL) {

printf("Stack Overflow!\n"); return createNewNode(value);

} }

}
if (value < root->data) {

int pop(struct Stack* stack) { root->left = insertInBST(root->left, value);


} else if (value > root->data) {
root->right = insertInBST(root->right, return queue->size == MAX_QUEUE_SIZE;
value);
}
}

void enqueue(struct Queue* queue, int value)


return root; {
} if (!isQueueFull(queue)) {
queue->rear = (queue->rear + 1) %
MAX_QUEUE_SIZE;
void inOrderTraversal(struct TreeNode* root) {
queue->array[queue->rear] = value;
if (root != NULL) {
queue->size++;
inOrderTraversal(root->left);
} else {
printf("%d -> ", root->data);
printf("Queue is Full!\n");
inOrderTraversal(root->right);
}
}
}
}

int dequeue(struct Queue* queue) {


// Queue
if (!isQueueEmpty(queue)) {
#define MAX_QUEUE_SIZE 100
int item = queue->array[queue->front];
queue->front = (queue->front + 1) %
struct Queue {
MAX_QUEUE_SIZE;
int front, rear, size; queue->size--;
int array[MAX_QUEUE_SIZE]; return item;
}; } else {
printf("Queue is Empty!\n");
void initializeQueue(struct Queue* queue) { return -1; // Assuming -1 is not a valid
queue->front = queue->size = 0; element in the queue.

queue->rear = MAX_QUEUE_SIZE - 1; }

} }

int isQueueEmpty(struct Queue* queue) { // Merge Sort

return queue->size == 0; void merge(int arr[], int left, int mid, int right)
{
}
int i, j, k;
int n1 = mid - left + 1;
int isQueueFull(struct Queue* queue) {
int n2 = right - mid; k++;
}
int L[n1], R[n2]; }

for (i = 0; i < n1; i++) void mergeSort(int arr[], int left, int right) {
L[i] = arr[left + i]; if (left < right) {
for (j = 0; j < n2; j++) int mid = left + (right - left) / 2;
R[j] = arr[mid + 1 + j];
mergeSort(arr, left, mid);
i = 0; mergeSort(arr, mid + 1, right);
j = 0;
k = left; merge(arr, left, mid, right);
while (i < n1 && j < n2) { }
if (L[i] <= R[j]) { }
arr[k] = L[i];
i++; // Complete Syntax of C in One Code!
} else {
arr[k] = R[j]; int main() {
j++; // Basic data types
} int integerVariable = 42;
k++; float floatVariable = 3.14;
} char charVariable = 'A';
double doubleVariable = 1.2345;
while (i < n1) { _Bool boolVariable = 1; // 1 for true, 0 for
false
arr[k] = L[i];
i++;
// Arrays
k++;
int integerArray[5] = {1, 2, 3, 4, 5};
}
char charArray[6] = "Hello"; //
Automatically includes a null terminator '\0'
while (j < n2) { float floatArray[] = {2.5, 3.14, 1.618};
arr[k] = R[j];
j++; // Pointers and Memory Allocation
int* intPtr = &integerVariable; count--;
*intPtr = 99; } while (count > 0);
printf("\n");
int* dynamicArray = (int*)malloc(5 *
sizeof(int));
// Functions
for (int i = 0; i < 5; i++) {
int result = addNumbers(10, 20);
dynamicArray[i] = i * 10;
float divisionResult = divideNumbers(10.5,
} 2.0);
free(dynamicArray);
// Data Structures and Algorithms
// Control Flow struct Node* linkedList = NULL;
if (integerVariable > 50) { insertAtBeginning(&linkedList, 10);
printf("Greater than 50\n"); insertAtBeginning(&linkedList, 20);
} else if (integerVariable < 30) { insertAtBeginning(&linkedList, 30);
printf("Less than 30\n"); printf("Linked List: ");
} else { printLinkedList(linkedList);
printf("Between 30 and 50\n");
} struct Stack stack;
initializeStack(&stack);
for (int i = 0; i < 5; i++) { push(&stack, 5);
printf("%d ", i); push(&stack, 10);
} push(&stack, 15);
printf("\n"); printf("Stack: ");
while (!isStackEmpty(&stack)) {
int count = 0; printf("%d -> ", pop(&stack));
while (count < 5) { }
printf("%d ", count); printf("NULL\n");
count++;
} struct TreeNode* root = NULL;
printf("\n"); root = insertInBST(root, 50);
root = insertInBST(root, 30);
do { root = insertInBST(root, 20);
printf("%d ", count); root = insertInBST(root, 40);
root = insertInBST(root, 70);
root = insertInBST(root, 60); return 0; // Returning 0 to indicate
successful program execution.
root = insertInBST(root, 80);
}
printf("Binary Search Tree (In-Order
Traversal): ");
inOrderTraversal(root); // Function definitions
printf("NULL\n"); int addNumbers(int num1, int num2) {
return num1 + num2;
struct Queue queue; }
initializeQueue(&queue);
enqueue(&queue, 5); float divideNumbers(float num1, float num2) {
enqueue(&queue, 10); if (num2 != 0) {
enqueue(&queue, 15); return num1 / num2;
printf("Queue: "); } else {
while (!isQueueEmpty(&queue)) { printf("Error: Division by zero!\n");
printf("%d -> ", dequeue(&queue)); return 0.0;
} }
printf("NULL\n"); }

int arr[] = {38, 27, 43, 3, 9, 82, 10};


int n = sizeof(arr) / sizeof(arr[0]);
printf("Before Merge Sort: "); #include <stdio.h> // Standard input-output
library for basic I/O functions.
for (int i = 0; i < n; i++) {
#include <stdlib.h> // Standard library for
printf("%d ", arr[i]); functions like malloc and free.
}
printf("\n"); // Basic Data Types
void basicDataTypesExample() {
mergeSort(arr, 0, n - 1); int integerVariable = 42;
printf("After Merge Sort: "); float floatVariable = 3.14;
for (int i = 0; i < n; i++) { char charVariable = 'A';
printf("%d ", arr[i]); double doubleVariable = 1.2345;
} _Bool boolVariable = 1; // 1 for true, 0 for
printf("\n"); false
printf("Integer: %d\n", integerVariable); int* dynamicArray = (int*)malloc(5 *
sizeof(int));
printf("Float: %f\n", floatVariable);
for (int i = 0; i < 5; i++) {
printf("Character: %c\n", charVariable);
dynamicArray[i] = i * 10;
printf("Double: %lf\n", doubleVariable);
}
printf("Boolean: %d\n", boolVariable);
}
for (int i = 0; i < 5; i++) {
printf("dynamicArray[%d]: %d\n", i,
// Arrays dynamicArray[i]);
void arrayExample() { }
int integerArray[5] = {1, 2, 3, 4, 5};
char charArray[] = "Hello"; free(dynamicArray);
float floatArray[] = {2.5, 3.14, 1.618}; }

for (int i = 0; i < 5; i++) { // Control Flow


printf("integerArray[%d]: %d\n", i, void controlFlowExample() {
integerArray[i]);
int num = 42;
}

if (num > 50) {


printf("charArray: %s\n", charArray);
printf("Greater than 50\n");
} else if (num < 30) {
for (int i = 0; i < 3; i++) {
printf("Less than 30\n");
printf("floatArray[%d]: %f\n", i,
floatArray[i]); } else {
} printf("Between 30 and 50\n");
} }

// Pointers and Memory Allocation for (int i = 0; i < 5; i++) {


void pointerAndMemoryExample() { printf("%d ", i);
int integerVariable = 42; }
int* intPtr = &integerVariable; printf("\n");

printf("Value of integerVariable: %d\n", int count = 0;


*intPtr);
while (count < 5) { printf("Result of division: %f\n",
divisionResult);
printf("%d ", count);
}
count++;
}
// Linked List
printf("\n");
struct Node {
int data;
do {
struct Node* next;
printf("%d ", count);
};
count--;
} while (count > 0);
void insertAtBeginning(struct Node** head,
printf("\n"); int value) {
} struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));

// Functions newNode->data = value;

int addNumbers(int num1, int num2) { newNode->next = *head;

return num1 + num2; *head = newNode;

} }

float divideNumbers(float num1, float num2) { void printLinkedList(struct Node* head) {

if (num2 != 0) { struct Node* current = head;

return num1 / num2; while (current != NULL) {

} else { printf("%d -> ", current->data);

printf("Error: Division by zero!\n"); current = current->next;

return 0.0; }

} printf("NULL\n");

} }

void functionExample() { void linkedListExample() {

int result = addNumbers(10, 20); struct Node* linkedList = NULL;

float divisionResult = divideNumbers(10.5, insertAtBeginning(&linkedList, 10);


2.0); insertAtBeginning(&linkedList, 20);
insertAtBeginning(&linkedList, 30);
printf("Result of addition: %d\n", result); printf("Linked List: ");
printLinkedList(linkedList); if (!isStackEmpty(stack)) {
} return stack->array[stack->top--];
} else {
// Stack printf("Stack Underflow!\n");
#define MAX_STACK_SIZE 100 return -1; // Assuming -1 is not a valid
element in the stack.
}
struct Stack {
}
int top;
int array[MAX_STACK_SIZE];
void stackExample() {
};
struct Stack stack;
initializeStack(&stack);
void initializeStack(struct Stack* stack) {
push(&stack, 5);
stack->top = -1;
push(&stack, 10);
}
push(&stack, 15);
printf("Stack: ");
int isStackEmpty(struct Stack* stack) {
while (!isStackEmpty(&stack)) {
return stack->top == -1;
printf("%d -> ", pop(&stack));
}
}
printf("NULL\n");
int isStackFull(struct Stack* stack) {
}
return stack->top == MAX_STACK_SIZE - 1;
}
// Binary Search Tree
struct TreeNode {
void push(struct Stack* stack, int value) {
int data;
if (!isStackFull(stack)) {
struct TreeNode* left;
stack->array[++stack->top] = value;
struct TreeNode* right;
} else {
};
printf("Stack Overflow!\n");
}
struct TreeNode* createNewNode(int value) {
}
struct TreeNode* newNode = (struct
TreeNode*)malloc(sizeof(struct TreeNode));
int pop(struct Stack* stack) { newNode->data = value;
newNode->left = NULL; root = insertInBST(root, 20);
newNode->right = NULL; root = insertInBST(root, 40);
return newNode; root = insertInBST(root, 70);
} root = insertInBST(root, 60);
root = insertInBST(root, 80);
struct TreeNode* insertInBST(struct printf("Binary Search Tree (In-Order
TreeNode* root, int value) { Traversal): ");
if (root == NULL) { inOrderTraversal(root);
return createNewNode(value); printf("NULL\n");
} }

if (value < root->data) { // Queue


root->left = insertInBST(root->left, value); #define MAX_QUEUE_SIZE 100
} else if (value > root->data) {
root->right = insertInBST(root->right, struct Queue {
value);
int front, rear, size;
}
int array[MAX_QUEUE_SIZE];
};
return root;
}
void initializeQueue(struct Queue* queue) {
queue->front = queue->size = 0;
void inOrderTraversal(struct TreeNode* root) {
queue->rear = MAX_QUEUE_SIZE - 1;
if (root != NULL) {
}
inOrderTraversal(root->left);
printf("%d -> ", root->data);
int isQueueEmpty(struct Queue* queue) {
inOrderTraversal(root->right);
return queue->size == 0;
}
}
}

int isQueueFull(struct Queue* queue) {


void binarySearchTreeExample() {
return queue->size == MAX_QUEUE_SIZE;
struct TreeNode* root = NULL;
}
root = insertInBST(root, 50);
root = insertInBST(root, 30);
void enqueue(struct Queue* queue, int value) printf("%d -> ", dequeue(&queue));
{
}
if (!isQueueFull(queue)) {
printf("NULL\n");
queue->rear = (queue->rear + 1) %
MAX_QUEUE_SIZE; }

queue->array[queue->rear] = value;
queue->size++; // Merge Sort

} else { void merge(int arr[], int left, int mid, int right)
{
printf("Queue is Full!\n");
int i, j, k;
}
int n1 = mid - left + 1;
}
int n2 = right - mid;

int dequeue(struct Queue* queue) {


int L[n1], R[n2];
if (!isQueueEmpty(queue)) {
int item = queue->array[queue->front];
for (i = 0; i < n1; i++)
queue->front = (queue->front + 1) %
MAX_QUEUE_SIZE; L[i] = arr[left + i];

queue->size--; for (j = 0; j < n2; j++)

return item; R[j] = arr[mid + 1 + j];

} else {
printf("Queue is Empty!\n"); i = 0;

return -1; // Assuming -1 is not a valid j = 0;


element in the queue. k = left;
} while (i < n1 && j < n2) {
} if (L[i] <= R[j]) {
arr[k] = L[i];
void queueExample() { i++;
struct Queue queue; } else {
initializeQueue(&queue); arr[k] = R[j];
enqueue(&queue, 5); j++;
enqueue(&queue, 10); }
enqueue(&queue, 15); k++;
printf("Queue: "); }
while (!isQueueEmpty(&queue)) {
while (i < n1) {
arr[k] = L[i]; mergeSort(arr, 0, n - 1);
i++; printf("After Merge Sort: ");
k++; for (int i = 0; i < n; i++) {
} printf("%d ", arr[i]);
}
while (j < n2) { printf("\n");
arr[k] = R[j]; }
j++;
k++; // Complete Example with All Sections
} int main() {
} printf("Basic Data Types Example:\n");
basicDataTypesExample();
void mergeSort(int arr[], int left, int right) { printf("\n");
if (left < right) {
int mid = left + (right - left) / 2; printf("Arrays Example:\n");
arrayExample();
mergeSort(arr, left, mid); printf("\n");
mergeSort(arr, mid + 1, right);
printf("Pointers and Memory Allocation
Example:\n");
merge(arr, left, mid, right);
pointerAndMemoryExample();
}
printf("\n");
}

printf("Control Flow Example:\n");


void mergeSortExample() {
controlFlowExample();
int arr[] = {38, 27, 43, 3, 9, 82, 10};
printf("\n");
int n = sizeof(arr) / sizeof(arr[0]);
printf("Before Merge Sort: ");
printf("Functions Example:\n");
for (int i = 0; i < n; i++) {
functionExample();
printf("%d ", arr[i]);
printf("\n");
}
printf("\n");
printf("Linked List Example:\n");
linkedListExample(); Learn how to access individual elements in an
array using index notation.
printf("\n");
Practice using loops to iterate through arrays
and perform operations.
printf("Stack Example:\n"); 3. Pointers and Memory Allocation
stackExample(); Understand the concept of pointers and how
printf("\n"); they store memory addresses.
Learn about the & (address-of) and *
(dereference) operators.
printf("Binary Search Tree Example:\n");
Explore dynamic memory allocation using
binarySearchTreeExample(); malloc and free.
printf("\n"); 4. Control Flow
Learn about conditional statements like if, else
printf("Queue Example:\n"); if, and else.

queueExample(); Practice using loops such as for, while, and do-


while.
printf("\n");
Understand how to break and continue loops
based on specific conditions.
printf("Merge Sort Example:\n"); 5. Functions
mergeSortExample(); Learn about functions and how to declare and
define them.
printf("\n");
Explore function parameters and return
values.
return 0; // Returning 0 to indicate
Practice using functions for various tasks and
successful program execution.
computations.
}
6. Data Structures and Algorithms
6.1 Linked List
Guide to Learning C Programming
Understand the concept of linked lists and
1. Basic Data Types their nodes.

Learn about the fundamental data types in C, Learn how to insert and delete elements in a
such as int, float, char, double, and _Bool. linked list.

Understand how to declare and initialize Practice traversing and printing linked lists.
variables of different data types.
6.2 Stack
Practice using format specifiers with printf to
Explore the concept of stacks and their LIFO
display data.
(Last-In-First-Out) behavior.
2. Arrays
Learn how to implement stack operations like
Explore one-dimensional arrays and how to push and pop.
declare and initialize them.
Practice using stacks for various applications,
such as balancing parentheses.
Comprehensive Guide to Learning C
6.3 Binary Search Tree (BST) Programming
Understand the properties of a binary search 1. Introduction to C Programming
tree.
Understand what C programming is and its
Learn how to insert elements into a BST while significance in the world of software
maintaining its properties. development.
Practice traversing and searching in a BST. Learn about the history and features of the C
programming language.
6.4 Queue
2. Setting Up the Development Environment
Explore the concept of queues and their FIFO
(First-In-First-Out) behavior. Install a C compiler (e.g., GCC) and an
Integrated Development Environment (IDE)
Learn how to implement queue operations for a smooth development experience.
like enqueue and dequeue.
Configure the development environment to
Practice using queues for tasks like BFS compile and run C programs.
(Breadth-First Search) traversal.
3. Basic Syntax and Data Types
6.5 Merge Sort
Learn about the basic syntax of C
Understand the concept of the merge sort programming, including comments, variables,
algorithm. and data types.
Learn how to implement merge sort to Explore fundamental data types such as int,
efficiently sort arrays.
float, char, double, and _Bool.
Practice applying merge sort to sort various Practice declaring and initializing variables of
datasets. different data types.
7. Putting it All Together
4. Input and Output
Combine the knowledge from the previous Understand C's standard I/O functions,
sections to create more complex programs. including printf and scanf.
Practice writing complete C programs that Learn how to read and write data from/to the
involve data manipulation, algorithms, and console.
user interactions.
5. Control Flow
Explore and solve programming challenges to
reinforce your skills. Explore conditional statements such as if, else
if, and else.
8. Continue Learning and Exploring
Learn about loops, including for, while, and
Explore advanced C topics such as structures, do-while.
file I/O, and memory management
techniques. Understand the use of break and continue
statements in loops.
6. Arrays and Pointers
Learn about arrays, one-dimensional and
multi-dimensional.
Understand the concept of pointers and their Explore commonly used preprocessor
relationship with arrays. directives like #include, #define, and #ifdef.
Practice using pointers to access and 13. Advanced Topics (Optional)
manipulate array elements.
Explore advanced C topics like structures,
7. Functions unions, and function pointers.
Understand the importance of functions in Learn about memory management techniques
organizing code and promoting reusability. and avoiding common pitfalls.
Learn how to declare and define functions, 14. Putting it All Together
including function prototypes.
Combine the knowledge from the previous
Explore function parameters and return sections to create comprehensive C programs.
values.
Practice solving coding challenges and
8. Memory Allocation and Dynamic Data exercises to reinforce your skills.
Structures
15. Further Learning and Resources
Learn about dynamic memory allocation using
malloc, calloc, and realloc. Continue your learning journey with books,
online courses, and coding platforms.
Understand how to work with memory
dynamically and handle memory leaks. Participate in coding competitions and open-
source projects to gain practical experience.
Explore dynamic data structures like linked
lists and trees. Remember, learning C programming requires
consistent practice and exploration. Don't
9. File I/O hesitate to experiment with code, seek help
from online communities, and challenge
Learn about file handling in C programming. yourself with new projects. Happy coding and
Understand how to read from and write to may your programming journey be filled with
files using fopen, fclose, fread, and fwrite. discovery and success!

10. Data Structures and Algorithms


Explore common data structures like linked
lists, stacks, queues, and binary search trees
(BST).
Learn about algorithms like searching, sorting,
and graph traversal.
Practice implementing data structures and
algorithms in C.
Comprehensive Guide to Learning C
11. Error Handling Programming with Code Examples
Understand the concept of error handling and 1. Introduction to C Programming
how to use errno and perror.
Understand what C programming is and its
Learn about custom error handling using significance in the world of software
assert and exit. development.
12. Preprocessor Directives Learn about the history and features of the C
programming language.
Learn about preprocessor directives and how
they affect the compilation process. 2. Setting Up the Development Environment
Install a C compiler (e.g., GCC) and an printf("You are %d years old.\n", age);
Integrated Development Environment (IDE)
for a smooth development experience.
Configure the development environment to return 0;
compile and run C programs. }
3. Basic Syntax and Data Types 5. Control Flow
c c
Copy code Copy code
#include <stdio.h> #include <stdio.h>

int main() { int main() {


int integerVariable = 42; int num = 42;
float floatVariable = 3.14;
char charVariable = 'A'; if (num > 50) {
double doubleVariable = 1.2345; printf("Greater than 50\n");
_Bool boolVariable = 1; } else if (num < 30) {
printf("Less than 30\n");
printf("Integer: %d\n", integerVariable); } else {
printf("Float: %f\n", floatVariable); printf("Between 30 and 50\n");
printf("Character: %c\n", charVariable); }
printf("Double: %lf\n", doubleVariable);
printf("Boolean: %d\n", boolVariable); for (int i = 0; i < 5; i++) {
printf("%d ", i);
return 0; }
} printf("\n");
4. Input and Output
c int count = 0;
Copy code while (count < 5) {
#include <stdio.h> printf("%d ", count);
count++;
int main() { }
int age; printf("\n");
printf("Enter your age: ");
scanf("%d", &age);
do { int main() {
printf("%d ", count); int result = addNumbers(10, 20);
count--; printf("Result of addition: %d\n", result);
} while (count > 0);
printf("\n"); return 0;
}
return 0; 8. Memory Allocation and Dynamic Data
Structures
}
c
6. Arrays and Pointers
Copy code
c
#include <stdio.h>
Copy code
#include <stdlib.h>
#include <stdio.h>

int main() {
int main() {
int* dynamicArray = (int*)malloc(5 *
int integerArray[5] = {1, 2, 3, 4, 5}; sizeof(int));
int* intPtr = integerArray; for (int i = 0; i < 5; i++) {
dynamicArray[i] = i * 10;
for (int i = 0; i < 5; i++) { }
printf("integerArray[%d]: %d\n", i,
*(intPtr + i));
for (int i = 0; i < 5; i++) {
}
printf("dynamicArray[%d]: %d\n", i,
dynamicArray[i]);
return 0; }
}
7. Functions free(dynamicArray);
c
Copy code return 0;
#include <stdio.h> }
9. File I/O
int addNumbers(int num1, int num2) { c
return num1 + num2; Copy code
} #include <stdio.h>
int main() { Code examples for preprocessor directives
have already been included in previous
FILE* file = fopen("example.txt", "w"); responses.
if (file != NULL) {
fprintf(file, "Hello, World!\n"); 13. Advanced Topics (Optional)
fclose(file); Code examples for advanced topics can be
} added depending on the specific topic of
interest.

return 0;
14. Putting it All Together
}
Combine the knowledge from the previous
10. Data Structures and Algorithms sections to create comprehensive C programs.
Code examples for data structures and
algorithms have already been provided in
previous responses. 15. Further Learning and Resources
Continue your learning journey with books,
online courses, and coding platforms.
11. Error Handling
Participate in coding competitions and open-
c source projects to gain practical experience.
Copy code #include <stdio.h> // Standard input-output
#include <stdio.h> library for basic I/O functions.

#include <assert.h> #include <stdlib.h> // Standard library for


functions like malloc and free.

int divide(int num1, int num2) {


// Basic Data Types
assert(num2 != 0);
void basicDataTypesExample() {
return num1 / num2;
int integerVariable = 42;
}
float floatVariable = 3.14;
char charVariable = 'A';
int main() {
double doubleVariable = 1.2345;
int result = divide(10, 0);
_Bool boolVariable = 1; // 1 for true, 0 for
printf("Result of division: %d\n", result); false

return 0; printf("Integer: %d\n", integerVariable);


} printf("Float: %f\n", floatVariable);
12. Preprocessor Directives printf("Character: %c\n", charVariable);
printf("Double: %lf\n", doubleVariable);
printf("Boolean: %d\n", boolVariable); }
}
for (int i = 0; i < 5; i++) {
// Arrays printf("dynamicArray[%d]: %d\n", i,
dynamicArray[i]);
void arrayExample() {
}
int integerArray[5] = {1, 2, 3, 4, 5};
char charArray[] = "Hello";
free(dynamicArray);
float floatArray[] = {2.5, 3.14, 1.618};
}

for (int i = 0; i < 5; i++) {


// Control Flow
printf("integerArray[%d]: %d\n", i,
integerArray[i]); void controlFlowExample() {
} int num = 42;

printf("charArray: %s\n", charArray); if (num > 50) {


printf("Greater than 50\n");
for (int i = 0; i < 3; i++) { } else if (num < 30) {
printf("floatArray[%d]: %f\n", i, printf("Less than 30\n");
floatArray[i]);
} else {
}
printf("Between 30 and 50\n");
}
}

// Pointers and Memory Allocation


for (int i = 0; i < 5; i++) {
void pointerAndMemoryExample() {
printf("%d ", i);
int integerVariable = 42;
}
int* intPtr = &integerVariable;
printf("\n");

printf("Value of integerVariable: %d\n",


*intPtr); int count = 0;
while (count < 5) {

int* dynamicArray = (int*)malloc(5 * printf("%d ", count);


sizeof(int)); count++;
for (int i = 0; i < 5; i++) { }
dynamicArray[i] = i * 10; printf("\n");
struct Node {
do { int data;
printf("%d ", count); struct Node* next;
count--; };
} while (count > 0);
printf("\n"); void insertAtBeginning(struct Node** head,
int value) {
}
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
// Functions newNode->data = value;
int addNumbers(int num1, int num2) { newNode->next = *head;
return num1 + num2; *head = newNode;
} }

float divideNumbers(float num1, float num2) { void printLinkedList(struct Node* head) {


if (num2 != 0) { struct Node* current = head;
return num1 / num2; while (current != NULL) {
} else { printf("%d -> ", current->data);
printf("Error: Division by zero!\n"); current = current->next;
return 0.0; }
} printf("NULL\n");
} }

void functionExample() {
void linkedListExample() {
int result = addNumbers(10, 20); struct Node* linkedList = NULL;
float divisionResult = divideNumbers(10.5, insertAtBeginning(&linkedList, 10);
2.0);
insertAtBeginning(&linkedList, 20);
insertAtBeginning(&linkedList, 30);
printf("Result of addition: %d\n", result);
printf("Linked List: ");
printf("Result of division: %f\n",
divisionResult); printLinkedList(linkedList);
} }

// Linked List // Stack


#define MAX_STACK_SIZE 100 return -1; // Assuming -1 is not a valid
element in the stack.
}
struct Stack {
}
int top;
int array[MAX_STACK_SIZE];
void stackExample() {
};
struct Stack stack;
initializeStack(&stack);
void initializeStack(struct Stack* stack) {
push(&stack, 5);
stack->top = -1;
push(&stack, 10);
}
push(&stack, 15);
printf("Stack: ");
int isStackEmpty(struct Stack* stack) {
while (!isStackEmpty(&stack)) {
return stack->top == -1;
printf("%d -> ", pop(&stack));
}
}
printf("NULL\n");
int isStackFull(struct Stack* stack) {
}
return stack->top == MAX_STACK_SIZE - 1;
}
// Binary Search Tree
struct TreeNode {
void push(struct Stack* stack, int value) {
int data;
if (!isStackFull(stack)) {
struct TreeNode* left;
stack->array[++stack->top] = value;
struct TreeNode* right;
} else {
};
printf("Stack Overflow!\n");
}
struct TreeNode* createNewNode(int value) {
}
struct TreeNode* newNode = (struct
TreeNode*)malloc(sizeof(struct TreeNode));
int pop(struct Stack* stack) { newNode->data = value;
if (!isStackEmpty(stack)) { newNode->left = NULL;
return stack->array[stack->top--]; newNode->right = NULL;
} else { return newNode;
printf("Stack Underflow!\n"); }
root = insertInBST(root, 80);
struct TreeNode* insertInBST(struct printf("Binary Search Tree (In-Order
TreeNode* root, int value) { Traversal): ");
if (root == NULL) { inOrderTraversal(root);
return createNewNode(value); printf("NULL\n");
} }

if (value < root->data) { // Queue


root->left = insertInBST(root->left, value); #define MAX_QUEUE_SIZE 100
} else if (value > root->data) {
root->right = insertInBST(root->right, struct Queue {
value);
int front, rear, size;
}
int array[MAX_QUEUE_SIZE];
};
return root;
}
void initializeQueue(struct Queue* queue) {
queue->front = queue->size = 0;
void inOrderTraversal(struct TreeNode* root) {
queue->rear = MAX_QUEUE_SIZE - 1;
if (root != NULL) {
}
inOrderTraversal(root->left);
printf("%d -> ", root->data);
int isQueueEmpty(struct Queue* queue) {
inOrderTraversal(root->right);
return queue->size == 0;
}
}
}

int isQueueFull(struct Queue* queue) {


void binarySearchTreeExample() {
return queue->size == MAX_QUEUE_SIZE;
struct TreeNode* root = NULL;
}
root = insertInBST(root, 50);
root = insertInBST(root, 30);
void enqueue(struct Queue* queue, int value)
root = insertInBST(root, 20); {
root = insertInBST(root, 40); if (!isQueueFull(queue)) {
root = insertInBST(root, 70); queue->rear = (queue->rear + 1) %
MAX_QUEUE_SIZE;
root = insertInBST(root, 60);
queue->array[queue->rear] = value;
queue->size++; // Merge Sort
} else { void merge(int arr[], int left, int mid, int right)
{
printf("Queue is Full!\n");
int i, j, k;
}
int n1 = mid - left + 1;
}
int n2 = right - mid;

int dequeue(struct Queue* queue) {


int L[n1], R[n2];
if (!isQueueEmpty(queue)) {
int item = queue->array[queue->front];
for (i = 0; i < n1; i++)
queue->front = (queue->front + 1) %
MAX_QUEUE_SIZE; L[i] = arr[left + i];
queue->size--; for (j = 0; j < n2; j++)
return item; R[j] = arr[mid + 1 + j];
} else {
printf("Queue is Empty!\n"); i = 0;
return -1; // Assuming -1 is not a valid j = 0;
element in the queue.
k = left;
}
while (i < n1 && j < n2) {
}
if (L[i] <= R[j]) {
arr[k] = L[i];
void queueExample() {
i++;
struct Queue queue;
} else {
initializeQueue(&queue);
arr[k] = R[j];
enqueue(&queue, 5);
j++;
enqueue(&queue, 10);
}
enqueue(&queue, 15);
k++;
printf("Queue: ");
}
while (!isQueueEmpty(&queue)) {
printf("%d -> ", dequeue(&queue));
while (i < n1) {
}
arr[k] = L[i];
printf("NULL\n");
i++;
}
k++;
}
}
while (j < n2) { printf("\n");
arr[k] = R[j]; }
j++;
k++; // Complete Example with All Sections
} int main() {
} printf("Basic Data Types Example:\n");
basicDataTypesExample();
void mergeSort(int arr[], int left, int right) { printf("\n");
if (left < right) {
int mid = left + (right - left) / 2; printf("Arrays Example:\n");
arrayExample();
mergeSort(arr, left, mid); printf("\n");
mergeSort(arr, mid + 1, right);
printf("Pointers and Memory Allocation
Example:\n");
merge(arr, left, mid, right);
pointerAndMemoryExample();
}
printf("\n");
}

printf("Control Flow Example:\n");


void mergeSortExample() {
controlFlowExample();
int arr[] = {38, 27, 43, 3, 9, 82, 10};
printf("\n");
int n = sizeof(arr) / sizeof(arr[0]);
printf("Before Merge Sort: ");
printf("Functions Example:\n");
for (int i = 0; i < n; i++) {
functionExample();
printf("%d ", arr[i]);
printf("\n");
}
printf("\n");
printf("Linked List Example:\n");
linkedListExample();
mergeSort(arr, 0, n - 1);
printf("\n");
printf("After Merge Sort: ");
for (int i = 0; i < n; i++) {
printf("Stack Example:\n");
printf("%d ", arr[i]);
stackExample();
printf("\n");

printf("Binary Search Tree Example:\n");


binarySearchTreeExample();
printf("\n");

printf("Queue Example:\n");
queueExample();
printf("\n");

printf("Merge Sort Example:\n");


mergeSortExample();
printf("\n");

return 0; // Returning 0 to indicate


successful program execution.
}

You might also like