Data Structure Lab Mannual
Data Structure Lab Mannual
Data Structure
Lab Manual
// Output
int large = arr1[0];
int seclarge = arr1[0];
for (int i = 1; i < size; i++)
{
}
printf("Largest Number is : %d\n",large);
printf("Second Largest Number is : %d",seclarge);
}
Output:
Enter the size of Array :5
Enter Array Elements
8
9
0
1
1
87
Largest Number is :87
Second Largest Number is :9
Program 2: Write a program to reverse the contents of an array.
Program:
// WAP to Reverse a given array
#include <stdio.h>
int main()
{
int size;
int i, j;
int temp;
// Reverse Operation
while (i < j)
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
i++;
j--;
}
2
return 0;
Output:
Enter the size of Array :5
Enter Array Elements
8
4
0
90
7
Original Array: 8 4 0 90 7
Reversed Array: 7 90 0 4 8
3
int temp = arr1[0];
for (i = 1; i <= size; i++)
{
arr1[i - 1] = arr1[i];
}
arr1[size - 1] = temp;
}
Output:
Enter the size of Array :5
Enter Array Elements
7
8
0
1
2
Enter the value of n :1
After Rotating an Array(1 times Rotation) :8 0 1 2 7
4
int arr1[size1];
int arr2[size2];
int arr3[size1 + size2];
printf("Enter the %d elements of 1st array in Ascending Order \n",size1);
for (i = 0; i < size1; i++)
{
scanf("%d", &arr1[i]);
}
k++;
}
// Copy remaining elements of arr1 if any
while (i < size1){
arr3[k] = arr1[i];
i++;
k++;
}
// Copy remaining elements of arr2 if any
while (j < size2){
arr3[k] = arr2[j];
j++;
k++;
}
5
printf("%d ", arr1[i]);
}
printf("\nThe Second array elements are \n");
for (j = 0; j < size2; j++)
{
printf("%d ", arr2[j]);
}
// Display the merged array
printf("\nMerged sorted array:\n");
for (k = 0; k < size1 + size2; k++)
{
printf("%d ", arr3[k]);
}
return 0;
}
Output:
6
int arr1[50][50];
int sp[100][3];
printf("Enter the no of Row :");
scanf("%d",&row);
printf("Enter the o of Col :");
scanf("%d",&col);
printf("Enter the elements of sparse matrix\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
scanf("%d", &arr1[i][j]);
if (arr1[i][j] != 0)
{
count++;
}
}
}
sp[0][0] = row;
sp[0][1] = col;
sp[0][2] = count;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (arr1[i][j] != 0)
{
sp[r][0] = i;
sp[r][1] = j;
sp[r][2] = arr1[i][j];
r++;
}
}
}
printf("Originsl Matrix\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("%d\t", arr1[i][j]);
}
printf("\n");
}
printf("\n");
printf("Sparse Matrix\n");
7
for (i = 0; i < r; i++)
{
int main()
{
int size1, size2, size3;
printf("Enter the size of 1st polynomial :");
scanf("%d", &size1);
printf("Enter the size of 2nd polynomiAal :");
scanf("%d", &size2);
size3 = size1 + size2;
int arr1[2][size1], arr2[2][size2], sum[2][size3];
8
printf("Enter the values of second Polynomial \n");
for (i = 0; i < size2; i++)
{
scanf("%d %d", &arr2[0][i], &arr2[1][i]);
}
i = 0;
j = 0;
k = 0;
9
while (j < size2)
{
sum[0][k] = arr2[0][j];
sum[1][k] = arr2[1][j];
j++;
k++;
}
Output:
10
Program 7: WAP to find the Transpose of a square matrix without using any additional
array. Transpose to be created in a given matrix itself.
Program:
#include<stdio.h>
int main()
{
int row_size,col_size;
printf("Enter the size of Row :");
scanf("%d",&row_size);
printf("Enter the size of Column :");
scanf("%d",&col_size);
int arr1[row_size][col_size],arr2[row_size][col_size];
int i,j;
printf("Enter the elements of Matrix \n");
for(i=0;i<row_size;i++){
for(j=0;j<col_size;j++)
{
11
scanf("%d",&arr1[i][j]);
}
}
printf("\nOriginal Matrix \n");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
printf("%d ",arr1[i][j]);
}
printf("\n");
}
printf("Transpose Matrix \n");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
arr2[j][i]=arr1[i][j];
printf("%d ",arr1[j][i]);
}
printf("\n");
}
}
Program:
12
Program 8: WAP to implement Towers of Hanoi Problem.
Program:
//WAP to implement Towers of Hanoi Problem
#include <stdio.h>
#include<conio.h>
static int count=0;
int main()
{
int a=1,b=2,c=3,k;
void tower(int,int,int,int);
printf("Enter the numbers of disks in the Tower\n");
scanf("%d",&k);
printf("\n");
tower(k,a,b,c);
printf("\nTotal value :%d",count);
return 0;
}
tower(n-1,y,x,z);
}
}
Output:
13
Program 9: WAP to implement Ackermann’s function.
Program:
#include <stdio.h>
// Function to implement Ackermann's function
int ackermann(int m, int n) {
if (m == 0)
return n + 1;
else if (n == 0)
return ackermann(m - 1, 1);
else
return ackermann(m - 1, ackermann(m, n - 1));
}
int main() {
int m, n;
if (m < 0 || n < 0) {
printf("Only non-negative integers are allowed.\n");
return 1;
}
14
int result = ackermann(m, n);
printf("Ackermann(%d, %d) = %d\n", m, n, result);
return 0;
}
Output:
Program:
//Linked list representation in C
// This program creates a linked list and prints its contents.
#include <stdio.h>
#include<stdlib.h>
struct linklist
{
int info;
struct linklist *next;
};
int main()
{
node *head;
create(head);
print(head);
return 0;
}
15
void create(node *list)
{
printf("Enter the Data :");
scanf("%d", &list->info);
if (list->info == -1)
list->next = NULL;
else{
list->next = (node *)malloc(sizeof(node));
create(list->next);
}
}
void print(node *list)
{
printf("\nLinkedList Contenets : ");
while (list != NULL)
{
printf("%d ", list->info);
list = list->next;
}
}
Output:
Program 11: Wap to delete the first and last node of Singly LinkedList.
Program:
//Linked list Representation and print its contents
#include <stdio.h>
#include<stdlib.h>
struct linklist
{
int info;
struct linklist *next;
};
16
int main()
{
node *head;
create(head);
print(head);
print(head);
printf("\n\nAfter Deleting First Node\n");
head = head->next;
print(head);
return 0;
}
void create(node *list)
{
printf("Enter the Data :");
scanf("%d", &list->info);
if (list->info == -1)
list->next = NULL;
else{
list->next = (node *)malloc(sizeof(node));
create(list->next);
}
}
void print(node *list)
17
node *y;
while(x->next!=NULL)
{
y=x;
x=x->next;
}
y->next=NULL;
}
Output:
Program 12: Write a program to add a new node in the beginning/ as the last node in a
singly linked list.
Program:
#include <stdio.h>
#include <stdlib.h>
18
// Function to print the linked list
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main function
int main() {
struct Node* head = NULL;
// Adding nodes
addAtEnd(&head, 10); // List: 10
addAtEnd(&head, 20); // List: 10 -> 20
addAtBeginning(&head, 5); // List: 5 -> 10 -> 20
addAtEnd(&head, 30); // List: 5 -> 10 -> 20 -> 30
printf("Linked List:\n");
printList(head);
return 0;
}
19
Output:
20
current->next = prev; // Reverse current node's pointer
prev = current; // Move prev to current
current = next; // Move to next node
}
return prev; // New head
}
// Main function
int main() {
// Creating a simple linked list: 10 -> 20 -> 30 -> 40 -> NULL
struct Node* head = createNode(10);
head->next = createNode(20);
head->next->next = createNode(30);
head->next->next->next = createNode(40);
printf("Original List:\n");
printList(head);
head = reverseList(head);
printf("Reversed List:\n");
printList(head);
return 0;
}
Output:
int main() {
int myArray[] = {64, 34, 25, 12, 22, 11, 90, 5};
int n = sizeof(myArray) / sizeof(myArray[0]);
21
quicksort(myArray, 0, n - 1);
Program 15: Write a program to find the Kth largest element from an array using the
technique used in Quick Sort.
Program:
#include <stdio.h>
22
int temp = *a;
*a = *b;
*b = temp;
}
if (index == k - 1)
return arr[index];
else if (index > k - 1)
return findKthLargest(arr, low, index - 1, k);
else
return findKthLargest(arr, index + 1, high, k);
}
return -1; // not found
}
// Main function
int main() {
int arr[] = {12, 3, 5, 7, 19, 0, 4};
int n = sizeof(arr) / sizeof(arr[0]);
int k;
printf("Enter the Largest no element:");
scanf("%d",&k);
23
printf("Invalid input.\n");
return 0;
}
Output:
Enter the largest no Element: 4
The 4th largest element is 5
int main() {
int arr[100], n, i;
24
}
return 0;
}
Output:
25
Program 17: Write a program to implement insertion sort.
Program:
#include <stdio.h>
// Move elements that are greater than key to one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
int main() {
int arr[100], n, i;
return 0;
26
}
Output:
int main() {
27
int arr[100], n, i;
return 0;
}
Output:
28
Program 19: Implement Linear Search. Write a module to add the element at the Kth
position if the searching is a failure.
Program:
#include <stdio.h>
int main() {
int arr[100], n, i, key, k, pos;
29
// Input number of elements
printf("Enter number of elements: ");
scanf("%d", &n);
if (pos != -1) {
printf("Element found at position %d\n", pos);
} else {
printf("Element not found.\n");
return 0;
}
Output:
30
Program 20: Write a program to implement Binary search.
Program:
#include <stdio.h>
if (arr[mid] == key)
return mid; // Element found
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
int main() {
31
int arr[100], n, i, key, result;
// Output result
if (result != -1)
printf("Element found at position %d\n", result);
else
printf("Element not found.\n");
return 0;
}
Output:
32
--------------------*-------------------*-----------------------
End.
33