fa
Feedback
C Programming Codes

C Programming Codes

رفتن به کانال در Telegram

C Programming Codes || Quizzes || DSA Learn along with the community Any queries admin - @Pradeep_saii

نمایش بیشتر

📈 تحلیل کانال تلگرام C Programming Codes

کانال C Programming Codes (@c_programming_codes) در بخش زبانی انگلیسی بازیگری فعال است. در حال حاضر جامعه شامل 13 433 مشترک است و جایگاه 9 525 را در دسته فناوری و برنامه‌ها و رتبه 32 112 را در منطقه الهند دارد.

📊 شاخص‌های مخاطب و پویایی

از زمان ایجاد در невідомо، پروژه رشد سریعی داشته و 13 433 مشترک جذب کرده است.

بر اساس آخرین داده‌ها در تاریخ 10 ژوئن, 2026، کانال فعالیت پایداری دارد. در ۳۰ روز گذشته تغییر اعضا برابر -232 و در ۲۴ ساعت گذشته برابر -3 بوده و همچنان دسترسی گسترده‌ای حفظ شده است.

  • وضعیت تأیید: تأیید نشده
  • نرخ تعامل (ER): میانگین تعامل مخاطب 9.77% است و در ۲۴ ساعت نخست پس از انتشار، محتوا معمولاً N/A% واکنش نسبت به کل مشترکان کسب می‌کند.
  • دسترسی پست‌ها: هر پست به طور میانگین 0 بازدید دریافت می‌کند. در اولین روز معمولاً 0 بازدید جمع‌آوری می‌شود.
  • واکنش‌ها و تعامل: مخاطبان به‌طور فعال حمایت می‌کنند؛ میانگین واکنش به هر پست 0 است.
  • علایق موضوعی: محتوا بر موضوعات کلیدی مانند input, string, scanf("%d, array, element تمرکز دارد.

📝 توضیح و سیاست محتوایی

نویسنده این فضا را محل بیان دیدگاه‌های شخصی توصیف می‌کند:
C Programming Codes || Quizzes || DSA Learn along with the community Any queries admin - @Pradeep_saii

به لطف به‌روزرسانی‌های پرتکرار (آخرین داده در تاریخ 11 ژوئن, 2026)، کانال همواره به‌روز و دارای دسترسی بالاست. تحلیل‌ها نشان می‌دهد مخاطبان به‌طور فعال با محتوا تعامل دارند و آن را به نقطه اثرگذاری مهم در دسته فناوری و برنامه‌ها تبدیل کرده‌اند.

13 433
مشترکین
-324 ساعت
-477 روز
-23230 روز
آرشیو پست ها
💻 Detect Cycle in Linked List
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

int detectCycle(struct Node *head) {
    struct Node *slow = head;
    struct Node *fast = head;

    while (slow != NULL && fast != NULL && fast->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;

        if (slow == fast) {
            return 1; // Cycle detected
        }
    }

    return 0; // No cycle detected
}

int main() {
    struct Node *head = NULL;
    struct Node *second = NULL;
    struct Node *third = NULL;

    head = (struct Node*) malloc(sizeof(struct Node));
    second = (struct Node*) malloc(sizeof(struct Node));
    third = (struct Node*) malloc(sizeof(struct Node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    // Create a cycle (optional, for testing)
    third->next = head;

    if (detectCycle(head)) {
        printf("Cycle detectedn");
    } else {
        printf("No cycle detectedn");
    }

    return 0;
}
📤 Output:
Cycle detected

💻 Find Middle Element of Linked List
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

struct Node* findMiddle(struct Node* head) {
    struct Node* slowPtr = head;
    struct Node* fastPtr = head;

    if (head == NULL)
        return NULL;

    while (fastPtr != NULL && fastPtr->next != NULL) {
        slowPtr = slowPtr->next;
        fastPtr = fastPtr->next->next;
    }

    return slowPtr;
}

int main() {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;
    struct Node* fourth = NULL;
    struct Node* fifth = NULL;

    head = createNode(1);
    second = createNode(2);
    third = createNode(3);
    fourth = createNode(4);
    fifth = createNode(5);

    head->next = second;
    second->next = third;
    third->next = fourth;
    fourth->next = fifth;

    struct Node* middle = findMiddle(head);

    if (middle != NULL) {
        printf("Middle element is: %dn", middle->data);
    } else {
        printf("Linked list is empty.n");
    }

    return 0;
}
📤 Output:
Middle element is: 3

💻 Reverse Linked List (Recursive)
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

void insert(struct Node** head, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = *head;
    *head = newNode;
}

void printList(struct Node *head) {
    while (head != NULL) {
        printf("%d ", head->data);
        head = head->next;
    }
    printf("n");
}

struct Node* reverseListRecursive(struct Node* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }

    struct Node* newHead = reverseListRecursive(head->next);
    head->next->next = head;
    head->next = NULL;
    return newHead;
}

int main() {
    struct Node* head = NULL;
    int n, data, i;

    printf("Enter the number of nodes: ");
    scanf("%d", &n);

    printf("Enter the data for each node:n");
    for (i = 0; i < n; i++) {
        scanf("%d", &data);
        insert(&head, data);
    }

    printf("Original Linked List: ");
    printList(head);

    head = reverseListRecursive(head);

    printf("Reversed Linked List: ");
    printList(head);

    return 0;
}
📤 Output:
Input: 5
Input: 1
Input: 2
Input: 3
Input: 4
Input: 5
Output: Enter the number of nodes: Enter the data for each node:
Original Linked List: 5 4 3 2 1
Reversed Linked List: 1 2 3 4 5

💻 Reverse Linked List (Iterative)
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* reverseList(struct Node* head) {
    struct Node* prev = NULL;
    struct Node* current = head;
    struct Node* next = NULL;
    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    head = prev;
    return head;
}

void printList(struct Node* head) {
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("n");
}

int main() {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    printf("Original list: ");
    printList(head);

    head = reverseList(head);

    printf("Reversed list: ");
    printList(head);

    return 0;
}
📤 Output:
Original list: 1 2 3
Reversed list: 3 2 1

💻 Circular Linked List Implementation
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

struct Node *addToEmpty(struct Node *last, int data) {
    if (last != NULL)
        return last;

    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    last = newNode;
    last->next = last;
    return last;
}

struct Node *addBegin(struct Node *last, int data) {
    if (last == NULL)
        return addToEmpty(last, data);

    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = last->next;
    last->next = newNode;
    return last;
}

struct Node *addEnd(struct Node *last, int data) {
    if (last == NULL)
        return addToEmpty(last, data);

    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = last->next;
    last->next = newNode;
    last = newNode;
    return last;
}

void traverse(struct Node *last) {
    if (last == NULL) {
        printf("List is empty.n");
        return;
    }

    struct Node *temp = last->next;
    do {
        printf("%d ", temp->data);
        temp = temp->next;
    } while (temp != last->next);
    printf("n");
}

int main() {
    struct Node *last = NULL;
    int choice, data;

    while (1) {
        printf("1. Add to empty listn");
        printf("2. Add at the beginningn");
        printf("3. Add at the endn");
        printf("4. Traversen");
        printf("5. Exitn");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter data: ");
                scanf("%d", &data);
                last = addToEmpty(last, data);
                break;
            case 2:
                printf("Enter data: ");
                scanf("%d", &data);
                last = addBegin(last, data);
                break;
            case 3:
                printf("Enter data: ");
                scanf("%d", &data);
                last = addEnd(last, data);
                break;
            case 4:
                traverse(last);
                break;
            case 5:
                exit(0);
            default:
                printf("Invalid choice.n");
        }
    }

    return 0;
}
📤 Output:
1. Add to empty list
2. Add at the beginning
3. Add at the end
4. Traverse
5. Exit
Enter your choice: 1
Input: 1
Enter data: 10
1. Add to empty list
2. Add at the beginning
3. Add at the end
4. Traverse
5. Exit
Enter your choice: 4
Output: 10
1. Add to empty list
2. Add at the beginning
3. Add at the end
4. Traverse
5. Exit
Enter your choice: 2
Input: 2
Enter data: 20
1. Add to empty list
2. Add at the beginning
3. Add at the end
4. Traverse
5. Exit
Enter your choice: 4
Output: 20 10
1. Add to empty list
2. Add at the beginning
3. Add at the end
4. Traverse
5. Exit
Enter your choice: 3
Input: 3
Enter data: 30
1. Add to empty list
2. Add at the beginning
3. Add at the end
4. Traverse
5. Exit
Enter your choice: 4
Output: 20 10 30
1. Add to empty list
2. Add at the beginning
3. Add at the end
4. Traverse
5. Exit
Enter your choice: 5

💻 Doubly Linked List Implementation
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};

struct Node* head = NULL;

void insertAtHead(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = head;
    newNode->prev = NULL;
    if (head != NULL) {
        head->prev = newNode;
    }
    head = newNode;
}

void display() {
    struct Node* temp = head;
    printf("List: ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("n");
}

int main() {
    int choice, value;

    while (1) {
        printf("1. Insert at Headn");
        printf("2. Displayn");
        printf("3. Exitn");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter value to insert: ");
                scanf("%d", &value);
                insertAtHead(value);
                break;
            case 2:
                display();
                break;
            case 3:
                exit(0);
            default:
                printf("Invalid choicen");
        }
    }

    return 0;
}
📤 Output:
1. Insert at Head
2. Display
3. Exit
Enter your choice: 1
Input: 1
Enter value to insert: 10
1. Insert at Head
2. Display
3. Exit
Enter your choice: 1
Input: 1
Enter value to insert: 20
1. Insert at Head
2. Display
3. Exit
Enter your choice: 2
Input: 2
List: 20 10
1. Insert at Head
2. Display
3. Exit
Enter your choice: 3
Input: 3

💻 Singly Linked List Implementation
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

void insertAtBeginning(struct Node **head, int newData) {
    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = newData;
    newNode->next = *head;
    *head = newNode;
}

void insertAtEnd(struct Node **head, int newData) {
    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = newData;
    newNode->next = NULL;

    if (*head == NULL) {
        *head = newNode;
        return;
    }

    struct Node *last = *head;
    while (last->next != NULL) {
        last = last->next;
    }
    last->next = newNode;
}

void printList(struct Node *node) {
    while (node != NULL) {
        printf(" %d ", node->data);
        node = node->next;
    }
    printf("
");
}

int main() {
    struct Node* head = NULL;
    int choice, data;

    do {
        printf("1. Insert at Beginning
");
        printf("2. Insert at End
");
        printf("3. Print List
");
        printf("4. Exit
");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter data to insert at beginning: ");
                scanf("%d", &data);
                insertAtBeginning(&head, data);
                break;
            case 2:
                printf("Enter data to insert at end: ");
                scanf("%d", &data);
                insertAtEnd(&head, data);
                break;
            case 3:
                printf("Linked list: ");
                printList(head);
                break;
            case 4:
                printf("Exiting program.
");
                break;
            default:
                printf("Invalid choice.
");
        }
    } while (choice != 4);

    return 0;
}
📤 Output:
1. Insert at Beginning
2. Insert at End
3. Print List
4. Exit
Enter your choice: 1
Input: 1
Enter data to insert at beginning: 10
1. Insert at Beginning
2. Insert at End
3. Print List
4. Exit
Enter your choice: 1
Input: 1
Enter data to insert at beginning: 20
1. Insert at Beginning
2. Insert at End
3. Print List
4. Exit
Enter your choice: 2
Input: 2
Enter data to insert at end: 30
1. Insert at Beginning
2. Insert at End
3. Print List
4. Exit
Enter your choice: 3
Input: 3
Linked list:  20   10   30

1. Insert at Beginning
2. Insert at End
3. Print List
4. Exit
Enter your choice: 4
Input: 4
Exiting program.

🔧 Data Structures - Linked List

💻 Double Ended Queue Implementation
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 10

int deque[MAX_SIZE];
int front = -1;
int rear = -1;

int isFull() {
    return ((front == 0 && rear == MAX_SIZE - 1) || (front == rear + 1));
}

int isEmpty() {
    return (front == -1);
}

void insertFront(int value) {
    if (isFull()) {
        printf("Deque is full, cannot insert at frontn");
        return;
    }

    if (front == -1) {
        front = rear = 0;
    } else if (front == 0) {
        front = MAX_SIZE - 1;
    } else {
        front = front - 1;
    }

    deque[front] = value;
    printf("Inserted %d at frontn", value);
}

void insertRear(int value) {
    if (isFull()) {
        printf("Deque is full, cannot insert at rearn");
        return;
    }

    if (front == -1) {
        front = rear = 0;
    } else if (rear == MAX_SIZE - 1) {
        rear = 0;
    } else {
        rear = rear + 1;
    }

    deque[rear] = value;
    printf("Inserted %d at rearn", value);
}

void deleteFront() {
    if (isEmpty()) {
        printf("Deque is empty, cannot delete from frontn");
        return;
    }

    printf("Deleted %d from frontn", deque[front]);

    if (front == rear) {
        front = rear = -1;
    } else if (front == MAX_SIZE - 1) {
        front = 0;
    } else {
        front = front + 1;
    }
}

void deleteRear() {
    if (isEmpty()) {
        printf("Deque is empty, cannot delete from rearn");
        return;
    }

    printf("Deleted %d from rearn", deque[rear]);

    if (front == rear) {
        front = rear = -1;
    } else if (rear == 0) {
        rear = MAX_SIZE - 1;
    } else {
        rear = rear - 1;
    }
}

void display() {
    if (isEmpty()) {
        printf("Deque is emptyn");
        return;
    }

    printf("Deque elements: ");
    int i = front;
    while (i != rear) {
        printf("%d ", deque[i]);
        i = (i + 1) % MAX_SIZE;
    }
    printf("%dn", deque[rear]);
}

int main() {
    insertFront(5);
    insertRear(10);
    display();
    deleteFront();
    display();
    insertRear(15);
    insertFront(20);
    display();
    deleteRear();
    display();

    return 0;
}
📤 Output:
Inserted 5 at front
Inserted 10 at rear
Deque elements: 5 10
Deleted 5 from front
Deque elements: 10
Inserted 15 at rear
Inserted 20 at front
Deque elements: 20 10 15
Deleted 15 from rear
Deque elements: 20 10

💻 Sort Queue Without Extra Space
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

typedef struct {
    Node* front;
    Node* rear;
} Queue;

Queue* createQueue() {
    Queue* q = (Queue*)malloc(sizeof(Queue));
    q->front = q->rear = NULL;
    return q;
}

void enqueue(Queue* q, int 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;
}

int dequeue(Queue* q) {
    if (q->front == NULL)
        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 sortQueue(Queue* q) {
    if (q == NULL || q->front == q->rear)
        return;

    int n = 0;
    Node* current = q->front;
    while (current != NULL) {
        n++;
        current = current->next;
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            int val1 = dequeue(q);
            int val2 = dequeue(q);

            if (val1 > val2) {
                enqueue(q, val2);
                enqueue(q, val1);
            } else {
                enqueue(q, val1);
                enqueue(q, val2);
            }
        }
        int last = dequeue(q);
        enqueue(q, last);
    }
}

void displayQueue(Queue* q) {
    Node* temp = q->front;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("n");
}

int main() {
    Queue* q = createQueue();
    enqueue(q, 5);
    enqueue(q, 1);
    enqueue(q, 4);
    enqueue(q, 2);
    enqueue(q, 8);

    printf("Original Queue: ");
    displayQueue(q);

    sortQueue(q);

    printf("Sorted Queue: ");
    displayQueue(q);

    return 0;
}
📤 Output:
Original Queue: 5 1 4 2 8
Sorted Queue: 1 2 4 5 8

💻 Interleave First Half of Queue with Second Half
#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 = 0;
    } else {
        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 interleaveQueue(Queue *q) {
    if (isEmpty(q)) return;

    Queue tempQueue;
    initializeQueue(&tempQueue);

    int size = (q->rear - q->front + MAX_SIZE) % MAX_SIZE + 1;
    int halfSize = size / 2;

    for (int i = 0; i < halfSize; i++) {
        enqueue(&tempQueue, dequeue(q));
    }

    while (!isEmpty(&tempQueue)) {
        enqueue(q, dequeue(&tempQueue));
        enqueue(q, dequeue(q));
    }
}

void displayQueue(Queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty.n");
        return;
    }

    printf("Queue elements: ");
    int i = q->front;
    do {
        printf("%d ", q->arr[i]);
        i = (i + 1) % MAX_SIZE;
    } while (i != (q->rear + 1) % MAX_SIZE);
    printf("n");
}

int main() {
    Queue myQueue;
    initializeQueue(&myQueue);

    int n, value;

    printf("Enter the number of elements to enqueue: ");
    scanf("%d", &n);

    printf("Enter the elements: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &value);
        enqueue(&myQueue, value);
    }

    printf("Original Queue:n");
    displayQueue(&myQueue);

    interleaveQueue(&myQueue);

    printf("Interleaved Queue:n");
    displayQueue(&myQueue);

    return 0;
}
📤 Output:
Input: 6
Input: 1 2 3 4 5 6
Output: Enter the number of elements to enqueue: Enter the elements: Original Queue:
Queue elements: 1 2 3 4 5 6
Interleaved Queue:
Queue elements: 1 4 2 5 3 6

💻 Reverse Queue Using Recursion
#include <stdio.h>
#include <stdlib.h>

struct Queue {
    int data;
    struct Queue *next;
};

struct Queue *front = NULL;
struct Queue *rear = NULL;

void enqueue(int value) {
    struct Queue *newNode = (struct Queue*)malloc(sizeof(struct Queue));
    newNode->data = value;
    newNode->next = NULL;
    if (rear == NULL) {
        front = rear = newNode;
    } else {
        rear->next = newNode;
        rear = newNode;
    }
}

int dequeue() {
    if (front == NULL) {
        printf("Queue is emptyn");
        return -1;
    }
    int value = front->data;
    struct Queue *temp = front;
    front = front->next;
    if (front == NULL) {
        rear = NULL;
    }
    free(temp);
    return value;
}

void reverseQueue() {
    if (front == NULL)
        return;

    int data = dequeue();
    reverseQueue();

    struct Queue *newNode = (struct Queue*)malloc(sizeof(struct Queue));
    newNode->data = data;
    newNode->next = NULL;

    if (front == NULL) {
        front = rear = newNode;
    } else {
        rear->next = newNode;
        rear = newNode;
    }
}

void displayQueue() {
    struct Queue *temp = front;
    if (front == NULL) {
        printf("Queue is emptyn");
        return;
    }
    printf("Queue elements are: ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("n");
}

int main() {
    enqueue(1);
    enqueue(2);
    enqueue(3);
    enqueue(4);

    printf("Original Queue:n");
    displayQueue();

    reverseQueue();

    printf("Reversed Queue:n");
    displayQueue();

    return 0;
}
📤 Output:
Original Queue:
Queue elements are: 1 2 3 4
Reversed Queue:
Queue elements are: 4 3 2 1

💻 Circular Tour Problem
#include <stdio.h>
#include <stdbool.h>

int main() {
    int petrol[] = {4, 6, 7, 4};
    int distance[] = {6, 5, 3, 5};
    int n = sizeof(petrol) / sizeof(petrol[0]);

    int start = 0;
    int balance = 0;
    int deficit = 0;

    for (int i = 0; i < n; i++) {
        balance += petrol[i] - distance[i];
        if (balance < 0) {
            deficit += balance;
            start = i + 1;
            balance = 0;
        }
    }

    if (balance + deficit >= 0) {
        printf("Starting point: %dn", start);
    } else {
        printf("No solution existsn");
    }

    return 0;
}
📤 Output:
Starting point: 1

💻 First Non-Repeating Character in Stream
#include <stdio.h>
#include <stdlib.h>

#define MAX_CHARS 256

int main() {
    int count[MAX_CHARS] = {0};
    int inQueue[MAX_CHARS] = {0};
    char stream[100];
    int streamIndex = 0;
    char c;

    printf("Enter the character stream (max 99 characters, press Enter to finish):n");
    scanf("%99[^n]", stream);

    while (stream[streamIndex] != '0') {
        c = stream[streamIndex];
        streamIndex++;

        count[c]++;

        if (count[c] == 1) {
            inQueue[c] = 1;
        }

        char firstNonRepeating = '0';
        for (int i = 0; i < MAX_CHARS; i++) {
            if (inQueue[i] == 1 && count[i] == 1) {
                firstNonRepeating = (char)i;
                break;
            }
        }

        if (firstNonRepeating != '0') {
            printf("First non-repeating character so far: %cn", firstNonRepeating);
        } else {
            printf("No non-repeating character found so farn");
        }
    }

    return 0;
}
📤 Output:
Input: abcabcbb

Output:
Enter the character stream (max 99 characters, press Enter to finish):
First non-repeating character so far: a
First non-repeating character so far: b
First non-repeating character so far: b
First non-repeating character so far: b
First non-repeating character so far: b
No non-repeating character found so far
No non-repeating character found so far
No non-repeating character found so far

💻 Sliding Window Maximum
#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, k;

    printf("Enter the size of the array: ");
    scanf("%d", &n);

    int arr[n];

    printf("Enter the elements of the array:n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Enter the window size: ");
    scanf("%d", &k);

    printf("Sliding window maximums:n");

    for (int i = 0; i <= n - k; i++) {
        int max = arr[i];
        for (int j = 1; j < k; j++) {
            if (arr[i + j] > max) {
                max = arr[i + j];
            }
        }
        printf("%d ", max);
    }
    printf("n");

    return 0;
}
📤 Output:
Input: 5
Input: 1 3 -1 -3 5
Input: 3
Output: Enter the size of the array: Enter the elements of the array:
Enter the window size: Sliding window maximums:
3 3 5
Input: 10
Input: 1 2 3 4 5 6 7 8 9 10
Input: 4
Output: Enter the size of the array: Enter the elements of the array:
Enter the window size: Sliding window maximums:
4 5 6 7 8 9 10
Input: 7
Input: 9 8 7 6 5 4 3
Input: 1
Output: Enter the size of the array: Enter the elements of the array:
Enter the window size: Sliding window maximums:
9 8 7 6 5 4 3

💻 Implement Queue Using Two Stacks
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

typedef struct {
    int top;
    int items[MAX_SIZE];
} Stack;

void initialize(Stack *s) {
    s->top = -1;
}

int isEmpty(Stack *s) {
    return (s->top == -1);
}

int isFull(Stack *s) {
    return (s->top == MAX_SIZE - 1);
}

void push(Stack *s, int value) {
    if (isFull(s)) {
        printf("Stack Overflown");
        return;
    }
    s->items[++s->top] = value;
}

int pop(Stack *s) {
    if (isEmpty(s)) {
        printf("Stack Underflown");
        return -1;
    }
    return s->items[s->top--];
}

typedef struct {
    Stack s1;
    Stack s2;
} Queue;

void enqueue(Queue *q, int value) {
    push(&q->s1, value);
}

int dequeue(Queue *q) {
    if (isEmpty(&q->s1) && isEmpty(&q->s2)) {
        printf("Queue is emptyn");
        return -1;
    }
    if (isEmpty(&q->s2)) {
        while (!isEmpty(&q->s1)) {
            push(&q->s2, pop(&q->s1));
        }
    }
    return pop(&q->s2);
}

int main() {
    Queue q;
    initialize(&q.s1);
    initialize(&q.s2);

    enqueue(&q, 1);
    enqueue(&q, 2);
    enqueue(&q, 3);

    printf("%d ", dequeue(&q));
    printf("%d ", dequeue(&q));
    printf("%d ", dequeue(&q));
    printf("n");

    return 0;
}
📤 Output:
1 2 3

💻 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)