Data Structures Lab
Data Structures Lab
Data Structures Lab
#include <string.h>
return 1; // Palindrome
int main() {
char word[100];
scanf("%s", word);
if (isPalindrome(word)) {
} else {
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() {
scanf("%d", &rows);
scanf("%d", &cols);
int matrix[rows][cols];
// Input
scanf("%d", &matrix[i][j]);
printf("Row sums:\n");
int rowSum = 0;
rowSum += matrix[i][j];
printf("Column sums:\n");
int colSum = 0;
Data Structures Lab
for (int i = 0; i < rows; i++) {
colSum += matrix[i][j];
int grandTotal = 0;
grandTotal += matrix[i][j];
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
result[i][j] = 0;
printf("%d\t", mat[i][j]);
printf("\n");
int main() {
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;
};
stack->top = -1;
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
stack->arr[++stack->top] = value;
}
Data Structures Lab
void pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return;
int main() {
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
pop(&stack);
pop(&stack);
pop(&stack);
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>
struct Stack {
char *arr;
int top;
};
stack->top = -1;
stack->arr[++stack->top] = ch;
return stack->arr[stack->top--];
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;
initialize(&stack, MAX_SIZE);
int i, j;
i = j = 0;
if (isOperand(current)) {
postfix[j++] = current;
push(&stack, current);
postfix[j++] = pop(&stack);
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];
scanf("%s", infix);
infixToPostfix(infix, postfix);
return 0;
}
Data Structures Lab
6. To evaluate a postfix expression
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct Stack {
int *arr;
int top;
};
stack->top = -1;
stack->arr[++stack->top] = value;
return stack->arr[stack->top--];
initialize(&stack, MAX_SIZE);
Data Structures Lab
int i = 0;
if (isdigit(current)) {
} else {
switch (current) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
i++;
free(stack.arr);
Data Structures Lab
return result;
int main() {
char postfix[MAX_SIZE];
scanf("%s", postfix);
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 Queue {
};
newNode->data = value;
newNode->next = NULL;
if (isEmpty(queue)) {
} else {
queue->rear->next = newNode;
queue->rear = newNode;
if (isEmpty(queue)) {
printf("Queue Underflow\n");
return;
queue->front = temp->next;
free(temp);
if (queue->front == NULL) {
queue->rear = NULL;
int main() {
initializeQueue(&queue);
enqueue(&queue, 10);
enqueue(&queue, 20);
enqueue(&queue, 30);
dequeue(&queue);
dequeue(&queue);
dequeue(&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;
};
if (isFull(queue)) {
printf("Queue Overflow\n");
return;
if (isEmpty(queue)) {
queue->front = queue->rear = 0;
} else {
queue->rear++;
queue->arr[queue->rear] = value;
if (isEmpty(queue)) {
printf("Queue Underflow\n");
return;
if (queue->front == queue->rear) {
queue->front = 0;
} else {
queue->front++;
int main() {
initializeCircularQueue(&queue);
Data Structures Lab
enqueueCircularQueue(&queue, 10);
enqueueCircularQueue(&queue, 20);
enqueueCircularQueue(&queue, 30);
enqueueCircularQueue(&queue, 40);
enqueueCircularQueue(&queue, 50);
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;
};
int main() {
scanf("%s", item.name);
scanf("%f", &item.unitPrice);
scanf("%d", &item.quantity);
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;
};
newNode->data = value;
newNode->next = NULL;
return newNode;
newNode->next = *head;
*head = newNode;
if (*head == NULL) {
*head = newNode;
return;
}
Data Structures Lab
temp = temp->next;
temp->next = newNode;
if (prevNode == NULL) {
return;
newNode->next = prevNode->next;
prevNode->next = newNode;
if (*head == NULL) {
return;
*head = (*head)->next;
free(temp);
}
Data Structures Lab
// Function to delete the last node of the list
if (*head == NULL) {
return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
temp = temp->next;
free(temp->next);
temp->next = NULL;
if (*head == NULL) {
return;
temp = temp->next;
if (temp == NULL) {
return;
if (prev == NULL) {
*head = temp->next;
} else {
prev->next = temp->next;
free(temp);
if (*list1 == NULL) {
*list1 = list2;
} else {
temp = temp->next;
temp->next = list2;
temp = temp->next;
printf("NULL\n");
int main() {
insertAtEnd(&list1, 1);
insertAtEnd(&list1, 2);
insertAtEnd(&list1, 3);
// Display list1
printf("List 1: ");
displayList(list1);
insertAtEnd(&list2, 4);
insertAtEnd(&list2, 5);
insertAtEnd(&list2, 6);
// Display list2
printf("List 2: ");
displayList(list2);
concatenateLists(&list1, list2);
displayList(list1);
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;
};
newNode->data = value;
return newNode;
if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
if (prevNode == NULL) {
return;
newNode->prev = prevNode;
newNode->next = prevNode->next;
if (prevNode->next != NULL) {
prevNode->next->prev = newNode;
prevNode->next = newNode;
}
Data Structures Lab
if (*head == NULL) {
return;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
free(temp);
if (*head == NULL) {
return;
temp = temp->next;
if (temp->prev != NULL) {
temp->prev->next = NULL;
} else {
Data Structures Lab
*head = NULL;
free(temp);
if (*head == NULL) {
return;
temp = temp->next;
if (temp == NULL) {
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);
if (*list1 == NULL) {
*list1 = list2;
} else {
Data Structures Lab
struct Node *temp = *list1;
temp = temp->next;
temp->next = list2;
list2->prev = temp;
temp = temp->next;
printf("NULL\n");
int main() {
insertAtEnd(&list, 1);
insertAtEnd(&list, 2);
insertAtEnd(&list, 3);
displayList(list);
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;
};
newNode->data = value;
newNode->next = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
} else {
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
if (*head == NULL) {
return;
do {
if (temp->data == value) {
if (temp == *head) {
lastNode = lastNode->next;
if (*head == (*head)->next) {
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;
if (head == NULL) {
printf("List is empty\n");
return;
do {
temp = temp->next;
printf("(head)\n");
int main() {
insertAtEnd(&circularList, 1);
insertAtEnd(&circularList, 2);
insertAtEnd(&circularList, 3);
displayList(circularList);
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;
};
newNode->data = value;
newNode->next = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
}
Data Structures Lab
// Function to merge two sorted linked lists
insertAtEnd(&mergedList, list1->data);
list1 = list1->next;
} else {
insertAtEnd(&mergedList, list2->data);
list2 = list2->next;
insertAtEnd(&mergedList, list1->data);
list1 = list1->next;
insertAtEnd(&mergedList, list2->data);
list2 = list2->next;
return mergedList;
printf("NULL\n");
int main() {
insertAtEnd(&list1, 1);
insertAtEnd(&list1, 3);
insertAtEnd(&list1, 5);
printf("List 1: ");
displayList(list1);
insertAtEnd(&list2, 2);
insertAtEnd(&list2, 4);
insertAtEnd(&list2, 6);
printf("List 2: ");
displayList(list2);
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;
};
newNode->data = value;
newNode->next = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
}
Data Structures Lab
// Function to reverse a linked list
next = current->next;
current->next = prev;
prev = current;
current = next;
*head = prev;
temp = temp->next;
printf("NULL\n");
int main() {
insertAtEnd(&list, 1);
insertAtEnd(&list, 2);
insertAtEnd(&list, 3);
Data Structures Lab
insertAtEnd(&list, 4);
displayList(list);
reverseLinkedList(&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>
if (r == 0 || r == n) {
return 1;
} else {
int res = 1;
if (r > n - r) {
r = n - r;
res *= (n - i);
res /= (i + 1);
return res;
int main() {
int 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>
int fibonacciRecursive(int n) {
if (n <= 1) {
return n;
} else {
void fibonacciNonRecursive(int n) {
int a = 0, b = 1, c;
c = a + b;
a = b;
b = c;
printf("\n");
int main() {
Data Structures Lab
int n;
scanf("%d", &n);
printf("\n");
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;
};
newNode->data = value;
return newNode;
if (root != NULL) {
preOrderTraversal(root->left);
preOrderTraversal(root->right);
if (root != NULL) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
int main() {
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
preOrderTraversal(root);
printf("\n");
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;
};
newNode->data = value;
return newNode;
if (root == NULL) {
return createNode(value);
return root;
}
Data Structures Lab
// Function to search for a node in the BST
return root;
root = root->left;
return root;
if (root == NULL) {
return root;
} else {
if (root->left == NULL) {
Data Structures Lab
struct Node *temp = root->right;
free(root);
return temp;
free(root);
return temp;
root->data = temp->data;
return root;
if (root != NULL) {
inOrderTraversal(root->left);
inOrderTraversal(root->right);
int main() {
inOrderTraversal(root);
printf("\n");
if (searchResult != NULL) {
} else {
inOrderTraversal(root);
printf("\n");
return 0;