0% found this document useful (0 votes)
3 views9 pages

Queue Inclass Postclass

The document contains multiple C programming examples demonstrating queue and stack implementations using arrays and linked lists. It includes functions for enqueueing, dequeueing, displaying, and checking the status of the data structures. Each section is labeled as either 'Inclass' or 'Postclass' and showcases different approaches to managing data in queues and stacks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Queue Inclass Postclass

The document contains multiple C programming examples demonstrating queue and stack implementations using arrays and linked lists. It includes functions for enqueueing, dequeueing, displaying, and checking the status of the data structures. Each section is labeled as either 'Inclass' or 'Postclass' and showcases different approaches to managing data in queues and stacks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10 March 2026 08:31

Inclass q1

#include <stdio.h>
#include <string.h>

int front = -1, rear = -1, capacity;


char queue[100][50];

void enqueue(char name[])


{
if (rear == capacity - 1)
{
printf("Queue is full. Cannot add %s\n", name);
return;
}

if (front == -1)
front = 0;

rear++;
strcpy(queue[rear], name);
}

void dequeue()
{
if (front == -1 || front > rear)
{
printf("Served Customer: Queue is empty. No customer to serve.\n");
return;
}

printf("Served Customer: %s\n", queue[front]);

queue_inclass_postclass Page 1
printf("Served Customer: %s\n", queue[front]);
front++;

if (front > rear)


{
front = -1;
rear = -1;
}
}

void display()
{
if (front == -1 || front > rear)
{
printf("Queue is empty.\n");
return;
}

printf("Current Queue: ");

for (int i = front; i <= rear; i++)


{
printf("%s ", queue[i]);
}

printf("\n");
}

int main()
{
int n;
char operation[20];
char name[50];

scanf("%d", &capacity);
scanf("%d", &n);

for (int i = 0; i < n; i++)


{
scanf("%s", operation);

if (strcmp(operation, "ENQUEUE") == 0)
{
scanf("%s", name);
enqueue(name);
}
else if (strcmp(operation, "DEQUEUE") == 0)
{
dequeue();
}
else if (strcmp(operation, "DISPLAY") == 0)
{
display();
}
}

return 0;
}

Inclass q2

queue_inclass_postclass Page 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
char ticket[100];
struct node *next;
};

struct node *front = NULL;


struct node *rear = NULL;

void enqueue(char ticket[])


{
struct node *newNode = (struct node *)malloc(sizeof(struct node));

strcpy(newNode->ticket, ticket);
newNode->next = NULL;

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

void dequeue()
{
if (front == NULL)
{
printf("Queue is empty.\n");
return;
}

struct node *temp = front;

printf("Processed Ticket: %s\n", front->ticket);

front = front->next;
free(temp);

if (front == NULL)
rear = NULL;
}

void display()
{
if (front == NULL)
{
printf("Queue is empty.\n");
return;
}

struct node *temp = front;

printf("Current Queue: ");

while (temp != NULL)


{
printf("%s ", temp->ticket);
temp = temp->next;

queue_inclass_postclass Page 3
temp = temp->next;
}

printf("\n");
}

int main()
{
int n;
scanf("%d", &n);

for (int i = 0; i < n; i++)


{
char input[20];
scanf("%s", input);

if (strcmp(input, "ENQUEUE") == 0)
{
char ticket[100];
scanf("%s", ticket);
enqueue(ticket);
}
else if (strcmp(input, "DEQUEUE") == 0)
{
dequeue();
}
else if (strcmp(input, "DISPLAY") == 0)
{
display();
}
else
{
printf("Invalid command.\n");
}
}

return 0;
}

Inclass q3

#include <stdio.h>
#include <string.h>

#define MAX 100

int q1[MAX], q2[MAX];


int front1 = -1, rear1 = -1;
int front2 = -1, rear2 = -1;

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

void enqueue_q2(int x) {
if (front2 == -1)
front2 = 0;
rear2++;
q2[rear2] = x;
}

int dequeue_q1() {

queue_inclass_postclass Page 4
int dequeue_q1() {
int value = q1[front1];
front1++;

if (front1 > rear1) {


front1 = rear1 = -1;
}
return value;
}
void push(int x) {

enqueue_q2(x);

while (!isEmpty()) {
enqueue_q2(dequeue_q1());
}

for (int i = front2; i <= rear2; i++) {


if (front1 == -1)
front1 = 0;
rear1++;
q1[rear1] = q2[i];
}

front2 = rear2 = -1;

printf("Pushed: %d\n", x);


}

void pop() {
if (isEmpty()) {
printf("Stack is empty.\n");
return;
}

int value = dequeue_q1();


printf("Popped: %d\n", value);
}

void top() {
if (isEmpty()) {
printf("Stack is empty.\n");
return;
}

printf("Top version: %d\n", q1[front1]);


}

void empty() {
if (isEmpty())
printf("Is stack empty: true\n");
else
printf("Is stack empty: false\n");
}

int main() {

int n;
scanf("%d", &n);

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

char command[10];
scanf("%s", command);

if (strcmp(command, "push") == 0) {
int x;

queue_inclass_postclass Page 5
int x;
scanf("%d", &x);
push(x);
}
else if (strcmp(command, "pop") == 0) {
pop();
}
else if (strcmp(command, "top") == 0) {
top();
}
else if (strcmp(command, "empty") == 0) {
empty();
}
}

return 0;
}

Postclass q1

#include <stdio.h>
#include <string.h>
int stack1[100], stack2[100];
int top1 = -1, top2 = -1;
void push(int x)
{
stack1[++top1] = x;
printf("Order added: %d\n", x);
}

void move()
{
if (top2 == -1)
{
while (top1 != -1)
{
stack2[++top2] = stack1[top1--];
}
}
}

void pop()
{
move();

if (top2 == -1)
return;

printf("Order processed: %d\n", stack2[top2--]);


}

void peek()
{
move();

if (top2 == -1)
return;

printf("Next order: %d\n", stack2[top2]);


}

void empty()
{

queue_inclass_postclass Page 6
{
if (top1 == -1 && top2 == -1)
printf("Is queue empty: true\n");
else
printf("Is queue empty: false\n");
}

int main()
{
int n, x;
char op[10];

scanf("%d", &n);

for (int i = 0; i < n; i++)


{
scanf("%s", op);

if (strcmp(op, "push") == 0)
{
scanf("%d", &x);
push(x);
}
else if (strcmp(op, "pop") == 0)
{
pop();
}
else if (strcmp(op, "peek") == 0)
{
peek();
}
else if (strcmp(op, "empty") == 0)
{
empty();
}
}

return 0;
}

Postclass q2

#include <stdio.h>
#include <string.h>

int q[100];
int front = -1, rear = -1;
int size;

int isFull()
{
if ((rear + 1) % size == front)
return 1;
return 0;
}

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

queue_inclass_postclass Page 7
void enqueue(int x)
{
if (isFull())
{
printf("Booking failed\n");
return;
}

if (front == -1)
front = 0;

rear = (rear + 1) % size;


q[rear] = x;

printf("Booked seat: %d\n", x);


}

void dequeue()
{
if (isEmpty())
{
printf("No booking to cancel\n");
return;
}

printf("Canceled seat: %d\n", q[front]);

if (front == rear)
front = rear = -1;
else
front = (front + 1) % size;
}

void showFront()
{
if (isEmpty())
printf("No bookings\n");
else
printf("Next seat to be canceled: %d\n", q[front]);
}
int main()
{
int n, x;
char op[20];

scanf("%d", &size);
scanf("%d", &n);

for (int i = 0; i < n; i++)


{
scanf("%s", op);

if (strcmp(op, "enqueue") == 0)
{
scanf("%d", &x);
enqueue(x);
}
else if (strcmp(op, "dequeue") == 0)
{
dequeue();
}
else if (strcmp(op, "front") == 0)
{
showFront();
}
else if (strcmp(op, "isFull") == 0)

queue_inclass_postclass Page 8
else if (strcmp(op, "isFull") == 0)
{
if (isFull())
printf("true\n");
else
printf("false\n");
}
else if (strcmp(op, "isEmpty") == 0)
{
if (isEmpty())
printf("true\n");
else
printf("false\n");
}
}

return 0;
}

Postclass q3
#include <stdio.h>

int stack[100000];
int top = -1;

int main()
{
int n, x;

scanf("%d", &n);

for(int i = 0; i < n; i++)


{
scanf("%d", &x);
stack[++top] = x;
}

while(top != -1)
{
printf("%d ", stack[top--]);
}

return 0;
}

queue_inclass_postclass Page 9

You might also like