Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Structures Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

Data Structures Lab

1. To write a program to check whether a word is palindrome or not


#include <stdio.h>

#include <string.h>

int isPalindrome(char word[]) {

int length = strlen(word);

for (int i = 0; i < length / 2; i++) {

if (word[i] != word[length - i - 1]) {

return 0; // Not a palindrome

return 1; // Palindrome

int main() {

char word[100];

printf("Enter a word: ");

scanf("%s", word);

if (isPalindrome(word)) {

printf("%s is a palindrome.\n", word);

} else {

printf("%s is not a palindrome.\n", word);

return 0;

}
Data Structures Lab
2. To create a two-dimensional array of numbers and calculate & display the row & column sum and
thegrand total.
#include <stdio.h>

int main() {

int rows, cols;

printf("Enter number of rows: ");

scanf("%d", &rows);

printf("Enter number of columns: ");

scanf("%d", &cols);

int matrix[rows][cols];

// Input

printf("Enter elements of the matrix:\n");

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

for (int j = 0; j < cols; j++) {

scanf("%d", &matrix[i][j]);

// Calculate and Display Row & Column Sums

printf("Row sums:\n");

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

int rowSum = 0;

for (int j = 0; j < cols; j++) {

rowSum += matrix[i][j];

printf("Row %d: %d\n", i + 1, rowSum);

printf("Column sums:\n");

for (int j = 0; j < cols; j++) {

int colSum = 0;
Data Structures Lab
for (int i = 0; i < rows; i++) {

colSum += matrix[i][j];

printf("Col %d: %d\n", j + 1, colSum);

// Calculate and Display Grand Total

int grandTotal = 0;

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

for (int j = 0; j < cols; j++) {

grandTotal += matrix[i][j];

printf("Grand Total: %d\n", grandTotal);

return 0;

}
Data Structures Lab
3. To write a program of matrix multiplication.
#include <stdio.h>

#define ROW1 3

#define COL1 3

#define ROW2 3

#define COL2 3

void multiplyMatrix(int mat1[][COL1], int mat2[][COL2], int result[][COL2]) {

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

for (int j = 0; j < COL2; ++j) {

result[i][j] = 0;

for (int k = 0; k < COL1; ++k) {

result[i][j] += mat1[i][k] * mat2[k][j];

void displayMatrix(int mat[][COL2]) {

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

for (int j = 0; j < COL2; ++j) {

printf("%d\t", mat[i][j]);

printf("\n");

int main() {

int mat1[ROW1][COL1] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int mat2[ROW2][COL2] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};

int result[ROW1][COL2];
Data Structures Lab
multiplyMatrix(mat1, mat2, result);

printf("Matrix 1:\n");

displayMatrix(mat1);

printf("\nMatrix 2:\n");

displayMatrix(mat2);

printf("\nResultant Matrix:\n");

displayMatrix(result);

return 0;

}
Data Structures Lab
4. To write a program to insert (Push) an element into the sack and delete (Pop) an element from the
stackusing pointer.
#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 10

struct Stack {

int *arr;

int top;

};

void initialize(struct Stack *stack) {

stack->arr = (int *)malloc(MAX_SIZE * sizeof(int));

stack->top = -1;

int isFull(struct Stack *stack) {

return stack->top == MAX_SIZE - 1;

int isEmpty(struct Stack *stack) {

return stack->top == -1;

void push(struct Stack *stack, int value) {

if (isFull(stack)) {

printf("Stack Overflow\n");

return;

stack->arr[++stack->top] = value;

printf("%d pushed to the stack\n", value);

}
Data Structures Lab
void pop(struct Stack *stack) {

if (isEmpty(stack)) {

printf("Stack Underflow\n");

return;

printf("%d popped from the stack\n", stack->arr[stack->top--]);

int main() {

struct Stack stack;

initialize(&stack);

push(&stack, 10);

push(&stack, 20);

push(&stack, 30);

pop(&stack);

pop(&stack);

pop(&stack);

pop(&stack); // Trying to pop from an empty stack

free(stack.arr); // Free allocated memory

return 0;

}
Data Structures Lab
5. To write a program to convert an infix expression to a postfix expression.
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_SIZE 100

struct Stack {

char *arr;

int top;

};

void initialize(struct Stack *stack, int size) {

stack->arr = (char *)malloc(size * sizeof(char));

stack->top = -1;

int isEmpty(struct Stack *stack) {

return stack->top == -1;

void push(struct Stack *stack, char ch) {

stack->arr[++stack->top] = ch;

char pop(struct Stack *stack) {

return stack->arr[stack->top--];

int isOperand(char ch) {

return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');

}
Data Structures Lab
int precedence(char ch) {

switch(ch) {

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

return -1;

void infixToPostfix(char *infix, char *postfix) {

struct Stack stack;

initialize(&stack, MAX_SIZE);

int i, j;

i = j = 0;

while (infix[i] != '\0') {

char current = infix[i];

if (isOperand(current)) {

postfix[j++] = current;

} else if (current == '(') {

push(&stack, current);

} else if (current == ')') {

while (!isEmpty(&stack) && stack.arr[stack.top] != '(') {

postfix[j++] = pop(&stack);

pop(&stack); // Pop '('


Data Structures Lab
} else {

while (!isEmpty(&stack) && precedence(current) <= precedence(stack.arr[stack.top])) {

postfix[j++] = pop(&stack);

push(&stack, current);

i++;

while (!isEmpty(&stack)) {

postfix[j++] = pop(&stack);

postfix[j] = '\0';

free(stack.arr);

int main() {

char infix[MAX_SIZE];

char postfix[MAX_SIZE];

printf("Enter an infix expression: ");

scanf("%s", infix);

infixToPostfix(infix, postfix);

printf("Postfix expression: %s\n", postfix);

return 0;

}
Data Structures Lab
6. To evaluate a postfix expression
#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#define MAX_SIZE 100

struct Stack {

int *arr;

int top;

};

void initialize(struct Stack *stack, int size) {

stack->arr = (int *)malloc(size * sizeof(int));

stack->top = -1;

int isEmpty(struct Stack *stack) {

return stack->top == -1;

void push(struct Stack *stack, int value) {

stack->arr[++stack->top] = value;

int pop(struct Stack *stack) {

return stack->arr[stack->top--];

int evaluatePostfix(char *postfix) {

struct Stack stack;

initialize(&stack, MAX_SIZE);
Data Structures Lab
int i = 0;

while (postfix[i] != '\0') {

char current = postfix[i];

if (isdigit(current)) {

push(&stack, current - '0');

} else {

int operand2 = pop(&stack);

int operand1 = pop(&stack);

switch (current) {

case '+':

push(&stack, operand1 + operand2);

break;

case '-':

push(&stack, operand1 - operand2);

break;

case '*':

push(&stack, operand1 * operand2);

break;

case '/':

push(&stack, operand1 / operand2);

break;

i++;

int result = pop(&stack);

free(stack.arr);
Data Structures Lab

return result;

int main() {

char postfix[MAX_SIZE];

printf("Enter a postfix expression: ");

scanf("%s", postfix);

int result = evaluatePostfix(postfix);

printf("Result: %d\n", result);

return 0;

}
Data Structures Lab
7. To write a program to insert an element in the queue and delete an element from the queue using
pointer.
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

struct Queue {

struct Node *front, *rear;

};

void initializeQueue(struct Queue *queue) {

queue->front = queue->rear = NULL;

int isEmpty(struct Queue *queue) {

return queue->front == NULL;

void enqueue(struct Queue *queue, int value) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

if (isEmpty(queue)) {

queue->front = queue->rear = newNode;

} else {

queue->rear->next = newNode;

queue->rear = newNode;

printf("%d enqueued to the queue\n", value);


Data Structures Lab
}

void dequeue(struct Queue *queue) {

if (isEmpty(queue)) {

printf("Queue Underflow\n");

return;

struct Node *temp = queue->front;

int value = temp->data;

queue->front = temp->next;

free(temp);

if (queue->front == NULL) {

queue->rear = NULL;

printf("%d dequeued from the queue\n", value);

int main() {

struct Queue queue;

initializeQueue(&queue);

enqueue(&queue, 10);

enqueue(&queue, 20);

enqueue(&queue, 30);

dequeue(&queue);

dequeue(&queue);

dequeue(&queue);

dequeue(&queue); // Trying to dequeue from an empty queue

return 0;

}
Data Structures Lab
8. To create a circular queue and add an element and delete an element from a circular queue.
#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 5

struct CircularQueue {

int *arr;

int front, rear;

};

void initializeCircularQueue(struct CircularQueue *queue) {

queue->arr = (int *)malloc(MAX_SIZE * sizeof(int));

queue->front = queue->rear = -1;

int isFull(struct CircularQueue *queue) {

return (queue->front == 0 && queue->rear == MAX_SIZE - 1) || (queue->front == queue->rear + 1);

int isEmpty(struct CircularQueue *queue) {

return queue->front == -1;

void enqueueCircularQueue(struct CircularQueue *queue, int value) {

if (isFull(queue)) {

printf("Queue Overflow\n");

return;

if (isEmpty(queue)) {

queue->front = queue->rear = 0;

} else if (queue->rear == MAX_SIZE - 1) {


Data Structures Lab
queue->rear = 0;

} else {

queue->rear++;

queue->arr[queue->rear] = value;

printf("%d enqueued to the circular queue\n", value);

void dequeueCircularQueue(struct CircularQueue *queue) {

if (isEmpty(queue)) {

printf("Queue Underflow\n");

return;

int value = queue->arr[queue->front];

if (queue->front == queue->rear) {

queue->front = queue->rear = -1;

} else if (queue->front == MAX_SIZE - 1) {

queue->front = 0;

} else {

queue->front++;

printf("%d dequeued from the circular queue\n", value);

int main() {

struct CircularQueue queue;

initializeCircularQueue(&queue);
Data Structures Lab
enqueueCircularQueue(&queue, 10);

enqueueCircularQueue(&queue, 20);

enqueueCircularQueue(&queue, 30);

enqueueCircularQueue(&queue, 40);

enqueueCircularQueue(&queue, 50);

enqueueCircularQueue(&queue, 60); // Trying to enqueue to a full circular queue

dequeueCircularQueue(&queue);

dequeueCircularQueue(&queue);

dequeueCircularQueue(&queue);

enqueueCircularQueue(&queue, 70);

enqueueCircularQueue(&queue, 80);

return 0;

}
Data Structures Lab
9. To write a program of a structure containing an item name along with the unit price. The user enters
the item name and quantity to be purchased. Program print outs total price of item with name using
pointer ina structure or array in a structure.
#include <stdio.h>

#include <string.h>

struct Item {

char name[50];

float unitPrice;

int quantity;

};

float calculateTotal(struct Item *item) {

return item->unitPrice * item->quantity;

int main() {

struct Item item;

// Input item details

printf("Enter item name: ");

scanf("%s", item.name);

printf("Enter unit price: ");

scanf("%f", &item.unitPrice);

printf("Enter quantity: ");

scanf("%d", &item.quantity);

// Calculate and print total price

float total = calculateTotal(&item);

printf("Total price for %s: $%.2f\n", item.name, total);

return 0;

}
Data Structures Lab
10. To create a single linked list and — (a) insert a node in the list (before header node, in between two
nodes, end of the list); (b0 delete a node from the list (1st node, last node, in between two nodes); (c)
Concatenate two lists.
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

// Function to create a new node

struct Node *createNode(int value) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

return newNode;

// Function to insert a node at the beginning of the list

void insertAtBeginning(struct Node **head, int value) {

struct Node *newNode = createNode(value);

newNode->next = *head;

*head = newNode;

// Function to insert a node at the end of the list

void insertAtEnd(struct Node **head, int value) {

struct Node *newNode = createNode(value);

if (*head == NULL) {

*head = newNode;

return;

}
Data Structures Lab

struct Node *temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

// Function to insert a node after a specific node in the list

void insertAfterNode(struct Node *prevNode, int value) {

if (prevNode == NULL) {

printf("Previous node cannot be NULL\n");

return;

struct Node *newNode = createNode(value);

newNode->next = prevNode->next;

prevNode->next = newNode;

// Function to delete the first node of the list

void deleteFirstNode(struct Node **head) {

if (*head == NULL) {

printf("List is empty, cannot delete\n");

return;

struct Node *temp = *head;

*head = (*head)->next;

free(temp);

}
Data Structures Lab
// Function to delete the last node of the list

void deleteLastNode(struct Node **head) {

if (*head == NULL) {

printf("List is empty, cannot delete\n");

return;

if ((*head)->next == NULL) {

free(*head);

*head = NULL;

return;

struct Node *temp = *head;

while (temp->next->next != NULL) {

temp = temp->next;

free(temp->next);

temp->next = NULL;

// Function to delete a node with a specific value from the list

void deleteNodeWithValue(struct Node **head, int value) {

if (*head == NULL) {

printf("List is empty, cannot delete\n");

return;

struct Node *temp = *head;

struct Node *prev = NULL;

while (temp != NULL && temp->data != value) {


Data Structures Lab
prev = temp;

temp = temp->next;

if (temp == NULL) {

printf("Node with value %d not found\n", value);

return;

if (prev == NULL) {

*head = temp->next;

} else {

prev->next = temp->next;

free(temp);

// Function to concatenate two lists

void concatenateLists(struct Node **list1, struct Node *list2) {

if (*list1 == NULL) {

*list1 = list2;

} else {

struct Node *temp = *list1;

while (temp->next != NULL) {

temp = temp->next;

temp->next = list2;

// Function to display the linked list

void displayList(struct Node *head) {

struct Node *temp = head;


Data Structures Lab
while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

struct Node *list1 = NULL;

struct Node *list2 = NULL;

// Insert nodes into list1

insertAtEnd(&list1, 1);

insertAtEnd(&list1, 2);

insertAtEnd(&list1, 3);

// Display list1

printf("List 1: ");

displayList(list1);

// Insert nodes into list2

insertAtEnd(&list2, 4);

insertAtEnd(&list2, 5);

insertAtEnd(&list2, 6);

// Display list2

printf("List 2: ");

displayList(list2);

// Concatenate list2 to list1

concatenateLists(&list1, list2);

// Display the concatenated list

printf("Concatenated List: ");

displayList(list1);

// Example of other operations (insertions and deletions) can be performed similarly

return 0;

}
Data Structures Lab
11. To create a doubly linked list and — (a) insert a node in the list (before header node, in between two
nodes, end of the list); (b) delete a node from the list (1st node, last node, in between two nodes); (c)
Concatenate two lists
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *prev;

struct Node *next;

};

// Function to create a new node

struct Node *createNode(int value) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value;

newNode->prev = newNode->next = NULL;

return newNode;

// Function to insert a node at the beginning of the list

void insertAtBeginning(struct Node **head, int value) {

struct Node *newNode = createNode(value);

if (*head == NULL) {

*head = newNode;

} else {

newNode->next = *head;

(*head)->prev = newNode;

*head = newNode;

// Function to insert a node at the end of the list


Data Structures Lab
void insertAtEnd(struct Node **head, int value) {

struct Node *newNode = createNode(value);

if (*head == NULL) {

*head = newNode;

} else {

struct Node *temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

// Function to insert a node after a specific node in the list

void insertAfterNode(struct Node *prevNode, int value) {

if (prevNode == NULL) {

printf("Previous node cannot be NULL\n");

return;

struct Node *newNode = createNode(value);

newNode->prev = prevNode;

newNode->next = prevNode->next;

if (prevNode->next != NULL) {

prevNode->next->prev = newNode;

prevNode->next = newNode;

}
Data Structures Lab

// Function to delete the first node of the list

void deleteFirstNode(struct Node **head) {

if (*head == NULL) {

printf("List is empty, cannot delete\n");

return;

struct Node *temp = *head;

*head = (*head)->next;

if (*head != NULL) {

(*head)->prev = NULL;

free(temp);

// Function to delete the last node of the list

void deleteLastNode(struct Node **head) {

if (*head == NULL) {

printf("List is empty, cannot delete\n");

return;

struct Node *temp = *head;

while (temp->next != NULL) {

temp = temp->next;

if (temp->prev != NULL) {

temp->prev->next = NULL;

} else {
Data Structures Lab
*head = NULL;

free(temp);

// Function to delete a node with a specific value from the list

void deleteNodeWithValue(struct Node **head, int value) {

if (*head == NULL) {

printf("List is empty, cannot delete\n");

return;

struct Node *temp = *head;

while (temp != NULL && temp->data != value) {

temp = temp->next;

if (temp == NULL) {

printf("Node with value %d not found\n", value);

return;

if (temp->prev != NULL) {

temp->prev->next = temp->next;

} else {

*head = temp->next;

if (temp->next != NULL) {

temp->next->prev = temp->prev;

free(temp);

// Function to concatenate two lists

void concatenateLists(struct Node **list1, struct Node *list2) {

if (*list1 == NULL) {

*list1 = list2;

} else {
Data Structures Lab
struct Node *temp = *list1;

while (temp->next != NULL) {

temp = temp->next;

temp->next = list2;

list2->prev = temp;

// Function to display the doubly linked list

void displayList(struct Node *head) {

struct Node *temp = head;

while (temp != NULL) {

printf("%d <-> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

struct Node *list = NULL;

// Insert nodes into the doubly linked list

insertAtEnd(&list, 1);

insertAtEnd(&list, 2);

insertAtEnd(&list, 3);

// Display the doubly linked list

printf("Doubly Linked List: ");

displayList(list);

// Example of other operations (insertions and deletions) can be performed similarly

return 0;

}
Data Structures Lab
12. To create a circular linked list and insert & delete an element from the list
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

// Function to create a new node

struct Node *createNode(int value) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

return newNode;

// Function to insert a node at the end of the circular list

void insertAtEnd(struct Node **head, int value) {

struct Node *newNode = createNode(value);

if (*head == NULL) {

*head = newNode;

newNode->next = *head;

} else {

struct Node *temp = *head;

while (temp->next != *head) {

temp = temp->next;

temp->next = newNode;

newNode->next = *head;

}
Data Structures Lab
}

// Function to delete a node with a specific value from the circular list

void deleteNodeWithValue(struct Node **head, int value) {

if (*head == NULL) {

printf("List is empty, cannot delete\n");

return;

struct Node *temp = *head;

struct Node *prev = NULL;

do {

if (temp->data == value) {

if (temp == *head) {

// If the node to be deleted is the head

struct Node *lastNode = *head;

while (lastNode->next != *head) {

lastNode = lastNode->next;

if (*head == (*head)->next) {

// Only one node in the list

free(temp);

*head = NULL;

} else {

lastNode->next = temp->next;

*head = temp->next;

free(temp);

return;

} else {

prev->next = temp->next;

free(temp);

return;

}
Data Structures Lab
}

prev = temp;

temp = temp->next;

} while (temp != *head);

printf("Node with value %d not found\n", value);

// Function to display the circular linked list

void displayList(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("(head)\n");

int main() {

struct Node *circularList = NULL;

// Insert nodes into the circular linked list

insertAtEnd(&circularList, 1);

insertAtEnd(&circularList, 2);

insertAtEnd(&circularList, 3);

// Display the circular linked list

printf("Circular Linked List: ");

displayList(circularList);

// Example of other operations (insertions and deletions) can be performed similarly

return 0;

}
Data Structures Lab
13. Write a program to merge two shorted linked list.
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

// Function to create a new node

struct Node *createNode(int value) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

return newNode;

// Function to insert a node at the end of the linked list

void insertAtEnd(struct Node **head, int value) {

struct Node *newNode = createNode(value);

if (*head == NULL) {

*head = newNode;

} else {

struct Node *temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

}
Data Structures Lab
// Function to merge two sorted linked lists

struct Node *mergeSortedLists(struct Node *list1, struct Node *list2) {

struct Node *mergedList = NULL;

struct Node *temp = NULL;

while (list1 != NULL && list2 != NULL) {

if (list1->data < list2->data) {

insertAtEnd(&mergedList, list1->data);

list1 = list1->next;

} else {

insertAtEnd(&mergedList, list2->data);

list2 = list2->next;

while (list1 != NULL) {

insertAtEnd(&mergedList, list1->data);

list1 = list1->next;

while (list2 != NULL) {

insertAtEnd(&mergedList, list2->data);

list2 = list2->next;

return mergedList;

// Function to display the linked list

void displayList(struct Node *head) {

struct Node *temp = head;

while (temp != NULL) {

printf("%d -> ", temp->data);


Data Structures Lab
temp = temp->next;

printf("NULL\n");

int main() {

struct Node *list1 = NULL;

struct Node *list2 = NULL;

// Insert nodes into the first sorted linked list

insertAtEnd(&list1, 1);

insertAtEnd(&list1, 3);

insertAtEnd(&list1, 5);

// Display the first sorted linked list

printf("List 1: ");

displayList(list1);

// Insert nodes into the second sorted linked list

insertAtEnd(&list2, 2);

insertAtEnd(&list2, 4);

insertAtEnd(&list2, 6);

// Display the second sorted linked list

printf("List 2: ");

displayList(list2);

// Merge the two sorted linked lists

struct Node *mergedList = mergeSortedLists(list1, list2);

// Display the merged list

printf("Merged List: ");

displayList(mergedList);

return 0;

}
Data Structures Lab
14. Write a program to reverse a linked list.
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

// Function to create a new node

struct Node *createNode(int value) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

return newNode;

// Function to insert a node at the end of the linked list

void insertAtEnd(struct Node **head, int value) {

struct Node *newNode = createNode(value);

if (*head == NULL) {

*head = newNode;

} else {

struct Node *temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

}
Data Structures Lab
// Function to reverse a linked list

void reverseLinkedList(struct Node **head) {

struct Node *prev = NULL;

struct Node *current = *head;

struct Node *next = NULL;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

*head = prev;

// Function to display the linked list

void displayList(struct Node *head) {

struct Node *temp = head;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

struct Node *list = NULL;

// Insert nodes into the linked list

insertAtEnd(&list, 1);

insertAtEnd(&list, 2);

insertAtEnd(&list, 3);
Data Structures Lab
insertAtEnd(&list, 4);

// Display the original linked list

printf("Original Linked List: ");

displayList(list);

// Reverse the linked list

reverseLinkedList(&list);

// Display the reversed linked list

printf("Reversed Linked List: ");

displayList(list);

return 0;

}
Data Structures Lab
15. To write a program to calculate the binomial co-efficient of n C r of two numbers using recursive
function. Also write the same program using function in non-recursive way.
#include <stdio.h>

// Recursive function to calculate binomial coefficient

int binomialCoefficientRecursive(int n, int r) {

if (r == 0 || r == n) {

return 1;

} else {

return binomialCoefficientRecursive(n - 1, r - 1) + binomialCoefficientRecursive(n - 1, r);

// Non-recursive function to calculate binomial coefficient

int binomialCoefficientNonRecursive(int n, int r) {

int res = 1;

if (r > n - r) {

r = n - r;

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

res *= (n - i);

res /= (i + 1);

return res;

int main() {

int n, r;

printf("Enter values for n and r: ");

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

// Using recursive function

printf("Binomial Coefficient (Recursive): %d\n", binomialCoefficientRecursive(n, r));

// Using non-recursive function

printf("Binomial Coefficient (Non-Recursive): %d\n", binomialCoefficientNonRecursive(n, r));

return 0;

}
Data Structures Lab

16. To write a program to generate Fibonacci Series using recursive function. Also write the same
programusing function in non-recursive way.
#include <stdio.h>

// Recursive function to generate Fibonacci series

int fibonacciRecursive(int n) {

if (n <= 1) {

return n;

} else {

return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);

// Non-recursive function to generate Fibonacci series

void fibonacciNonRecursive(int n) {

int a = 0, b = 1, c;

printf("Fibonacci Series (Non-Recursive): ");

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

printf("%d ", a);

c = a + b;

a = b;

b = c;

printf("\n");

int main() {
Data Structures Lab
int n;

printf("Enter the number of terms for Fibonacci series: ");

scanf("%d", &n);

// Using recursive function

printf("Fibonacci Series (Recursive): ");

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

printf("%d ", fibonacciRecursive(i));

printf("\n");

// Using non-recursive function

fibonacciNonRecursive(n);

return 0;

}
Data Structures Lab
17. To write a program to create a binary tree and traverse it in pre-order and post-order form.
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *left;

struct Node *right;

};

// Function to create a new node

struct Node *createNode(int value) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value;

newNode->left = newNode->right = NULL;

return newNode;

// Function to traverse the binary tree in pre-order

void preOrderTraversal(struct Node *root) {

if (root != NULL) {

printf("%d ", root->data);

preOrderTraversal(root->left);

preOrderTraversal(root->right);

// Function to traverse the binary tree in post-order

void postOrderTraversal(struct Node *root) {

if (root != NULL) {

postOrderTraversal(root->left);

postOrderTraversal(root->right);

printf("%d ", root->data);


Data Structures Lab
}

int main() {

// Create a binary tree

struct Node *root = createNode(1);

root->left = createNode(2);

root->right = createNode(3);

root->left->left = createNode(4);

root->left->right = createNode(5);

// Traverse the binary tree in pre-order

printf("Pre-order Traversal: ");

preOrderTraversal(root);

printf("\n");

// Traverse the binary tree in post-order

printf("Post-order Traversal: ");

postOrderTraversal(root);

printf("\n");

return 0;

}
Data Structures Lab
18.To write a program to create a binary search tree and — (a) insert a new node in the BST (b) search
anode in the BST (c) delete a node from the BST
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *left;

struct Node *right;

};

// Function to create a new node

struct Node *createNode(int value) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = value;

newNode->left = newNode->right = NULL;

return newNode;

// Function to insert a new node in the BST

struct Node *insertNode(struct Node *root, int value) {

if (root == NULL) {

return createNode(value);

if (value < root->data) {

root->left = insertNode(root->left, value);

} else if (value > root->data) {

root->right = insertNode(root->right, value);

return root;

}
Data Structures Lab
// Function to search for a node in the BST

struct Node *searchNode(struct Node *root, int value) {

if (root == NULL || root->data == value) {

return root;

if (value < root->data) {

return searchNode(root->left, value);

return searchNode(root->right, value);

// Function to find the minimum value node in the BST

struct Node *findMin(struct Node *root) {

while (root->left != NULL) {

root = root->left;

return root;

// Function to delete a node from the BST

struct Node *deleteNode(struct Node *root, int value) {

if (root == NULL) {

return root;

if (value < root->data) {

root->left = deleteNode(root->left, value);

} else if (value > root->data) {

root->right = deleteNode(root->right, value);

} else {

if (root->left == NULL) {
Data Structures Lab
struct Node *temp = root->right;

free(root);

return temp;

} else if (root->right == NULL) {

struct Node *temp = root->left;

free(root);

return temp;

struct Node *temp = findMin(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

return root;

// Function to traverse the BST in-order

void inOrderTraversal(struct Node *root) {

if (root != NULL) {

inOrderTraversal(root->left);

printf("%d ", root->data);

inOrderTraversal(root->right);

int main() {

struct Node *root = NULL;

// Insert nodes into the BST

root = insertNode(root, 50);

root = insertNode(root, 30);

root = insertNode(root, 20);


Data Structures Lab
root = insertNode(root, 40);

root = insertNode(root, 70);

root = insertNode(root, 60);

root = insertNode(root, 80);

// Traverse the BST in-order

printf("In-order Traversal: ");

inOrderTraversal(root);

printf("\n");

// Search for a node in the BST

int searchValue = 40;

struct Node *searchResult = searchNode(root, searchValue);

if (searchResult != NULL) {

printf("%d found in the BST\n", searchValue);

} else {

printf("%d not found in the BST\n", searchValue);

// Delete a node from the BST

int deleteValue = 30;

root = deleteNode(root, deleteValue);

// Traverse the BST in-order after deletion

printf("In-order Traversal after deleting %d: ", deleteValue);

inOrderTraversal(root);

printf("\n");

return 0;

You might also like