Lyo001 / pop

0 stars 0 forks source link

C dsa #1

Open Lyo001 opened 1 month ago

Lyo001 commented 1 month ago

include

define MAX_SIZE 100

struct Queue { int queue[MAX_SIZE]; int front; int rear; };

void initializeQueue(struct Queue *q) { q->front = -1; q->rear = -1; }

int isEmpty(struct Queue *q) { return (q->front == -1); }

int isFull(struct Queue *q) { return (q->rear == MAX_SIZE - 1); }

void enqueue(struct Queue *q, int data) { if (isFull(q)) { printf("Queue is full\n"); return; }

if (isEmpty(q)) {
    q->front = 0;
}

q->rear++;
q->queue[q->rear] = data;
printf("Enqueued %d in queue\n", data);

}

int dequeue(struct Queue *q) { if (isEmpty(q)) { printf("Queue is empty\n"); return -1; }

int data = q->queue[q->front];
if (q->front == q->rear) {
    q->front = -1;
    q->rear = -1;
} else {
    q->front++;
}

printf("Deleted element: %d\n", data);
return data;

}

void peek(struct Queue *q) { if (isEmpty(q)) { printf("Queue is empty\n"); return; }

for (int i = q->front; i <= q->rear; i++) {
    printf("%d ", q->queue[i]);
}
printf("\n");

}

int main() { int a, b, c; struct Queue q;

initializeQueue(&q);

printf("Enter 3 nos. to enqueue\n");
scanf("%d %d %d", &a, &b, &c);

enqueue(&q, a);
enqueue(&q, b);
enqueue(&q, c);

printf("Elements in the queue after enqueue operation: ");
peek(&q);

dequeue(&q);

printf("Elements in the queue after dequeue operation: ");
peek(&q);

return 0;

}

Lyo001 commented 1 month ago

include

include

include

define CAPACITY 100

int queue[CAPACITY]; unsigned int size = 0; int front = 0; unsigned int rear = CAPACITY - 1;

int enqueue(int data); int dequeue(); int isEmpty(); int display();

int main() { int ch, data; while (1) { printf("QUEUE ARRAY IMPLEMENTATION\n"); printf("1. Enqueue\n"); printf("2. Dequeue\n"); printf("3. Display\n"); printf("0. Exit\n"); printf("Enter your choice: "); scanf("%d", &ch);

    switch (ch) {
        case 1:
            printf("\nEnter data to enqueue: ");
            scanf("%d", &data);
            enqueue(data);
            break;
        case 2:
            dequeue();
            break;
        case 3:
            display();
            break;
        case 0:
            exit(0);
        default:
            printf("\nInvalid choice! Please try again.\n");
    }
}
return 0;

}

Lyo001 commented 1 month ago

include

define MAX 5

struct Queue { int item[MAX]; int front; int rear; };

int main() { struct Queue q; q.front = -1; q.rear = -1;

if (q.rear == MAX - 1) {
    printf("Queue is full\n");
} else {
    int value = 10;
    if (q.front == -1) q.front = 0;
    q.rear++;
    q.item[q.rear] = value;
    printf("Enqueued: %d\n", value);
}

if (q.front == -1 || q.front > q.rear) {
    printf("Queue is empty\n");
} else {
    int dequeuedValue = q.item[q.front];
    q.front++;
    printf("Dequeued: %d\n", dequeuedValue);
    if (q.front > q.rear) {
        q.front = q.rear = -1;
    }
}

return 0;

}

Lyo001 commented 1 month ago

include

include

define SIZE 100

void enqueue(); void dequeue(); void show(); void frontandrear();

int inp_arr[SIZE]; int Rear = -1; int Front = -1;

void main() { int ch; while (1) { printf("1. Enqueue Operation\n"); printf("2. Dequeue Operation\n"); printf("3. Display the Queue\n"); printf("4. Exit\n"); printf("5. Display front and rear\n"); printf("Enter your choice of operations: "); scanf("%d", &ch);

    switch (ch) {
        case 1:
            enqueue();
            break;
        case 2:
            dequeue();
            break;
        case 3:
            show();
            break;
        case 4:
            exit(0);
        case 5:
            frontandrear();
            break;
        default:
            printf("Incorrect choice\n");
    }
}

}

void enqueue() { int insert_item; if (Rear == SIZE - 1) { printf("Overflow\n"); } else { if (Front == -1) Front = 0; printf("Element to be inserted in the Queue: "); scanf("%d", &insert_item); Rear = Rear + 1; inp_arr[Rear] = insert_item; } }

void dequeue() { if (Front == -1 || Front > Rear) { printf("Underflow\n"); return; } else { printf("Element deleted from the Queue: %d\n", inp_arr[Front]); Front = Front + 1; } }

void show() { if (Front == -1) { printf("Empty Queue\n"); } else { printf("Queue: \n"); for (int i = Front; i <= Rear; i++) printf("%d ", inp_arr[i]); printf("\n"); } }

void frontandrear() { printf("Front is %d and rear is %d\n", inp_arr[Front], inp_arr[Rear]); }