DSA Activity
DSA Activity
#include <stdio.h>
#include <stdbool.h>
typedef struct {
int data[MAX_QUEUE_SIZE];
int front;
int rear;
} Queue;
SYBSc CS if (isFull(q)) {
printf("Queue is full. Cannot enqueue.\n");
} else {
if (isEmpty(q)) {
q->front = 0;
}
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
q->data[q->rear] = item;
}
}
int main() {
Queue waitingList;
initQueue(&waitingList);
while (1) {
printf("Railway Reservation System Waiting List\n");
printf("1. Add passenger to waiting list\n");
printf("2. Remove passenger from waiting list\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter passenger number: ");
scanf("%d", &passenger);
enqueue(&waitingList, passenger); Q Write a program to find all solutions to the four queens problem. Your program will need
printf("Passenger %d added to the waiting list.\n", passenger); to be able to handle a search for a configuration that has no solution.
break;
case 2: #include <stdio.h>
passenger = dequeue(&waitingList); #include <stdbool.h>
if (passenger != -1) {
printf("Passenger %d removed from the waiting list.\n", passenger); #define N 4
}
break; int board[N][N];
case 3:
printf("Exiting the program.\n"); // Function to print the board
return 0; void printBoard() {
default: for (int i = 0; i < N; i++) {
printf("Invalid choice. Please try again.\n"); for (int j = 0; j < N; j++) {
} printf(board[i][j] ? "Q " : ". ");
} }
printf("\n"); }
}
printf("\n"); return hasSolution;
} }
return true;
}
if (isalnum(currentChar)) {
postfix[j++] = currentChar;
} else if (currentChar == '(') {
stack[++top] = currentChar;
} else if (currentChar == ')') { Q Write a program that copies the contents of one stack into another. Use stack library to
while (top >= 0 && stack[top] != '(') { perform basic stack operations. The order of two stacks must be identical.(Hint: Use a
postfix[j++] = stack[top--]; temporary stack to preserve the order).
}
top--; // Pop '(' #include <stdio.h>
} else if (isOperator(currentChar)) { #include <stdlib.h>
while (top >= 0 && getPrecedence(stack[top]) >= getPrecedence(currentChar)) {
postfix[j++] = stack[top--]; #define MAX_STACK_SIZE 100
}
stack[++top] = currentChar; struct Stack {
} int data[MAX_STACK_SIZE];
} int top;
};
while (top >= 0) {
postfix[j++] = stack[top--]; void initializeStack(struct Stack *stack) {
} stack->top = -1;
}
postfix[j] = '\0';
} int isFull(struct Stack *stack) {
return stack->top == MAX_STACK_SIZE - 1; initializeStack(&destinationStack);
}
// Push some elements into the source stack
int isEmpty(struct Stack *stack) { push(&sourceStack, 1);
return stack->top == -1; push(&sourceStack, 2);
} push(&sourceStack, 3);
push(&sourceStack, 4);
void push(struct Stack *stack, int value) {
if (!isFull(stack)) { printf("Copying the source stack into the destination stack...\n");
stack->data[++stack->top] = value;
} else { copyStack(&sourceStack, &destinationStack);
printf("Stack is full. Cannot push %d.\n", value);
} printf("Contents of the source stack: ");
} while (!isEmpty(&sourceStack)) {
printf("%d ", pop(&sourceStack));
int pop(struct Stack *stack) { }
if (!isEmpty(stack)) { printf("\n");
return stack->data[stack->top--];
} else { printf("Contents of the destination stack: ");
printf("Stack is empty.\n"); while (!isEmpty(&destinationStack)) {
return -1; // Return a sentinel value printf("%d ", pop(&destinationStack));
} }
} printf("\n");
// Copy elements from the source stack to the destination stack while preserving order
while (!isEmpty(source)) {
int value = pop(source);
push(&temp, value);
}
while (!isEmpty(&temp)) {
int value = pop(&temp);
push(destination, value); Q There are lists where new elements are always appended at the end of the list. The list
push(source, value); // Restore the original order in the source stack can be implemented as a circular list with the external pointer pointing to the last element of
} the list. Implement doubly linked circular list of integers with append and display operations.
} The operation append(L, n), appends to the end of the list, n integers either accepted from
the user or randomly generated.
int main() {
struct Stack sourceStack, destinationStack; #include <stdio.h>
initializeStack(&sourceStack); #include <stdlib.h>
#include <time.h>
printf("Elements in the list: ");
// Structure for a node in the doubly linked circular list struct Node* current = last->next;
struct Node { do {
int data; printf("%d ", current->data);
struct Node* next; current = current->next;
struct Node* prev; } while (current != last->next);
}; printf("\n");
}
// Function to create a new node
struct Node* createNode(int value) { int main() {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); struct Node* last = NULL;
newNode->data = value; int n;
newNode->next = newNode->prev = newNode;
return newNode; printf("Enter the number of integers to append: ");
} scanf("%d", &n);
if (*last == NULL) {
*last = newNode;
} else {
newNode->next = (*last)->next;
newNode->prev = *last;
(*last)->next->prev = newNode;
(*last)->next = newNode;
*last = newNode;
}
}
}
// Swap the randomly selected element with the last element to use it as the pivot
swap(&arr[randomIndex], &arr[high]);
void enqueue(struct PriorityQueue *queue, int data, int priority) { // Enqueue elements with priorities
if (queue->size >= MAX_QUEUE_SIZE) { enqueue(&queue, 5, 2);
printf("Queue is full. Cannot add element.\n"); enqueue(&queue, 8, 1);
return; enqueue(&queue, 3, 3);
} enqueue(&queue, 7, 1);
enqueue(&queue, 6, 2);
int i = queue->size;
while (i > 0 && priority < queue->elements[i - 1].priority) { printQueue(&queue);
queue->elements[i] = queue->elements[i - 1];
i--; // Dequeue elements based on priority
} int dequeued = dequeue(&queue);
if (dequeued != -1) {
queue->elements[i].data = data; printf("Dequeued element: %d\n", dequeued);
queue->elements[i].priority = priority; }
queue->size++;
} printQueue(&queue);
return data;
}
if (Q->front == Q->rear) {
return (Q->size == MAX_SIZE); Q->front = Q->rear = -1;
} } else if (Q->front == MAX_SIZE - 1) {
Q->front = 0;
int getFront(struct Deque *Q) { } else {
if (isEmpty(Q)) { Q->front++;
printf("Deque is empty.\n"); }
return -1; // Return a sentinel value
} Q->size--;
return Q->arr[Q->front]; }
}
void addRear(struct Deque *Q, int x) {
int getRear(struct Deque *Q) { if (isFull(Q)) {
if (isEmpty(Q)) { printf("Deque is full. Cannot add element at the rear.\n");
return; printf("Rear: %d\n", getRear(&Q));
}
deleteFront(&Q);
if (Q->front == -1) { deleteRear(&Q);
Q->front = Q->rear = 0;
} else if (Q->rear == MAX_SIZE - 1) { printf("Front: %d\n", getFront(&Q));
Q->rear = 0; printf("Rear: %d\n", getRear(&Q));
} else {
Q->rear++; return 0;
} }
Q->arr[Q->rear] = x;
Q->size++;
}
if (Q->front == Q->rear) {
Q->front = Q->rear = -1;
} else if (Q->rear == 0) {
Q->rear = MAX_SIZE - 1;
} else {
Q->rear--;
}
Q->size--;
}
int main() {
struct Deque Q;
init(&Q);
addRear(&Q, 1);
addRear(&Q, 2);
addFront(&Q, 0);