Ashutosh Dilip Raoplaei
Bits ID- 2023wd86488
Data Structures and Algorithms
Lab Assignment1
1. We have a box with N slots we have to place ‘M’ balls
at positions dictated by following rule, based on their
color codes of 6 digits. Rule 1: Create a function such
that the color code should map with in ‘N’ slots. Rule 2:
If the slot is already filled find the next consecutive slot
which is free. // Hint: Use the concept of hashing
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to calculate hash for a given color code
int hash_function(char *color_code, int n) {
int sum = 0;
for (int i = 0; i < strlen(color_code); i++) {
sum += color_code[i] - '0';
return sum % n;
// Function to find the next free slot
int find_next_free_slot(int slots[], int n, int start_index) {
for (int i = 0; i < n; i++) {
int index = (start_index + i) %
n; if (slots[index] == 0) {
return index;
}
}
return -1; // No free slot found (should not happen if n >= M)
int main() {
int n; // Number of slots
printf("Enter No of Slots: ");
scanf("%d", &n);
int slots[n]; // Array to keep track of filled slots
memset(slots, 0, sizeof(slots)); // Initialize all slots to 0 (free)
char color_code[7]; // Buffer to hold the color code input
for (int i = 0; i < n; i++) {
printf("enter 6-digit color code of ball to be inserted: ");
scanf("%s", color_code);
int initial_slot = hash_function(color_code, n); // Calculate initial slot using hash function
int slot = find_next_free_slot(slots, n, initial_slot); // Find the next free slot
if (slot != -1) {
slots[slot] = 1; // Mark the slot as filled
printf("Ball %s colour code is at %d slot\n", color_code, slot);
} else {
printf("No free slot available for ball %s\n", color_code);
return 0;
}
2. Thereare two hockey teams with 5 experts having
their scores and 6 experts having their scores. Now
concatenate the two hockey teams and display both the
teams score and also
total score of both the teams. // Hint: Use the concept of Linked List
Sample Input and Output
#include <stdio.h>
#include <stdlib.h>
// Node structure for the linked list
struct Node {
int score;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int score) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->score = score;
newNode->next = NULL;
return newNode;
}
// Function to append a node to the end of the
list void appendNode(struct Node** head, int
score) { struct Node* newNode =
createNode(score);
if (*head == NULL) {
*head = newNode;
return;
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
temp->next = newNode;
// Function to print the linked list and calculate the total score
int printListAndCalculateTotal(struct Node* head) {
int totalScore = 0;
struct Node* temp = head;
while (temp != NULL) {
printf("%d-", temp->score);
totalScore += temp->score;
temp = temp->next;
printf("NULL\n");
return totalScore;
int main() {
int teamASize, teamBSize;
struct Node* teamA = NULL;
struct Node* teamB = NULL;
printf("Enter Hockey Team -A Size: ");
scanf("%d", &teamASize);
printf("Enter Scores Of Team-A\n");
for (int i = 0; i < teamASize; i++) {
int score;
scanf("%d", &score);
appendNode(&teamA, score);
printf("Enter Hockey Team -B Size: ");
scanf("%d", &teamBSize);
printf("Enter Scores Of Team-B\n");
for (int i = 0; i < teamBSize; i++) {
int score;
scanf("%d", &score);
appendNode(&teamB, score);
// Concatenate teamB at the end of teamA
struct Node* temp = teamA;
while (temp->next != NULL) {
temp = temp->next;
temp->next = teamB;
printf("The scores of team-A & team-B\n");
int totalScore = printListAndCalculateTotal(teamA);
printf("Total Score of both teams %d\n", totalScore);
return 0;
}
3. A
digit means push operation and digit ‘10’ means
pop operation. Given the sequence of values returned
by pop operation when this sequence of operations is
performed on an initially empty LIFO stack.
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
// Stack structure definition
struct Stack {
int arr[MAX];
int top;
};
// Function to initialize the stack
void initStack(struct Stack* stack) {
stack->top = -1;
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1;
// Function to check if the stack is
full int isFull(struct Stack* stack) {
return stack->top == MAX - 1;
// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
stack->arr[++stack->top] = value;
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
return stack->arr[stack->top--];
// Function to print the stack elements
void printStack(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
for (int i = stack->top; i >= 0; i--) {
printf("%d ", stack->arr[i]);
printf("\n");
int main() {
int n; // Number of elements
printf("Enter Size of stack: ");
scanf("%d", &n);
struct Stack stack;
initStack(&stack);
printf("Enter The Elements\n");
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
if (value == 10) {
pop(&stack);
} else {
push(&stack, value);
printf("Stack Elements Are\n");
printStack(&stack);
return 0;