B.
SC (Computer Science)
2nd Year 3rd Semester
SESSION: 2022-23
SUBJECT: Data Structure
SUBMITTED TO: SUBMITTED BY:
Mrs. Meenakshi Gupta Prashant Arya
INDEX
S.No Program Name Sign.
01. Write a program Stack.
02. Write a program Queue.
03. Write a program Linear Search.
04. Write a program Binary Search.
05. Write a program Insertion Sort.
06. Write a program Selection Sort.
07. Write a program 1-D Array.
08. Write a program 2-D Array.
09. Write a program of Sparse Matrix.
10. Write a program of Lower and Upper
Matrix.
1
1. Write a program of Stack.
#include<stdio.h>
#include<conio.h>
int MAXSIZE = 8 ;
int stacks[8] ;
int top = -1;
int isempty()
{
if (top == -1)
return 1;
else
return 0 ;
}
int isfull()
{
if(top == MAXSIZE)
return 1;
else
return 0;
}
int peek()
{
return stack[top];
}
int pop()
{
int data ;
if( ! isempty())
2
{
data = stack[top];
top = top – 1;
return data ;
}
else
{
printf(“Could not retrieve data, Stack is empty.\n”);
}
}
int push(int data)
{
if ( ! isfull())
{
top = top + 1 ;
stack[top] = data ;
}
else
{
printf(“Could not insert data, Stack is full.\n ”);
}
}
int main()
{
push(3);
push(5);
push(9);
push(1);
push(12);
3
push(15);
printf(“Element at top of the stack : %d\n”,peek());
printf(“Elements:\n”);
while(! isempty())
{
int data = pop();
printf(“%d\n”,data);
}
printf(“Stack full: %s\n”,isfull()?”true”:”false”);
printf(“Stack empty: %s\n”,isempty()?”true”:”false”);
return 0;
}
Output:-
Element at top of the stack: 15
Elements:
15
12
1
9
5
3
Stack full: false
Stack empty: true
4
2. Write a program of Queue.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h
#include<stdbool.h>
#define MAX 6
int int Array[MAX];
int front = 0 ;
int rear = -1 ;
int itemCount = 0;
int peek ()
{
return intArray[front] ;
}
bool isEmpty()
{
return itemCourt == 0;
}
bool isFull()
{
return itemCount == MAX ;
}
void insert(int data)
{
if( ! isFull)
{
if(rear == MAX-1)
{
rear = -1;
5
}
intArray[++rear]=data;
itemCourt++;
}
}
int removeData()
{
int data = intArray[front++];
if(front == MAX)
{
front = 0;
}
itemCourt-- ;
return data ;
}
int main()
insert (3);
insert (5);
insert (9);
insert (1);
insert (12);
insert (15);
if(isFull())
{
printf(“Queue is full \n”);
}
int num = removeData();
printf(“Element removed: %d\n”,num);
insert (16);
6
insert (17);
insert (18);
printf(“Element at front: %d\n”,peek());
printf(“--------------------\n”);
printf(“index : 5 4 3 2 1 0\n”);
printf(“--------------------\n”);
printf(“Queue: ”);
while( ! isEmpty())
{
int n = removeData();
printf(“%d”,n);
}
}
Output:-
Queue is full
Element removed: 3
Element at front: 5
--------------------------------------
index : 5 4 3 2 1 0
--------------------------------------
Queue : 5 9 1 12 15 16
--------------------------------------
7
3. Write a program of Linear Search.
#include<stdio.h>
#include<conio.h>
int main()
{
int list[20],size,i,element;
printf("Enter size of the list: ");
scanf("%d", &size);
printf("Enter any %d integer values: ",size);
for(i=0; i< size; i++)
{
scanf(“%d”,&list[i]);
}
printf("Enter the element to be Search: ");
scanf("%d", &element);
for(i=0; i< size; i++)
{
if(element == list[i])
{
printf("Element is found at %d index", i);
break;
}
}
if(i == size)
printf("Given element is not found in the list!!!");
getch();
}
8
Output:-
Enter the no of elements: 5
Enter 5 elements in array: 7
6
5
4
3
Sorted list in ascending order:
3
4
5
6
7
9
4. Write a program Binary Search.
#include <stdio.h>
int binarySearch(int arr[], int i, int r, int x)
{
if (r>= i)
{
int mid=i+(r-i)/2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, i, mid-1,x);
return binarySearch(arr, mid + 1, r,x);
}
return -1;
}
int main(void)
{
int arr[] = {2, 3, 4, 10, 40);
int n = sizeof(arr) / sizeof(arr[0]);
int x,i;
printf("Given array is: ");
for(i=0; i<5; i++)
{
printf("%d ",arr[i]);
}
printf("\nEnter any element to search: ");
scanf("%d",&x);
int result = binarySearch(arr, 0, n-1,x);
10
(result == -1)? printf("Element is not present in array")
: printf("Element is present at index %d",result);
return 0;
}
Output:-
Given array is: 2 3 4 10 40
Enter any element to search: 40
Element is present at index 4
11
5. Write a program Insertion Sort.
#include <stdio.h>
int main()
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i=1;i<=n-1; i++)
{
j=i;
while (j > 0 && arr[j-1]> arr[i])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i=0;i<=n-1; i++)
{
printf("%d\n", arr[i]);
12
}
return 0;
}
Output:-
Enter number of elements
5
Enter 5 integers
6
5
4
3
8
Sorted list in ascending order:
3
4
5
6
8
13
6. Write a program of Selection Sort.
#include <stdio.h>
vold swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b= temp;
}
void selectionSort(int array[], int size)
{
for (int step = 0; step < size - 1; step++)
{
int min_idx= step;
for (int i = step + 1; i < size; i++)
{
if (array[i] < array[min_idx])
min_idx = i;
}
swap(&array[min_idx], &array[step]);
}
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", array[i]);
}
printf(“\n”);
14
}
int main()
{
int data[] = {20,12,10,15,2};
int i;
printf(“Before Sorting: ”);
for(i=0;i<5;i++)
{
printf(“%d”,data[i]);
}
int size= sizeof(data)/sizeof(data[0]);
selectionSort(data,size);
printf(“\nSorted array in Ascending Order:\n”);
printArray(data, size);
}
Output:-
Before Sorting: 20 12 10 15 2
Sorted array in Ascending Order:
2 10 12 15 20
15
7. Write a program of 1-D Array.
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[10] , i , n ;
printf("Enter no of elements");
scanf("%d",&n);
printf("Enter %d elements in array: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("These are elememts of one-d array: \n");
for(i=0; i<n;i++)
{
printf("%d\n",arr[i]);
}
getch();
}
16
Output:-
Enter no of element: 4
Enter 4 element in array: 2
3
4
5
These are element of one – d array :
2
3
4
5
17
8. Write a program of 2-D Array.
#include<stdio.h>
#include<conio.h>
int main()
{
int array[2][3];
int i,j:
for(i=0;i<2; i++)
{
for(j=0; j<3; j++)
{
printf("Enter the elements of arrray[%d] [%d]: ",i,j);
scanf("%d",&array[i][j]);
}
}
printf("Elements of 2d array: \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",array[i][j]);
if(j==2)
{
printf("\n");
}
}
}
getch(); }
18
Output:-
Enter the elements of array[0][0]: 5
Enter the elements of array[0][1]: 6
Enter the elements of array[0][2]: 7
Enter the elements of array[1][0]: 4
Enter the elements of array[1][1]: 3
Enter the elements of array[1][2]: 2
Elements of 2d array:
5 6 7
4 3 2
19
9. Write a program Sparse Matrix.
#include<iostream>
int main()
{
int sparseMatrix[5][6]= {{0,0,0,0,9,0},{0.8,0,0,0,0},{4,0,0,2,0,0},
{0,0,0,0,0,5}. {0,0,2,0,0,0}};
int size = 0;
for (int row = 0; row <5; row++)
for (int column = 0; column < 6; column++)
if (sparseMatrix[row][column] != 0)
size++;
int result Matrix[3][size];
int k = 0;
for (int row = 0; row < 5; row++)
for (int column = 0; column <6; column++)
if (sparseMatrix[row][column] != 0)
{
resultMatrix[0][k] = row;
resultMatrix[1][k] = column;
resultMatrix[2][k] sparseMatrix[row][column];
k++;
}
cout<<"Representation Of a Sparse Matrix :"<<endl;
for (int row=0; row<3; row++)
{
for (int column= 0; column<size; column++)
cout<<resultMatrix[row][column]<<" ";
cout<<endl;
20
}
return 0;
}
Output:-
Representation of a Sparse Matrix :
0 1 2 2 3 4
4 1 0 3 5 2
9 8 4 2 5 2
21
10. Write a program of Lower and Upper Matrix.
#include<iostream>
void lower(int matrix[3][3], int row, int col)
{
int i, j;
for (i=0; i<row; i++)
{
for (j = 0; j<col; j++)
{
if (i<j)
{
cout << "0" << " ";
}
else
cout << matrix[i] << " ";
}
cout << endl;
}
}
void upper(int matrix[3][3], int row, int col)
int i, j;
for (i=0; i < row; i++)
{
for (j=0; j<col; j++)
{
if (i > j)
{
cout << "0" << " ";
22
}
else
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int matrix[3][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};
int row = 3, col = 3;
cout << "Lower triangular matrix: \n";
lower(matrix, row, col);
cout << "Upper triangular matrix: \n";
upper(matrix, row, col);
return 0;
}
Output:-
Lower triangular matrix:
1 0 0
4 5 0
7 8 9
Upper triangular matrix:
1 2 3
0 5 6
0 0 9
23