Ds Lab Manual
Ds Lab Manual
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
return 0;
}
Output:
#include <stdio.h>
int main() {
int n, key;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
int result = linearSearch(arr, n, key);
if (result == -1)
printf("Element %d is not present in the array\n", key);
else
printf("Element %d is present at index %d\n", key, result);
return 0;
}
Output:
Source Code:
Binary Search with User Input:
#include <stdio.h>
int main() {
int n, key;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements in sorted order:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
int result = binarySearch(arr, 0, n - 1, key);
if (result == -1)
printf("Element %d is not present in the array\n", key);
else
printf("Element %d is present at index %d\n", key, result);
return 0;
}
Output:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
// Search for the key to be deleted, keep track of the previous node as we need to change
'prev->next'
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
// Free memory
free(temp);
}
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insert(&head, data);
printf("Node with data %d inserted.\n", data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
printf("Node with data %d deleted.\n", data);
break;
case 3:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
ii) Develop a program to reverse a linked list iteratively and recursively.
Source Code:
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
int choice, data;
while (1) {
printf("\n1. Insert\n");
printf("2. Reverse Iteratively\n");
printf("3. Reverse Recursively\n");
printf("4. Print List\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insert(&head, data);
printf("Node with data %d inserted.\n", data);
break;
case 2:
reverseIterative(&head);
printf("List reversed iteratively.\n");
break;
case 3:
reverseRecursive(&head);
printf("List reversed recursively.\n");
break;
case 4:
printList(head);
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
// Search for the key to be deleted, keep track of the previous node
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
// If the key was not present in the linked list
if (temp == NULL) return;
// Free memory
free(temp);
}
// Main function
int main() {
struct Node* head = NULL;
int choice, data;
while (1) {
printf("\n1. Insert\n");
printf("2. Delete\n");
printf("3. Print List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insert(&head, data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
break;
case 3:
printList(head);
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
Exercise 3: Linked List Applications
i) Create a program to detect and remove duplicates from a linked list.
Source Code:
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
int data;
// Creating the linked list
while (1) {
printf("Enter data to insert (Enter -1 to stop): ");
scanf("%d", &data);
if (data == -1)
break;
insert(&head, data);
}
// Removing duplicates
removeDuplicates(head);
printf("Duplicates removed.\n");
return 0;
}
Output:
ii) Implement a linked list to represent polynomials and perform addition.
Source Code:
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the linked list representing a term in a polynomial
struct Node {
int coefficient;
int exponent;
struct Node* next;
};
// Function to insert a new term (node) into the polynomial linked list
void insertTerm(struct Node** head_ref, int coef, int exp) {
// Allocate memory for new node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
// Assign coefficient and exponent to new node
new_node->coefficient = coef;
new_node->exponent = exp;
// Make next of new node as head
new_node->next = (*head_ref);
// Move the head to point to the new node
(*head_ref) = new_node;
}
return result;
}
// Main function
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* result = NULL;
int coef, exp;
return 0;
}
Output:
// Initialize deque
void initDeque(struct Deque *deque) {
deque->front = -1;
deque->rear = -1;
}
// Main function
int main() {
struct Deque deque;
initDeque(&deque);
switch (choice) {
case 1:
printf("Enter element to insert at front: ");
scanf("%d", &data);
insertFront(&deque, data);
break;
case 2:
printf("Enter element to insert at rear: ");
scanf("%d", &data);
insertRear(&deque, data);
break;
case 3:
deleteFront(&deque);
printf("Element deleted from front.\n");
break;
case 4:
deleteRear(&deque);
printf("Element deleted from rear.\n");
break;
case 5:
display(&deque);
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
Exercise 4: Double Linked List Implementation
i) Implement a doubly linked list and perform various operations to understand its
properties and applications.
Source Code:
#include <stdio.h>
#include <stdlib.h>
// Function to insert a new node at the beginning of the doubly linked list
void insertAtBeginning(struct Node** head_ref, int data) {
struct Node* newNode = createNode(data);
if (*head_ref == NULL) {
*head_ref = newNode;
} else {
newNode->next = *head_ref;
(*head_ref)->prev = newNode;
*head_ref = newNode;
}
}
// Function to insert a new node at the end of the doubly linked list
void insertAtEnd(struct Node** head_ref, int data) {
struct Node* newNode = createNode(data);
if (*head_ref == NULL) {
*head_ref = newNode;
} else {
struct Node* temp = *head_ref;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
// Function to delete the first occurrence of a node with given data from the doubly linked list
void deleteNode(struct Node** head_ref, int data) {
if (*head_ref == NULL) {
printf("List is empty. Deletion failed.\n");
return;
}
struct Node* temp = *head_ref;
// If the node to be deleted is the first node
if (temp->data == data) {
*head_ref = temp->next;
if (*head_ref != NULL) {
(*head_ref)->prev = NULL;
}
free(temp);
printf("Node with data %d deleted.\n", data);
return;
}
// Search for the node to be deleted
while (temp != NULL && temp->data != data) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with data %d not found.\n", data);
return;
}
temp->prev->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
printf("Node with data %d deleted.\n", data);
}
// Function to display the doubly linked list
void display(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
printf("Doubly Linked List: ");
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
// Main function
int main() {
struct Node* head = NULL;
int choice, data;
while (1) {
printf("\n1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Delete node\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data to insert at end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter data to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
break;
case 4:
display(head);
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
ii) Implement a circular linked list and perform insertion, deletion, and traversal.
Source code:
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the circular linked list
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a new node at the beginning of the circular linked list
void insertAtBeginning(struct Node** head_ref, int data) {
struct Node* newNode = createNode(data);
if (*head_ref == NULL) {
*head_ref = newNode;
newNode->next = newNode;
} else {
struct Node* last = *head_ref;
while (last->next != *head_ref) {
last = last->next;
}
newNode->next = *head_ref;
last->next = newNode;
*head_ref = newNode;
}
}
// Function to insert a new node at the end of the circular linked list
void insertAtEnd(struct Node** head_ref, int data) {
struct Node* newNode = createNode(data);
if (*head_ref == NULL) {
*head_ref = newNode;
newNode->next = newNode;
} else {
struct Node* last = *head_ref;
while (last->next != *head_ref) {
last = last->next;
}
last->next = newNode;
newNode->next = *head_ref;
}
}
// Function to delete the first occurrence of a node with given data from the circular linked
list
void deleteNode(struct Node** head_ref, int data) {
if (*head_ref == NULL) {
printf("List is empty. Deletion failed.\n");
return;
}
struct Node *temp = *head_ref, *prev;
if (temp->data == data) {
if (temp->next == temp) {
free(temp);
*head_ref = NULL;
} else {
while (temp->next != *head_ref) {
temp = temp->next;
}
temp->next = (*head_ref)->next;
temp = *head_ref;
*head_ref = (*head_ref)->next;
free(temp);
}
printf("Node with data %d deleted.\n", data);
return;
}
while (temp->next != *head_ref && temp->data != data) {
prev = temp;
temp = temp->next;
}
if (temp->data != data) {
printf("Node with data %d not found.\n", data);
return;
}
prev->next = temp->next;
free(temp);
printf("Node with data %d deleted.\n", data);
}
// Function to display the circular linked list
void display(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}
// Main function
int main() {
struct Node* head = NULL;
int choice, data;
while (1) {
printf("\n1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Delete node\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data to insert at end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter data to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
break;
case 4:
display(head);
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Output:
EXERCISE 5:
1) IMPLEMENT A STACK USING ARRAYS AND LINKED LISTS?
1)Stack using Arrays:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int items[MAX_SIZE];
int top;
} StackArray;
int main() {
StackArray stack;
initStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
printf("Popping elements:\n");
while (!isEmpty(&stack)) {
printf("%d\n", pop(&stack));
}
return 0;
}
OUTPUT:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
Node *top;
} StackLinkedList;
void initStack(StackLinkedList *stack) {
stack->top = NULL;
}
int main() {
StackLinkedList stack;
initStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
printf("Popping elements:\n");
while (!isEmpty(&stack)) {
printf("%d\n", pop(&stack));
}
return 0;
}
output:
typedef struct {
int items[MAX_SIZE];
int top;
} Stack;
while (*expression) {
if (isdigit(*expression)) {
push(&stack, *expression - '0');
} else {
int operand2 = pop(&stack);
int operand1 = pop(&stack);
switch (*expression) {
case '+':
push(&stack, operand1 + operand2);
break;
case '-':
push(&stack, operand1 - operand2);
break;
case '*':
push(&stack, operand1 * operand2);
break;
case '/':
push(&stack, operand1 / operand2);
break;
default:
printf("Invalid operator: %c\n", *expression);
exit(EXIT_FAILURE);
}
}
expression++;
}
return pop(&stack);
}
int main() {
char expression[] = "532*+"; // Postfix expression: 5 + (3 * 2)
int result = evaluatePostfix(expression);
printf("Result: %d\n", result); // Output: 11
return 0;
}
OUTPUT :
typedef struct {
char items[MAX_SIZE];
int top;
} Stack;
while (*expression) {
if (*expression == '(') {
push(&stack, *expression);
} else if (*expression == ')') {
if (isEmpty(&stack) || pop(&stack) != '(') {
return 0; // Unbalanced
}
}
expression++;
}
int main() {
char expression[] = "((()))()"; // Balanced parentheses
if (isBalanced(expression)) {
printf("Parentheses are balanced.\n");
} else {
printf("Parentheses are not balanced.\n");
}
return 0;
}
OUTPUT:
EXERCISE 6: QUEUE OPERATION
I) IMPLEMENT A QUEUE USING ARRAYS AND LINKED LISTS ?
1)Queue using Arrays:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int items[MAX_SIZE];
int front, rear;
} QueueArray;
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
#include <stdio.h>
#include <stdlib.h>
typedef struct {
Node *front, *rear;
} QueueLinkedList;
int main() {
QueueLinkedList queue;
initQueue(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
return 0;
}
OUTPUT:
2)DEVELOP A PROGRAM TO SIMULATE A SIMPLE PRINTER QUEUE SYSTEM?
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h> // for sleep()
typedef struct {
int jobNumber;
} PrintJob;
typedef struct {
PrintJob items[MAX_QUEUE_SIZE];
int front, rear;
} PrinterQueue;
int main() {
PrinterQueue printerQueue;
initQueue(&printerQueue);
printf("\nPrinting started...\n");
// Simulating printing
while (!isEmpty(&printerQueue)) {
PrintJob job = dequeue(&printerQueue);
if (job.jobNumber != -1) {
printJob(job);
}
}
printf("All jobs printed.\n");
return 0;
}
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
while (!isEmpty(&stack)) {
if (stack.items[stack.top] == '(') {
printf("Invalid expression\n");
exit(EXIT_FAILURE);
}
postfix[j++] = pop(&stack);
}
postfix[j] = '\0';
}
return pop(&stack);
}
int main() {
char infix[MAX_SIZE], postfix[MAX_SIZE];
infixToPostfix(infix, postfix);
OUTPUT:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
if (isPalindrome(str)) {
printf("'%s' is a palindrome.\n", str);
} else {
printf("'%s' is not a palindrome.\n", str);
}
return 0;
}
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
int main() {
TreeNode *root = NULL;
return 0;
}
OUTPUT:
2)TRAVERSING OF BST?
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main() {
TreeNode *root = NULL;
// Inorder traversal
printf("Inorder traversal of the BST: ");
inorder(root);
printf("\n");
// Preorder traversal
printf("Preorder traversal of the BST: ");
preorder(root);
printf("\n");
// Postorder traversal
printf("Postorder traversal of the BST: ");
postorder(root);
printf("\n");
return 0;
}
OUTPUT:
EXERCISE 9:HASHING
1)IMPLEMENT A HASH TABLE WITH COLLISION RESOLUTION TECHNIQUES?
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 10
// Function to search for a key in the hash table and return its value
int search(HashTable *hashTable, char *key) {
int index = hash(key);
Node *temp = hashTable->buckets[index];
while (temp != NULL) {
if (strcmp(temp->key, key) == 0) {
return temp->value;
}
temp = temp->next;
}
return -1; // Key not found
}
int main() {
HashTable hashTable;
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable.buckets[i] = NULL;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CACHE_SIZE 10
#define TABLE_SIZE 20
int main() {
HashTable hashTable;
initHashTable(&hashTable);
return 0;
}
OUTPUT: