0% found this document useful (0 votes)
0 views

Data Structure 1

The document contains a series of C programming exercises related to data structures, including matrix multiplication, operations on 2D arrays, recursion for factorial calculation, quicksort implementation, linked list operations, and tree traversals. Each section includes code snippets and expected outputs for various operations. The exercises are designed to demonstrate fundamental programming concepts and data structure manipulations.

Uploaded by

rahulmoriya2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Data Structure 1

The document contains a series of C programming exercises related to data structures, including matrix multiplication, operations on 2D arrays, recursion for factorial calculation, quicksort implementation, linked list operations, and tree traversals. Each section includes code snippets and expected outputs for various operations. The exercises are designed to demonstrate fundamental programming concepts and data structure manipulations.

Uploaded by

rahulmoriya2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

DAV CENTENARY COLLEGE

Data Structure
(23BCA402DS02)

CLASS :

Roll no. :

Submitted by Submitted to
9. Write a Program for Matrix Multiplication.
#include<stdio.h>
int main(){
int a[3][3],b[3][3],c[3][3],i,j,k;
// enter the values of first matrix
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("\nenter the values of first matrix :");
scanf(" %d",&a[i][j]);}}
//enter the values of second matrix
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("\nenter the values of second matrix :");
scanf(" %d",&b[i][j]);}}
//printing the first matrix
printf("\nthe first matrix is :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d\t",a[i][j]);}}
//printing the value of second matrix
printf("\nthe second matrix is :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d\t",b[i][j]); }}
//multiplication of two matrix
printf("\nthe multiplication of two matrix are :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
c[i][j]=0;
for(k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j]; }
printf("%d\t",c[i][j]);}}
return 0;}

output:
enter the values of first matrix :1
enter the values of first matrix :2
enter the values of first matrix :3
enter the values of first matrix :4
enter the values of first matrix :5
enter the values of first matrix :6
enter the values of first matrix :7
enter the values of first matrix :8
enter the values of first matrix :9
enter the values of second matrix :1
enter the values of second matrix :2
enter the values of second matrix :3
enter the values of second matrix :4
enter the values of second matrix :5
enter the values of second matrix :6
enter the values of second matrix :7
enter the values of second matrix :8
enter the values of second matrix :9
the first matrix is :
1 2 3
4 5 6
7 8 9
the second matrix is :
1 2 3
4 5 6
7 8 9
the multiplication of two matrix are :
30 36 42
66 81 96
102 126 150

10. Write a program of operations of 2D Array(addition,


subtraction, transpose).
#include<stdio.h>
int main(){
int a[3][3],b[3][3],c[3][3],i,j;
//enter values of first matrix
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("\nenter the values of first matrix:");
scanf(" %d",&a[i][j]);}}
//enter the value of second matrix
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("\nenter the values of second matrix:");
scanf(" %d",&b[i][j]); } }
//printing the first matrix
printf("\nthe first matrix is :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d\t",a[i][j]);}}
//printing the second matrix
printf("\nthe second matrix is :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d\t",b[i][j]);}}
//sum of matrix
printf("\nsum of first and second matrix is :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);}}
//difference of matrix
printf("\ndifference of first and second matrix is :");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
c[i][j]=a[i][j]-b[i][j];
printf("%d\t",c[i][j]);}}
return 0;
}

output:
enter the values of first matrix:1
enter the values of first matrix:2
enter the values of first matrix:3
enter the values of first matrix:4
enter the values of first matrix:5
enter the values of first matrix:6
enter the values of first matrix:7
enter the values of first matrix:8
enter the values of first matrix:9
enter the values of second matrix:1
enter the values of second matrix:2
enter the values of second matrix:3
enter the values of second matrix:4
enter the values of second matrix:5
enter the values of second matrix:6
enter the values of second matrix:7
enter the values of second matrix:8
enter the values of second matrix:9
the first matrix is :
1 2 3
4 5 6
7 8 9

the second matrix is :


1 2 3
4 5 6
7 8 9
sum of first and second matrix is :
2 4 6
8 10 12
14 16 18
difference of first and second matrix is :
0 0 0
0 0 0
0 0 0

14. write a program of factorial using recursion.


ANSWER:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
OUTPUT :
Enter a positive integer: 6
Factorial of 6 = 720
15.write a program of quick short.
ANSWER:
#include <stdio.h>

void quicksort(int array[], int low, int high);


int partition(int array[], int low, int high);

int main() {
int myArray[] = {64, 34, 25, 12, 22, 11, 90, 5};
int n = sizeof(myArray) / sizeof(myArray[0]);

quicksort(myArray, 0, n - 1);

printf("Sorted array: ");


for (int i = 0; i < n; i++) {
printf("%d ", myArray[i]);
}
return 0;
}

void quicksort(int array[], int low, int high) {


if (low < high) {
int pivotIndex = partition(array, low, high);
quicksort(array, low, pivotIndex - 1);
quicksort(array, pivotIndex + 1, high);
}
}

int partition(int array[], int low, int high) {


int pivot = array[high];
int i = low - 1;

for (int j = low; j < high; j++) {


if (array[j] <= pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

int temp = array[i + 1];


array[i + 1] = array[high];
array[high] = temp;
return i + 1;
}
OUTPUT:
Sorted array: 5 11 12 22 25 34 64 90
16.write a program of link list implementation.
ANSWER:
// // C Program for Implementation of Singly Linked List
#include <stdio.h>
#include <stdlib.h>

// Define the Node structure


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));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a new element at the beginning of the singly


linked list
void insertAtFirst(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

// Function to insert a new element at the end of the singly linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

// Function to insert a new element at a specific position in the singly


linked list
void insertAtPosition(struct Node** head, int data, int position) {
struct Node* newNode = createNode(data);
if (position == 0) {
insertAtFirst(head,data);
return;
}
struct Node* temp = *head;
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
// Function to delete the first node of the singly linked list
void deleteFromFirst(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = temp->next;
free(temp);
}

// Function to delete the last node of the singly linked list


void deleteFromEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (temp->next == NULL) {
free(temp);
*head = NULL;
return;
}
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}

// Function to delete a node at a specific position in the singly linked


list
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (position == 0) {
deleteFromFirst(head);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("Position out of range\n");
return;
}
struct Node* next = temp->next->next;
free(temp->next);
temp->next = next;
}

// Function to print the LinkedList


void print(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Driver Code
int main() {
struct Node* head = NULL;

insertAtFirst(&head, 10);
printf("Linked list after inserting the node:10 at the beginning \n");
print(head);

printf("Linked list after inserting the node:20 at the end \n");


insertAtEnd(&head, 20);
print(head);

printf("Linked list after inserting the node:5 at the end \n");


insertAtEnd(&head, 5);
print(head);

printf("Linked list after inserting the node:30 at the end \n");


insertAtEnd(&head, 30);
print(head);

printf("Linked list after inserting the node:15 at position 2 \n");


insertAtPosition(&head, 15, 2);
print(head);
printf("Linked list after deleting the first node: \n");
deleteFromFirst(&head);
print(head);

printf("Linked list after deleting the last node: \n");


deleteFromEnd(&head);
print(head);

printf("Linked list after deleting the node at position 1: \n");


deleteAtPosition(&head, 1);
print(head);

return 0;
}
OUTPUT:
Linked list after inserting the node:10 at the beginning
10 -> NULL
Linked list after inserting the node:20 at the end
10 -> 20 -> NULL
Linked list after inserting the node:5 at the end
10 -> 20 -> 5 -> NULL
Linked list after inserting the node:30 at the end
10 -> 20 -> 5 -> 30 -> NULL
Linked list after inserting the node:15 at position 2
10 -> 20 -> 15 -> 5 -> 30 -> NULL
Linked list after deleting the first node:
20 -> 15 -> 5 -> 30 -> NULL
Linked list after deleting the last node:
20 -> 15 -> 5 -> NULL
Linked list after deleting the node at position 1:
20 -> 5 -> NULL
17.write a program of tree traversals.
ANSWER:
// Tree traversal in C

#include <stdio.h>
#include <stdlib.h>

struct node {
int item;
struct node* left;
struct node* right;
};

// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}

// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

// postorderTraversal traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}

// Create a new Node


struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

// Insert on the left of the node


struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}

// Insert on the right of the node


struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}

int main() {
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);

insertLeft(root->left, 5);
insertRight(root->left, 6);

printf("Inorder traversal \n");


inorderTraversal(root);

printf("\nPreorder traversal \n");


preorderTraversal(root);

printf("\nPostorder traversal \n");


postorderTraversal(root);
}
OUTPUT:
Inorder traversal 5 ->12 ->6 ->1 ->9 ->
Preorder traversal 1 ->12 ->5 ->6 ->9 ->
Postorder traversal 5 ->6 ->12 ->9 ->1 ->

You might also like