C Programming Language || Hands On Coding
前往频道在 Telegram
Hands-on C programming language challenges for beginners. Learn building logic by solving programs. Owner: @Pradeep_saii
显示更多📈 Telegram 频道 C Programming Language || Hands On Coding 的分析概览
频道 C Programming Language || Hands On Coding (@c_programming_language_coding) 英语 语言赛道中的 是活跃参与者。目前社区聚集了 12 824 名订阅者,在 技术与应用 类别中位列第 9 562,并在 印度 地区排名第 31 207 位。
📊 受众指标与增长动态
自 невідомо 创建以来,项目保持高速增长,吸引了 12 824 名订阅者。
根据 26 八月, 2026 的最新数据,频道保持稳定运转。过去 30 天订阅人数变化为 -210,过去 24 小时变化为 -2,整体触达仍然可观。
- 认证状态: 未认证
- 互动率 (ER): 平均受众互动率为 12.56%。内容发布后 24 小时内通常能获得 2.42% 的反应,占订阅者总量。
- 帖子覆盖: 每篇帖子平均可获得 1 612 次浏览,首日通常累积 310 次浏览。
- 互动与反馈: 受众积极参与,单帖平均反应数为 4。
- 主题关注点: 内容集中在 input, string, scanf("%d, array, element 等核心主题上。
📝 描述与内容策略
作者将该频道定位为表达主观观点的平台:
“Hands-on C programming language challenges for beginners. Learn building logic by solving programs.
Owner: @Pradeep_saii”
凭借高频更新(最新数据采集于 27 八月, 2026),频道始终保持新鲜度与高覆盖。分析显示受众积极互动,使其成为 技术与应用 类别中的关键影响点。
12 824
订阅者
-224 小时
-357 天
-21030 天
帖子存档
💻 Implement Stack Using Two Queues
#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->data = (int*)malloc(queue->capacity * sizeof(int));
queue->front = queue->rear = -1;
return queue;
}
int isQueueEmpty(Queue* queue) {
return (queue->front == -1);
}
int isQueueFull(Queue* queue) {
return ((queue->rear + 1) % queue->capacity == queue->front);
}
void enqueue(Queue* queue, int item) {
if (isQueueFull(queue)) {
printf("Queue is fulln");
return;
}
if (isQueueEmpty(queue)) {
queue->front = 0;
}
queue->rear = (queue->rear + 1) % queue->capacity;
queue->data[queue->rear] = item;
}
int dequeue(Queue* queue) {
if (isQueueEmpty(queue)) {
printf("Queue is emptyn");
return -1;
}
int item = queue->data[queue->front];
if (queue->front == queue->rear) {
queue->front = queue->rear = -1;
} else {
queue->front = (queue->front + 1) % queue->capacity;
}
return item;
}
typedef struct {
Queue *q1, *q2;
int capacity;
} Stack;
Stack* createStack(int capacity) {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->capacity = capacity;
stack->q1 = createQueue(capacity);
stack->q2 = createQueue(capacity);
return stack;
}
void push(Stack* stack, int item) {
enqueue(stack->q1, item);
}
int pop(Stack* stack) {
if (isQueueEmpty(stack->q1)) {
printf("Stack is emptyn");
return -1;
}
while (stack->q1->front != stack->q1->rear) {
enqueue(stack->q2, dequeue(stack->q1));
}
int item = dequeue(stack->q1);
Queue* temp = stack->q1;
stack->q1 = stack->q2;
stack->q2 = temp;
return item;
}
int main() {
Stack* stack = createStack(10);
push(stack, 10);
push(stack, 20);
push(stack, 30);
printf("%d popped from stackn", pop(stack));
printf("%d popped from stackn", pop(stack));
printf("%d popped from stackn", pop(stack));
return 0;
}
📤 Output:
30 popped from stack 20 popped from stack 10 popped from stack
💻 Reverse First K Elements of Queue
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int arr[MAX_SIZE];
int front;
int rear;
} Queue;
void initializeQueue(Queue *q) {
q->front = -1;
q->rear = -1;
}
int isEmpty(Queue *q) {
return (q->front == -1);
}
int isFull(Queue *q) {
return ((q->rear + 1) % MAX_SIZE == q->front);
}
void enqueue(Queue *q, int value) {
if (isFull(q)) {
printf("Queue is full!n");
return;
}
if (isEmpty(q)) {
q->front = 0;
}
q->rear = (q->rear + 1) % MAX_SIZE;
q->arr[q->rear] = value;
}
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty!n");
return -1;
}
int value = q->arr[q->front];
if (q->front == q->rear) {
initializeQueue(q);
} else {
q->front = (q->front + 1) % MAX_SIZE;
}
return value;
}
void reverseFirstK(Queue *q, int k) {
if (isEmpty(q) || k > (q->rear - q->front + 1 + MAX_SIZE) % MAX_SIZE || k <= 0) {
return;
}
int stack[k];
for (int i = 0; i < k; i++) {
stack[i] = dequeue(q);
}
for (int i = k - 1; i >= 0; i--) {
enqueue(q, stack[i]);
}
for (int i = 0; i < (q->rear - q->front + 1 + MAX_SIZE) % MAX_SIZE - k; i++) {
enqueue(q, dequeue(q));
}
}
void displayQueue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty!n");
return;
}
printf("Queue: ");
int i = q->front;
while (i != q->rear) {
printf("%d ", q->arr[i]);
i = (i + 1) % MAX_SIZE;
}
printf("%dn", q->arr[q->rear]);
}
int main() {
Queue q;
initializeQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50);
printf("Original ");
displayQueue(&q);
int k = 3;
reverseFirstK(&q, k);
printf("After reversing first %d elements: ", k);
displayQueue(&q);
return 0;
}
📤 Output:
Original Queue: 10 20 30 40 50 After reversing first 3 elements: Queue: 30 20 10 40 50
💻 Generate Binary Numbers from 1 to N
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
char* data;
struct Node* next;
} Node;
typedef struct Queue {
Node* front;
Node* rear;
} Queue;
Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = q->rear = NULL;
return q;
}
void enqueue(Queue* q, char* data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
q->rear->next = newNode;
q->rear = newNode;
}
char* dequeue(Queue* q) {
if (q->front == NULL)
return NULL;
Node* temp = q->front;
char* data = temp->data;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
return data;
}
int main() {
int n;
scanf("%d", &n);
Queue* q = createQueue();
enqueue(q, "1");
for (int i = 0; i < n; i++) {
char* current = dequeue(q);
printf("%s ", current);
char* s1 = (char*)malloc(sizeof(char) * 20);
char* s2 = (char*)malloc(sizeof(char) * 20);
sprintf(s1, "%s0", current);
sprintf(s2, "%s1", current);
enqueue(q, s1);
enqueue(q, s2);
}
printf("n");
return 0;
}
📤 Output:
Input: 5 Output: 1 10 11 100 101
💻 Implement Priority Queue
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct PriorityQueue {
int items[MAX_SIZE];
int priorities[MAX_SIZE];
int size;
};
void initialize(struct PriorityQueue *pq) {
pq->size = 0;
}
int isEmpty(struct PriorityQueue *pq) {
return (pq->size == 0);
}
int isFull(struct PriorityQueue *pq) {
return (pq->size == MAX_SIZE);
}
void enqueue(struct PriorityQueue *pq, int item, int priority) {
if (isFull(pq)) {
printf("Queue is full!n");
return;
}
int i = pq->size - 1;
while (i >= 0 && priority < pq->priorities[i]) {
pq->items[i + 1] = pq->items[i];
pq->priorities[i + 1] = pq->priorities[i];
i--;
}
pq->items[i + 1] = item;
pq->priorities[i + 1] = priority;
pq->size++;
printf("Enqueued item: %d with priority: %dn", item, priority);
}
int dequeue(struct PriorityQueue *pq) {
if (isEmpty(pq)) {
printf("Queue is empty!n");
return -1;
}
int item = pq->items[0];
for (int i = 0; i < pq->size - 1; i++) {
pq->items[i] = pq->items[i + 1];
pq->priorities[i] = pq->priorities[i + 1];
}
pq->size--;
printf("Dequeued item: %dn", item);
return item;
}
void display(struct PriorityQueue *pq) {
if (isEmpty(pq)) {
printf("Queue is empty!n");
return;
}
printf("Queue: ");
for (int i = 0; i < pq->size; i++) {
printf("%d (Priority: %d) ", pq->items[i], pq->priorities[i]);
}
printf("n");
}
int main() {
struct PriorityQueue pq;
initialize(&pq);
enqueue(&pq, 10, 2);
enqueue(&pq, 30, 1);
enqueue(&pq, 20, 3);
display(&pq);
dequeue(&pq);
display(&pq);
return 0;
}
📤 Output:
Enqueued item: 10 with priority: 2 Enqueued item: 30 with priority: 1 Enqueued item: 20 with priority: 3 Queue: 30 (Priority: 1) 10 (Priority: 2) 20 (Priority: 3) Dequeued item: 30 Queue: 10 (Priority: 2) 20 (Priority: 3)
💻 Implement Circular Queue
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
void enqueue(int value) {
if ((rear + 1) % MAX_SIZE == front) {
printf("Queue is fulln");
return;
} else if (front == -1) {
front = 0;
rear = 0;
} else {
rear = (rear + 1) % MAX_SIZE;
}
queue[rear] = value;
printf("Inserted %dn", value);
}
int dequeue() {
int value;
if (front == -1) {
printf("Queue is emptyn");
return -1;
}
value = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
printf("Deleted %dn", value);
return value;
}
void display() {
int i;
if (front == -1) {
printf("Queue is emptyn");
return;
}
printf("Queue elements are:n");
for (i = front; i != rear; i = (i + 1) % MAX_SIZE)
printf("%d ", queue[i]);
printf("%d ", queue[rear]);
printf("n");
}
int main() {
int choice, value;
while (1) {
printf("1. Enqueuen");
printf("2. Dequeuen");
printf("3. Displayn");
printf("4. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choicen");
}
}
return 0;
}
📤 Output:
1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 1 Enter value to enqueue: Input: 10 Output: Inserted 10 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 1 Enter value to enqueue: Input: 20 Output: Inserted 20 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 1 Enter value to enqueue: Input: 30 Output: Inserted 30 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 1 Enter value to enqueue: Input: 40 Output: Inserted 40 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 1 Enter value to enqueue: Input: 50 Output: Inserted 50 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 1 Enter value to enqueue: Input: 60 Output: Queue is full 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 3 Output: Queue elements are: 10 20 30 40 50 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Output: Deleted 10 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 3 Output: Queue elements are: 20 30 40 50 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 1 Enter value to enqueue: Input: 60 Output: Inserted 60 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 3 Output: Queue elements are: 20 30 40 50 60 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Output: Deleted 20 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Output: Deleted 30 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Output: Deleted 40 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Output: Deleted 50 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Output: Deleted 60 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 3 Output: Queue is empty 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Output: Queue is empty 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 4
💻 Implement Queue Using Linked List
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Queue {
Node* front;
Node* rear;
} Queue;
Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
if (q == NULL) {
printf("Memory allocation failedn");
exit(EXIT_FAILURE);
}
q->front = q->rear = NULL;
return q;
}
void enqueue(Queue* q, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failedn");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
q->rear->next = newNode;
q->rear = newNode;
}
int dequeue(Queue* q) {
if (q->front == NULL) {
printf("Queue is emptyn");
return -1;
}
Node* temp = q->front;
int data = temp->data;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
return data;
}
int isEmpty(Queue* q) {
return (q->front == NULL);
}
void displayQueue(Queue* q) {
Node* current = q->front;
if (current == NULL) {
printf("Queue is emptyn");
return;
}
printf("Queue elements: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("n");
}
int main() {
Queue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
displayQueue(q);
printf("Dequeued: %dn", dequeue(q));
printf("Dequeued: %dn", dequeue(q));
displayQueue(q);
if (isEmpty(q)) {
printf("Queue is emptyn");
} else {
printf("Queue is not emptyn");
}
printf("Dequeued: %dn", dequeue(q));
if (isEmpty(q)) {
printf("Queue is emptyn");
} else {
printf("Queue is not emptyn");
}
dequeue(q);
return 0;
}
📤 Output:
Queue elements: 10 20 30 Dequeued: 10 Dequeued: 20 Queue elements: 30 Queue is not empty Dequeued: 30 Queue is empty Queue is empty
💻 Implement Queue Using Array
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
void enqueue(int value) {
if (rear == MAX_SIZE - 1) {
printf("Queue is full!n");
} else {
if (front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("Inserted %d into queuen", value);
}
}
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue is empty!n");
} else {
printf("Deleted element: %dn", queue[front]);
front++;
}
}
void display() {
if (front == -1) {
printf("Queue is empty!n");
} else {
printf("Queue elements are:n");
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("n");
}
}
int main() {
int choice, value;
while (1) {
printf("1. Enqueuen");
printf("2. Dequeuen");
printf("3. Displayn");
printf("4. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choicen");
}
}
return 0;
}
📤 Output:
1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 1 Enter value to enqueue: Input: 10 Inserted 10 into queue 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 1 Enter value to enqueue: Input: 20 Inserted 20 into queue 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 3 Queue elements are: 10 20 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Deleted element: 10 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 3 Queue elements are: 20 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Deleted element: 20 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 3 Queue is empty! 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 2 Queue is empty! 1. Enqueue 2. Dequeue 3. Display 4. Exit Enter your choice: Input: 4
💻 Sort a Stack Using Temporary Stack
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
unsigned capacity;
int *array;
};
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;
}
int pop(struct Stack* stack) {
if (isEmpty(stack))
return -1; // or some error value
return stack->array[stack->top--];
}
int peek(struct Stack* stack) {
if (isEmpty(stack))
return -1; // or some error value
return stack->array[stack->top];
}
void sortStack(struct Stack* stack) {
struct Stack* tempStack = createStack(stack->capacity);
int tmp;
while (!isEmpty(stack)) {
tmp = pop(stack);
while (!isEmpty(tempStack) && peek(tempStack) > tmp) {
push(stack, pop(tempStack));
}
push(tempStack, tmp);
}
while (!isEmpty(tempStack)) {
push(stack, pop(tempStack));
}
free(tempStack->array);
free(tempStack);
}
int main() {
struct Stack* stack = createStack(5);
push(stack, 5);
push(stack, 2);
push(stack, 4);
push(stack, 1);
push(stack, 3);
sortStack(stack);
printf("Sorted stack: ");
while (!isEmpty(stack)) {
printf("%d ", pop(stack));
}
printf("n");
free(stack->array);
free(stack);
return 0;
}
📤 Output:
Sorted stack: 1 2 3 4 5
💻 Check if Stack is Sorted
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 100
struct Stack {
int arr[MAX_SIZE];
int top;
};
void initialize(struct Stack *stack) {
stack->top = -1;
}
bool isEmpty(struct Stack *stack) {
return stack->top == -1;
}
bool isFull(struct Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
void push(struct Stack *stack, int value) {
if (isFull(stack)) {
printf("Stack Overflown");
return;
}
stack->arr[++stack->top] = value;
}
int pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack Underflown");
return -1;
}
return stack->arr[stack->top--];
}
bool isStackSorted(struct Stack *stack) {
if (isEmpty(stack) || stack->top == 0) {
return true;
}
struct Stack tempStack;
initialize(&tempStack);
int temp;
bool sorted = true;
while (!isEmpty(stack)) {
temp = pop(stack);
if (!isEmpty(&tempStack) && tempStack.arr[tempStack.top] < temp) {
sorted = false;
break;
}
while (!isEmpty(&tempStack) && tempStack.arr[tempStack.top] < temp) {
push(stack, pop(&tempStack));
}
push(&tempStack, temp);
}
while (!isEmpty(&tempStack)) {
push(stack, pop(&tempStack));
}
return sorted;
}
int main() {
struct Stack myStack;
initialize(&myStack);
push(&myStack, 5);
push(&myStack, 4);
push(&myStack, 3);
push(&myStack, 2);
push(&myStack, 1);
if (isStackSorted(&myStack)) {
printf("Stack is sorted.n");
} else {
printf("Stack is not sorted.n");
}
return 0;
}
📤 Output:
Stack is sorted.
💻 Implement Two Stacks in One Array
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int array[SIZE];
int top1 = -1;
int top2 = SIZE;
void push1(int data) {
if (top1 < top2 - 1) {
top1++;
array[top1] = data;
} else {
printf("Stack 1 is fulln");
}
}
void push2(int data) {
if (top1 < top2 - 1) {
top2--;
array[top2] = data;
} else {
printf("Stack 2 is fulln");
}
}
int pop1() {
if (top1 >= 0) {
int popped = array[top1];
top1--;
return popped;
} else {
printf("Stack 1 is emptyn");
return -1;
}
}
int pop2() {
if (top2 < SIZE) {
int popped = array[top2];
top2++;
return popped;
} else {
printf("Stack 2 is emptyn");
return -1;
}
}
int main() {
push1(10);
push1(20);
push2(30);
push2(40);
printf("Popped from Stack 1: %dn", pop1());
printf("Popped from Stack 2: %dn", pop2());
return 0;
}
📤 Output:
Popped from Stack 1: 20 Popped from Stack 2: 40
💻 Reverse a List Using Stack
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct Stack {
int top;
int 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, int data) {
if (isFull(s)) {
printf("Stack Overflow n");
return;
}
s->items[++s->top] = data;
}
int pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack Underflow n");
return -1;
}
return s->items[s->top--];
}
int main() {
struct Stack s;
initialize(&s);
int n, i, num;
printf("Enter the number of elements in the list: ");
scanf("%d", &n);
int list[n];
printf("Enter the elements of the list:n");
for (i = 0; i < n; i++) {
scanf("%d", &list[i]);
}
for (i = 0; i < n; i++) {
push(&s, list[i]);
}
printf("Reversed List:n");
for (i = 0; i < n; i++) {
list[i] = pop(&s);
printf("%d ", list[i]);
}
printf("n");
return 0;
}
📤 Output:
Input: 5 Input: 1 Input: 2 Input: 3 Input: 4 Input: 5 Output: Enter the number of elements in the list: Enter the elements of the list: Reversed List: 5 4 3 2 1
💻 Reverse a String Using Stack
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct Stack {
char items[MAX_SIZE];
int top;
};
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 Overflown");
return;
}
s->items[++s->top] = c;
}
char pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack Underflown");
return '0';
}
return s->items[s->top--];
}
int main() {
char str[MAX_SIZE];
struct Stack s;
initialize(&s);
printf("Enter a string: ");
scanf("%s", str);
int len = strlen(str);
for (int i = 0; i < len; i++) {
push(&s, str[i]);
}
printf("Reversed string: ");
for (int i = 0; i < len; i++) {
printf("%c", pop(&s));
}
printf("n");
return 0;
}
📤 Output:
Input: hello Output: Enter a string: Reversed string: olleh
💻 Stock Span Problem
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of days: ");
scanf("%d", &n);
int prices[n];
printf("Enter the stock prices for each day:n");
for (int i = 0; i < n; i++) {
scanf("%d", &prices[i]);
}
int span[n];
for (int i = 0; i < n; i++) {
span[i] = 1; // Initialize span to 1
for (int j = i - 1; j >= 0 && prices[j] <= prices[i]; j--) {
span[i]++;
}
}
printf("Stock Span values:n");
for (int i = 0; i < n; i++) {
printf("%d ", span[i]);
}
printf("n");
return 0;
}
📤 Output:
Input: 7 Input: 100 Input: 80 Input: 60 Input: 70 Input: 60 Input: 75 Input: 85 Output: Enter the number of days: Enter the stock prices for each day: Stock Span values: 1 1 1 2 1 4 6
💻 Next Smaller Element
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int nextSmaller[n];
for (int i = 0; i < n; i++) {
nextSmaller[i] = -1;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[i]) {
nextSmaller[i] = arr[j];
break;
}
}
}
printf("Next Smaller Element:n");
for (int i = 0; i < n; i++) {
printf("%d ", nextSmaller[i]);
}
printf("n");
return 0;
}
📤 Output:
Input: 5 Input: 5 Input: 4 Input: 3 Input: 2 Input: 1 Output: Enter the number of elements: Enter the elements: Next Smaller Element: 4 3 2 1 -1 Input: 4 Input: 1 Input: 3 Input: 2 Input: 4 Output: Enter the number of elements: Enter the elements: Next Smaller Element: -1 2 -1 -1
💻 Next Greater Element
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int nextGreater[n];
for (int i = 0; i < n; i++) {
nextGreater[i] = -1;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[i]) {
nextGreater[i] = arr[j];
break;
}
}
}
printf("Next Greater Elements are:n");
for (int i = 0; i < n; i++) {
printf("%d ", nextGreater[i]);
}
printf("n");
return 0;
}
📤 Output:
Input: 5 Input: 16 7 2 8 9 Output: Enter the number of elements: Enter the elements: Next Greater Elements are: -1 8 8 9 -1
💻 Evaluate Prefix Expression
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void push(int value) {
stack[++top] = value;
}
int pop() {
return stack[top--];
}
int evaluatePrefix(char* expression) {
int i, operand1, operand2, result;
for (i = strlen(expression) - 1; i >= 0; i--) {
if (isdigit(expression[i])) {
push(expression[i] - '0');
} else {
operand1 = pop();
operand2 = pop();
switch (expression[i]) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
default:
printf("Invalid operatorn");
return -1;
}
push(result);
}
}
return pop();
}
int main() {
char expression[MAX_SIZE];
printf("Enter prefix expression: ");
scanf("%s", expression);
int result = evaluatePrefix(expression);
if (result != -1) {
printf("Result: %dn", result);
}
return 0;
}
📤 Output:
Input: +*234 Output: Result: 10 Input: -+*23/825 Output: Result: 6
💻 Evaluate Postfix Expression
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void push(int value) {
stack[++top] = value;
}
int pop() {
return stack[top--];
}
int evaluatePostfix(char* expression) {
int i = 0;
while (expression[i] != '0') {
if (isdigit(expression[i])) {
int num = 0;
while(isdigit(expression[i])){
num = num * 10 + (expression[i] - '0');
i++;
}
i--;
push(num);
} else if (expression[i] == '+' || expression[i] == '-' ||
expression[i] == '*' || expression[i] == '/') {
int operand2 = pop();
int operand1 = pop();
switch (expression[i]) {
case '+':
push(operand1 + operand2);
break;
case '-':
push(operand1 - operand2);
break;
case '*':
push(operand1 * operand2);
break;
case '/':
push(operand1 / operand2);
break;
}
}
i++;
}
return pop();
}
int main() {
char expression[MAX_SIZE];
printf("Enter postfix expression: ");
scanf("%s", expression);
int result = evaluatePostfix(expression);
printf("Result: %dn", result);
return 0;
}
📤 Output:
Input: 23+ Output: Result: 5 Input: 123+* Output: Result: 6 Input: 567*+ Output: Result: 47 Input: 102/ Output: Result: 5 Input: 53- Output: Result: 2 Input: 105+2* Output: Result: 30 Input: 231*+9- Output: Result: -4 Input: 123+4*+ Enter postfix expression: 123+4*+ Result: 20
💻 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
💻 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^^
