DS slip test 1 questions and answers

 

  1. Write about stack representation using arrays and linked lists.
In computer science, a stack is a linear data structure that operates on a LIFO (Last-In-First-Out) principle. That means the last item added to the stack is the first item to be removed or retrieved. Elements are added and removed from one end of the stack known as the Top. stack can be implemented by using arrays, linked lists, structures or pointers.
















  1. 2. write a program to implement queues using arrays.
// C Program to implement queue using arrays
#include <stdio.h>
// Define the maximum size for the queue
#define MAX_SIZE 100

// Define a structure for the queue
struct Queue {
    int queue[MAX_SIZE];
    int front;
    int rear;
};

// Function to initialize the queue
void initializeQueue(struct Queue *q) {
    q->front = -1;
    q->rear = -1;
}

// Function to check if the queue is empty
int isEmpty(struct Queue *q) {
    return (q->front == -1);
}

// Function to check if the queue is full
int isFull(struct Queue *q) {
    return (q->rear == MAX_SIZE - 1);
}

// Function to insert an element into the queue
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);
}

// Function to remove an element from the queue
int dequeue(struct Queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty\n");
        return -1;
    }
    int data = q->queue[q->front];
    // If the queue is empty reset the pointers
    if (q->front == q->rear) {
        q->front = -1;
        q->rear = -1;
    } else {
        q->front++;
    }
    printf("Deleted element: %d\n", data);
    return data;
}

// Function to display the elements of the queue
void display(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() {
    // Initialize a queue
    struct Queue q;
    initializeQueue(&q);

    enqueue(&q, 1);
    enqueue(&q, 2);
    enqueue(&q, 3);
    printf("Elements in the queue after enqueue operation: ");
    display(&q);

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

    return 0;
}


Comments