Code With SaiPradeep
前往频道在 Telegram
A space for curious minds to explore software engineering, computer science, programming and technology. Learn. Explore. Understand.
显示更多📈 Telegram 频道 Code With SaiPradeep 的分析概览
频道 Code With SaiPradeep (@codewithsaipradeep) 英语 语言赛道中的 是活跃参与者。目前社区聚集了 12 333 名订阅者,在 技术与应用 类别中位列第 10 186,并在 印度 地区排名第 31 189 位。
📊 受众指标与增长动态
自 невідомо 创建以来,项目保持高速增长,吸引了 12 333 名订阅者。
根据 11 十一月, 2026 的最新数据,频道保持稳定运转。过去 30 天订阅人数变化为 -238,过去 24 小时变化为 -16,整体触达仍然可观。
- 认证状态: 未认证
- 互动率 (ER): 平均受众互动率为 10.07%。内容发布后 24 小时内通常能获得 N/A% 的反应,占订阅者总量。
- 帖子覆盖: 每篇帖子平均可获得 1 242 次浏览,首日通常累积 0 次浏览。
- 互动与反馈: 受众积极参与,单帖平均反应数为 11。
- 主题关注点: 内容集中在 input, string, scanf("%d, array, element 等核心主题上。
📝 描述与内容策略
作者将该频道定位为表达主观观点的平台:
“A space for curious minds to explore software engineering, computer science, programming and technology.
Learn. Explore. Understand.”
凭借高频更新(最新数据采集于 12 十一月, 2026),频道始终保持新鲜度与高覆盖。分析显示受众积极互动,使其成为 技术与应用 类别中的关键影响点。
12 333
订阅者
-1624 小时
-807 天
-23830 天
帖子存档
12 331
💻 Infix to Prefix Conversion
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
char stack[MAX_SIZE];
int top = -1;
void push(char c) {
if (top == MAX_SIZE - 1) {
printf("Stack Overflown");
return;
}
stack[++top] = c;
}
char pop() {
if (top == -1) {
return -1; // Indicates an empty stack
}
return stack[top--];
}
int precedence(char operator) {
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}
void reverseString(char *str) {
int len = strlen(str);
for (int i = 0, j = len - 1; i < j; i++, j--) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
void infixToPrefix(char *infix, char *prefix) {
int i, j = 0;
int len = strlen(infix);
reverseString(infix);
for (i = 0; i < len; i++) {
if (infix[i] == '(') {
infix[i] = ')';
} else if (infix[i] == ')') {
infix[i] = '(';
}
}
for (i = 0; i < len; i++) {
if (isalnum(infix[i])) {
prefix[j++] = infix[i];
} else if (infix[i] == '(') {
push(infix[i]);
} else if (infix[i] == ')') {
while (top != -1 && stack[top] != '(') {
prefix[j++] = pop();
}
if (top != -1 && stack[top] == '(') {
pop();
}
} else {
while (top != -1 && precedence(infix[i]) <= precedence(stack[top])) {
prefix[j++] = pop();
}
push(infix[i]);
}
}
while (top != -1) {
prefix[j++] = pop();
}
prefix[j] = '0';
reverseString(prefix);
}
int main() {
char infix[MAX_SIZE];
char prefix[MAX_SIZE];
printf("Enter infix expression: ");
scanf("%s", infix);
infixToPrefix(infix, prefix);
printf("Prefix expression: %sn", prefix);
return 0;
}
📤 Output:
Input: a+b*c Output: Prefix expression: +a*bc Input: (a+b)*c Output: Prefix expression: *+abc Input: a+b*(c^d-e)^(f+g*h)-i Output: Prefix expression: -+a*b^-^cde+f*ghi Input: A*(B+C)/D Output: Prefix expression: /*A+BCD
12 331
💻 Infix to Postfix Conversion
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
char stack[MAX_SIZE];
int top = -1;
void push(char item) {
stack[++top] = item;
}
char pop() {
if (top == -1) {
return -1;
}
return stack[top--];
}
int precedence(char operator) {
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}
int main() {
char infix[MAX_SIZE], postfix[MAX_SIZE];
int i, j = 0;
printf("Enter infix expression: ");
scanf("%s", infix);
for (i = 0; infix[i] != '0'; i++) {
if (isalnum(infix[i])) {
postfix[j++] = infix[i];
} else if (infix[i] == '(') {
push(infix[i]);
} else if (infix[i] == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = pop();
}
pop(); // Remove the '('
} else {
while (top != -1 && precedence(infix[i]) <= precedence(stack[top])) {
postfix[j++] = pop();
}
push(infix[i]);
}
}
while (top != -1) {
postfix[j++] = pop();
}
postfix[j] = '0';
printf("Postfix expression: %sn", postfix);
return 0;
}
📤 Output:
Input: a+b*c Output: abc*+ Input: (a+b)*c Output: ab+c* Input: a+b*(c^d-e)^(f+g*h)-i Output: abcd^e-fgh*+^*+i- Input: a*b+c/d Output: ab*cd/+ Input: a^b^c Output: abc^^
12 331
💻 Check for Balanced Brackets
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
char data[MAX_SIZE];
int top;
} Stack;
void initialize(Stack *stack) {
stack->top = -1;
}
int isEmpty(Stack *stack) {
return stack->top == -1;
}
int isFull(Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
void push(Stack *stack, char c) {
if (!isFull(stack)) {
stack->data[++stack->top] = c;
}
}
char pop(Stack *stack) {
if (!isEmpty(stack)) {
return stack->data[stack->top--];
}
return '0';
}
int isMatchingPair(char character1, char character2) {
if (character1 == '(' && character2 == ')')
return 1;
else if (character1 == '{' && character2 == '}')
return 1;
else if (character1 == '[' && character2 == ']')
return 1;
else
return 0;
}
int isBalanced(char *expression) {
Stack stack;
initialize(&stack);
int i;
for (i = 0; expression[i]; i++) {
if (expression[i] == '(' || expression[i] == '{' || expression[i] == '[')
push(&stack, expression[i]);
if (expression[i] == ')' || expression[i] == '}' || expression[i] == ']') {
if (isEmpty(&stack))
return 0;
char top = pop(&stack);
if (!isMatchingPair(top, expression[i]))
return 0;
}
}
return isEmpty(&stack);
}
int main() {
char expression[MAX_SIZE];
printf("Enter an expression: ");
scanf("%s", expression);
if (isBalanced(expression))
printf("Balancedn");
else
printf("Not Balancedn");
return 0;
}
📤 Output:
Input: {([])}
Output: Balanced
Input: ([)]
Output: Not Balanced
Input: ((
Output: Not Balanced
Input: ))
Output: Not Balanced
Input: abc
Output: Balanced
Input: {a[b(c)d]e}
Output: Balanced
Input: {a[b(c)d]e
Output: Not Balanced12 331
💻 Check for Balanced Parentheses
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
struct Stack {
int top;
char items[MAX_SIZE];
};
void initialize(struct Stack *s) {
s->top = -1;
}
int isEmpty(struct Stack *s) {
return (s->top == -1);
}
int isFull(struct Stack *s) {
return (s->top == MAX_SIZE - 1);
}
void push(struct Stack *s, char c) {
if (isFull(s)) {
printf("Stack Overflow n");
return;
}
s->items[++(s->top)] = c;
}
char pop(struct Stack *s) {
if (isEmpty(s)) {
return '0';
}
return s->items[(s->top)--];
}
int isMatchingPair(char character1, char character2) {
if (character1 == '(' && character2 == ')')
return 1;
else if (character1 == '{' && character2 == '}')
return 1;
else if (character1 == '[' && character2 == ']')
return 1;
else
return 0;
}
int isBalanced(char exp[]) {
struct Stack s;
initialize(&s);
for (int i = 0; exp[i]; i++) {
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
push(&s, exp[i]);
if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']') {
if (isEmpty(&s))
return 0;
char top = pop(&s);
if (!isMatchingPair(top, exp[i]))
return 0;
}
}
return (isEmpty(&s) ? 1 : 0);
}
int main() {
char exp[MAX_SIZE];
printf("Enter an expression: ");
scanf("%s", exp);
if (isBalanced(exp))
printf("Balanced n");
else
printf("Not Balanced n");
return 0;
}
📤 Output:
Input: {([])}
Output: Balanced
Input: ([)]
Output: Not Balanced
Input: ((
Output: Not Balanced
Input: ))
Output: Not Balanced
Input: abc
Output: Balanced
Input: {[()]}
Output: Balanced12 331
💻 Implement Stack Using Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node *top = NULL;
void push(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Stack Overflown");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d pushed to stackn", value);
}
void pop() {
if (top == NULL) {
printf("Stack Underflown");
return;
}
struct Node *temp = top;
top = top->next;
printf("%d popped from stackn", temp->data);
free(temp);
}
void peek() {
if (top == NULL) {
printf("Stack is emptyn");
return;
}
printf("Top element is %dn", top->data);
}
int isEmpty() {
return top == NULL;
}
int main() {
int choice, value;
while (1) {
printf("nStack Operations:n");
printf("1. Pushn");
printf("2. Popn");
printf("3. Peekn");
printf("4. IsEmptyn");
printf("5. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
if (isEmpty())
printf("Stack is emptyn");
else
printf("Stack is not emptyn");
break;
case 5:
exit(0);
default:
printf("Invalid choicen");
}
}
return 0;
}
📤 Output:
Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: 1 Input: 10 Enter value to push: 10 Output: 10 pushed to stack Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: 1 Input: 20 Enter value to push: 20 Output: 20 pushed to stack Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: 3 Output: Top element is 20 Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: 2 Output: 20 popped from stack Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: 4 Output: Stack is not empty Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: 2 Output: 10 popped from stack Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: 4 Output: Stack is empty Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: 2 Output: Stack Underflow Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: 5
12 331
💻 Implement Stack Using Array
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void push(int value) {
if (top >= MAX_SIZE - 1) {
printf("Stack Overflown");
return;
}
top++;
stack[top] = value;
printf("%d pushed to stackn", value);
}
void pop() {
if (top == -1) {
printf("Stack Underflown");
return;
}
printf("%d popped from stackn", stack[top]);
top--;
}
void peek() {
if (top == -1) {
printf("Stack is emptyn");
return;
}
printf("Top element is %dn", stack[top]);
}
int isEmpty() {
return top == -1;
}
int main() {
int choice, value;
while (1) {
printf("nStack Operations:n");
printf("1. Pushn");
printf("2. Popn");
printf("3. Peekn");
printf("4. IsEmptyn");
printf("5. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
if (isEmpty()) {
printf("Stack is emptyn");
} else {
printf("Stack is not emptyn");
}
break;
case 5:
exit(0);
default:
printf("Invalid choicen");
}
}
return 0;
}
📤 Output:
Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: Input: 1 Enter value to push: Input: 10 10 pushed to stack Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: Input: 1 Enter value to push: Input: 20 20 pushed to stack Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: Input: 3 Top element is 20 Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: Input: 4 Stack is not empty Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: Input: 2 20 popped from stack Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: Input: 2 10 popped from stack Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: Input: 2 Stack Underflow Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: Input: 4 Stack is empty Stack Operations: 1. Push 2. Pop 3. Peek 4. IsEmpty 5. Exit Enter your choice: Input: 5
12 331
💻 Command Line Arguments Processing
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int num_args = argc;
if (num_args == 1) {
printf("No command-line arguments provided.n");
} else {
printf("Number of command-line arguments: %dn", num_args);
printf("Program name: %sn", argv[0]);
// Dynamically allocate memory to store the arguments
char **args = (char **)malloc((num_args - 1) * sizeof(char *));
if (args == NULL) {
printf("Memory allocation failed.n");
return 1;
}
printf("Other arguments:n");
for (int i = 1; i < num_args; i++) {
int arg_len = 0;
while (argv[i][arg_len] != '0') {
arg_len++;
}
args[i - 1] = (char *)malloc((arg_len + 1) * sizeof(char));
if (args[i - 1] == NULL) {
printf("Memory allocation failed.n");
// Free previously allocated memory
for(int j = 0; j < i - 1; j++) {
free(args[j]);
}
free(args);
return 1;
}
int j = 0;
while (argv[i][j] != '0') {
args[i - 1][j] = argv[i][j];
j++;
}
args[i - 1][j] = '0';
printf("Argument %d: %sn", i, args[i - 1]);
}
// Free the dynamically allocated memory
for (int i = 0; i < num_args - 1; i++) {
free(args[i]);
}
free(args);
}
return 0;
}
📤 Output:
Number of command-line arguments: 4 Program name: ./program Other arguments: Argument 1: hello Argument 2: world Argument 3: 123
12 331
💻 Array of Dynamic Strings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int numStrings;
printf("Enter the number of strings: ");
scanf("%d", &numStrings);
getchar();
char **stringArray = (char **)malloc(numStrings * sizeof(char *));
if (stringArray == NULL) {
printf("Memory allocation failed.n");
return 1;
}
for (int i = 0; i < numStrings; i++) {
int stringLength;
printf("Enter the length of string %d: ", i + 1);
scanf("%d", &stringLength);
getchar();
stringArray[i] = (char *)malloc((stringLength + 1) * sizeof(char));
if (stringArray[i] == NULL) {
printf("Memory allocation failed for string %d.n", i + 1);
for (int j = 0; j < i; j++) {
free(stringArray[j]);
}
free(stringArray);
return 1;
}
printf("Enter string %d: ", i + 1);
fgets(stringArray[i], stringLength + 1, stdin);
stringArray[i][strcspn(stringArray[i], "n")] = 0;
}
printf("nEntered strings:n");
for (int i = 0; i < numStrings; i++) {
printf("String %d: %sn", i + 1, stringArray[i]);
}
for (int i = 0; i < numStrings; i++) {
free(stringArray[i]);
}
free(stringArray);
return 0;
}
📤 Output:
Input: 2 Input: 5 Input: hello Input: 3 Input: bye Output: Enter the number of strings: Enter the length of string 1: Enter string 1: Enter the length of string 2: Enter string 2: Entered strings: String 1: hello String 2: bye
12 331
💻 Dynamic Structure Allocation
#include <stdio.h>
#include <stdlib.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person *personPtr;
personPtr = (struct Person *)malloc(sizeof(struct Person));
if (personPtr == NULL) {
printf("Memory allocation failed!n");
return 1;
}
printf("Enter name: ");
scanf("%s", personPtr->name);
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Name: %s, Age: %dn", personPtr->name, personPtr->age);
free(personPtr);
return 0;
}
📤 Output:
Input: John Input: 30 Output: Enter name: Enter age: Name: John, Age: 30
12 331
💻 Memory Pool Implementation
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define POOL_SIZE 1024
#define BLOCK_SIZE 32
typedef struct Block {
bool is_free;
struct Block *next;
} Block;
char memory_pool[POOL_SIZE];
Block *free_list;
void initialize_pool() {
int num_blocks = POOL_SIZE / BLOCK_SIZE;
Block *current_block = (Block *)memory_pool;
for (int i = 0; i < num_blocks; i++) {
current_block->is_free = true;
current_block->next = (Block *)((char *)current_block + BLOCK_SIZE);
current_block = current_block->next;
}
((Block *)((char *)current_block - BLOCK_SIZE))->next = NULL;
free_list = (Block *)memory_pool;
}
void *allocate_memory() {
if (free_list == NULL) {
printf("Memory pool is empty.n");
return NULL;
}
Block *allocated_block = free_list;
free_list = free_list->next;
allocated_block->is_free = false;
return (void *)((char *)allocated_block + sizeof(Block));
}
void deallocate_memory(void *ptr) {
if (ptr == NULL) return;
Block *block_to_free = (Block *)((char *)ptr - sizeof(Block));
block_to_free->is_free = true;
block_to_free->next = free_list;
free_list = block_to_free;
}
int main() {
initialize_pool();
int *num1 = (int *)allocate_memory();
if (num1 != NULL) {
*num1 = 42;
printf("Allocated memory: %dn", *num1);
}
int *num2 = (int *)allocate_memory();
if (num2 != NULL) {
*num2 = 99;
printf("Allocated memory: %dn", *num2);
}
deallocate_memory(num1);
deallocate_memory(num2);
return 0;
}
📤 Output:
Allocated memory: 42 Allocated memory: 99
12 331
💻 Custom Memory Allocator
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MEMORY_SIZE 1024
static char memory[MEMORY_SIZE];
static bool memory_map[MEMORY_SIZE];
void* my_malloc(size_t size) {
if (size <= 0 || size > MEMORY_SIZE) {
return NULL;
}
for (int i = 0; i <= MEMORY_SIZE - size; i++) {
bool found = true;
for (int j = 0; j < size; j++) {
if (memory_map[i + j]) {
found = false;
break;
}
}
if (found) {
for (int j = 0; j < size; j++) {
memory_map[i + j] = true;
}
return (void*)&memory[i];
}
}
return NULL;
}
void my_free(void* ptr, size_t size) {
if (ptr == NULL) {
return;
}
char* start = (char*)memory;
char* current = (char*)ptr;
if (current < start || current >= start + MEMORY_SIZE) {
return;
}
size_t offset = current - start;
for (int i = 0; i < size; i++) {
memory_map[offset + i] = false;
}
}
int main() {
int* ptr1 = (int*)my_malloc(sizeof(int) * 5);
if (ptr1 == NULL) {
printf("Memory allocation failed!n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr1[i] = i * 2;
}
printf("Allocated memory at: %pn", (void*)ptr1);
for (int i = 0; i < 5; i++) {
printf("ptr1[%d] = %dn", i, ptr1[i]);
}
my_free(ptr1, sizeof(int) * 5);
printf("Memory freed.n");
return 0;
}
📤 Output:
Allocated memory at: 0x602000000000 ptr1[0] = 0 ptr1[1] = 2 ptr1[2] = 4 ptr1[3] = 6 ptr1[4] = 8 Memory freed.
12 331
💻 Memory Leak Detection
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!n");
return 1;
}
printf("Memory allocated at address: %pn", (void *)ptr);
printf("Enter an integer value: ");
scanf("%d", ptr);
printf("Value stored: %dn", *ptr);
// free(ptr); //Uncomment this to prevent memory leak
ptr = NULL; //Setting to NULL after free is a good practice
return 0;
}
📤 Output:
Memory allocated at address: 0x12345678 Enter an integer value: 10 Value stored: 10
12 331
💻 Dynamic Array Resizing
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int size = 2;
int count = 0;
int num;
arr = (int *)malloc(size * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.n");
return 1;
}
printf("Enter numbers (enter -1 to stop):n");
while (1) {
scanf("%d", &num);
if (num == -1) {
break;
}
if (count == size) {
size *= 2;
arr = (int *)realloc(arr, size * sizeof(int));
if (arr == NULL) {
printf("Memory reallocation failed.n");
return 1;
}
}
arr[count] = num;
count++;
}
printf("The numbers you entered are:n");
for (int i = 0; i < count; i++) {
printf("%d ", arr[i]);
}
printf("n");
free(arr);
return 0;
}
📤 Output:
Input: 1 Input: 2 Input: 3 Input: 4 Input: 5 Input: -1 Output: Enter numbers (enter -1 to stop): The numbers you entered are: 1 2 3 4 5
12 331
💻 Binary Tree Implementation
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
struct Node *createNode(int data) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void inorderTraversal(struct Node *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
struct Node *root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
printf("Inorder traversal of the binary tree is: ");
inorderTraversal(root);
printf("n");
return 0;
}
📤 Output:
Inorder traversal of the binary tree is: 4 2 5 1 3
12 331
💻 Queue Implementation using Dynamic Memory
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *data;
int front;
int rear;
int capacity;
} Queue;
Queue* createQueue(int capacity) {
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->capacity = capacity;
queue->front = 0;
queue->rear = -1;
queue->data = (int*)malloc(queue->capacity * sizeof(int));
return queue;
}
int isFull(Queue* queue) {
return (queue->rear == queue->capacity - 1);
}
int isEmpty(Queue* queue) {
return (queue->front > queue->rear);
}
void enqueue(Queue* queue, int item) {
if (isFull(queue)) {
printf("Queue is fulln");
return;
}
queue->rear = queue->rear + 1;
queue->data[queue->rear] = item;
printf("%d enqueued to queuen", item);
}
int dequeue(Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is emptyn");
return -1;
}
int item = queue->data[queue->front];
queue->front = queue->front + 1;
return item;
}
int main() {
Queue* queue = createQueue(5);
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
enqueue(queue, 50);
printf("%d dequeued from queuen", dequeue(queue));
printf("%d dequeued from queuen", dequeue(queue));
printf("%d dequeued from queuen", dequeue(queue));
free(queue->data);
free(queue);
return 0;
}
📤 Output:
10 enqueued to queue 20 enqueued to queue 30 enqueued to queue 40 enqueued to queue 50 enqueued to queue 10 dequeued from queue 20 dequeued from queue 30 dequeued from queue
12 331
💻 Stack Implementation using Dynamic Memory
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int *array;
int top;
unsigned capacity;
};
struct Stack* createStack(unsigned capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack->capacity * sizeof(int));
return stack;
}
int isFull(struct Stack* stack) {
return stack->top == stack->capacity - 1;
}
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
void push(struct Stack* stack, int item) {
if (isFull(stack))
return;
stack->array[++stack->top] = item;
printf("%d pushed to stackn", item);
}
int pop(struct Stack* stack) {
if (isEmpty(stack))
return -1;
return stack->array[stack->top--];
}
int peek(struct Stack* stack) {
if (isEmpty(stack))
return -1;
return stack->array[stack->top];
}
int main() {
struct Stack* stack = createStack(10);
push(stack, 10);
push(stack, 20);
push(stack, 30);
printf("%d popped from stackn", pop(stack));
printf("Top element is %dn", peek(stack));
return 0;
}
📤 Output:
10 pushed to stack 20 pushed to stack 30 pushed to stack 30 popped from stack Top element is 20
12 331
💻 Linked List Implementation
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node *head = NULL;
struct Node *temp = NULL;
int choice, value;
while (1) {
printf("1. Insert at beginningn");
printf("2. Displayn");
printf("3. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter the value to insert: ");
scanf("%d", &value);
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
} else if (choice == 2) {
temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("n");
} else if (choice == 3) {
break;
} else {
printf("Invalid choice.n");
}
}
temp = head;
while (temp != NULL) {
struct Node *next = temp->next;
free(temp);
temp = next;
}
head = NULL;
return 0;
}
📤 Output:
1. Insert at beginning 2. Display 3. Exit Enter your choice: 1 Enter the value to insert: 10 1. Insert at beginning 2. Display 3. Exit Enter your choice: 1 Enter the value to insert: 20 1. Insert at beginning 2. Display 3. Exit Enter your choice: 2 Linked List: 20 10 1. Insert at beginning 2. Display 3. Exit Enter your choice: 1 Enter the value to insert: 30 1. Insert at beginning 2. Display 3. Exit Enter your choice: 2 Linked List: 30 20 10 1. Insert at beginning 2. Display 3. Exit Enter your choice: 3
12 331
💻 String Operations with Dynamic Memory
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str1 = NULL, *str2 = NULL, *combinedStr = NULL;
int len1, len2;
printf("Enter the length of the first string: ");
scanf("%d", &len1);
str1 = (char *)malloc((len1 + 1) * sizeof(char));
if (str1 == NULL) {
printf("Memory allocation failedn");
return 1;
}
printf("Enter the first string: ");
scanf(" %[^n]", str1);
printf("Enter the length of the second string: ");
scanf("%d", &len2);
str2 = (char *)malloc((len2 + 1) * sizeof(char));
if (str2 == NULL) {
printf("Memory allocation failedn");
free(str1);
return 1;
}
printf("Enter the second string: ");
scanf(" %[^n]", str2);
combinedStr = (char *)malloc((len1 + len2 + 1) * sizeof(char));
if (combinedStr == NULL) {
printf("Memory allocation failedn");
free(str1);
free(str2);
return 1;
}
strcpy(combinedStr, str1);
strcat(combinedStr, str2);
printf("Combined string: %sn", combinedStr);
free(str1);
free(str2);
free(combinedStr);
return 0;
}
📤 Output:
Input: 5 Input: Hello Input: 3 Input: World Output: Enter the length of the first string: Enter the first string: Enter the length of the second string: Enter the second string: Combined string: HelloWorld
12 331
💻 Matrix Operations with Dynamic Allocation
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows, cols;
int i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int **matrix = (int **)malloc(rows * sizeof(int *));
if (matrix == NULL) {
printf("Memory allocation failed.n");
return 1;
}
for (i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
if (matrix[i] == NULL) {
printf("Memory allocation failed.n");
// Free previously allocated memory
for (j = 0; j < i; j++) {
free(matrix[j]);
}
free(matrix);
return 1;
}
}
printf("Enter the elements of the matrix:n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("The matrix is:n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("n");
}
// Free the allocated memory
for (i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
return 0;
}
📤 Output:
Input: 2 Input: 3 Input: 1 Input: 2 Input: 3 Input: 4 Input: 5 Input: 6 Output: Enter the number of rows: Enter the number of columns: Enter the elements of the matrix: The matrix is: 1 2 3 4 5 6
