Data Structure Practical File (2) .
Data Structure Practical File (2) .
CT University, Ludhiana
Submitted in partial fulfilment of the requirement for the award of the
Degree of Bachelors of Computer Science and Engineering
1 | Page
REGISTRATION NO: 72212532 MRS. NIKITA
SINGLA
REGISTRATION NO.: 1544545445
TABLE OF CONTENTS
2 | Page
10 Write a program to check the prime number. 12
___________________
TEACHER SIGNATURE
PRACTICAL-1
Q1. Write a program to create an array.
//1.Write a program to create an array.
#include<stdio.h>
int main()
{
int arr[10],i,j;
for(i=0;i<10;i++){
arr[i]=i+100;
}
3 | Page
for(j=0;j<10;j++){
printf("Elements[%d]= %d\n",j,arr[j]);
}
return 0;
}
PRACTICAL-2
Q2. Write a program to input elements into the array and print all elements.
//2.Program to input elements into the array and print all elements.
#include<stdio.h>
int main()
{
int arr[100],i,n;
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter the elements into array:\n");
for(i=0;i<n;i++){
4 | Page
scanf("%d",&arr[i]);
}
printf("\nElements of the array are:\n");
for(i=0;i<n;i++){
printf("%d ,",arr[i]);
}
return 0;
}
PRACTICAL-3
Q3. Write a program to find the sum of numbers in the array.
//3.Program to find the sum of numbers in the array.
#include<stdio.h>
int main()
{
int num[10],i,sum=0;
printf("Enter the 10 numbers:\n");
for(i=0;i<10;i++){
scanf("%d",&num[i]);
sum+=num[i];
5 | Page
}
printf("Sum of the entered numbers is = %d",sum);
return 0;
}
PRACTICAL-4
Q4. Write a program to find size of the array.
//4.Program to find size of the array.
#include<stdio.h>
int main()
{
int arr[]={1,2,3,4,5};
int length=sizeof(arr)/sizeof(arr[0]);
6 | Page
return 0;
}
PRACTICAL-5
Q5. Write a program to count number of positive and negative numbers in the
array.
//5.Program to count number of positive and negative numbers in the
array.
#include<stdio.h>
int main()
{
int arr[100],i,n,cn=0,cp=0;
printf("Enter the size of the array: ");
scanf("%d",&n);
7 | Page
printf("Enter the elements of the array: \n");
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++){
if(arr[i]<0){
cn++;
}
else{
cp++;
}
}
printf("There are %d negative numbers in the array.\n",cn);
printf("There are %d positive numbers in the array.\n",cp);
return 0;
}
PRACTICAL-6
Q6. Write a program to insert element in the array.
//6.Program to insert element in the array.
#include <stdio.h>
int main()
{
int arr[100],pos,c,n,val;
printf("Enter number of elements in array :\n");
scanf("%d", &n);
printf("Enter %d elements :\n", n);
for(c=0;c<n;c++){
scanf("%d",&arr[c]);
8 | Page
}
printf("Enter the location where you wish to insert an element :\n");
scanf("%d",&pos);
printf("Enter the value to insert:\n");
scanf("%d",&val);
for(c=n-1;c>=pos-1;c--){
arr[c+1]=arr[c];
arr[pos]=val;
}
printf("Resultant array is:\n");
for(c=0;c<=n;c++){
printf("%d ,",arr[c]);
}
return 0;
}
PRACTICAL-7
Q7. Write a program to do deletion in an array.
//7.Program to do deletion in an array.
#include <stdio.h>
int main()
{
int arr[100],pos,c,n;
printf("Enter number of elements in array:\n");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(c=0;c<n;c++){
scanf("%d",&arr[c]);
}
printf("Enter the location where you wish to delete element\n");
9 | Page
scanf("%d", &pos);
if (pos>=n+1){
printf("Deletion not possible.\n");
}
else{
for (c=pos-1;c<n-1;c++)
arr[c]=arr[c+1];
}
printf("Resultant array:\n");
for (c=0;c<n-1;c++){
printf("%d ,", arr[c]);
}
return 0;
}
PRACTICAL-8
Q8. Write a program to find the median of the numbers in the array.
//8.Program to find the median of the numbers in the array.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a[50];
int n;
cout<<"Enter number of elements of array: ";
cin>>n;
cout<<"Enter array elements\n";
for(int i=0;i<n;i++){
cin>>a[i];
10 | Page
}
int m=n/2;
int result;
if(n%2==0){
float w=(a[m]+a[m-1])/2.0;
result=ceil(w);
}
else{
m=n/2 ;
result=a[m];
}
cout<<"Array is: \n";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<"\nMedian is "<<result<<endl;
return 0;
}
PRACTICAL-9
Q9. Write a program to draw right angle triangle pattern.
//9.Program to draw right angle triangle pattern.
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("* ");
}
printf("\n");
11 | Page
}
return 0;
}
PRACTICAL-10
Q10. Write a program to check the prime number.
//10.Program to check the prime number.
#include<stdio.h>
int main()
{
int n,i,count=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
for(i=2;i<n/2;i++){
if(n%i==0){
printf("The given number is not prime");
12 | Page
count=1;
break;
}
}
if(count==0){
printf("The given number is prime");
}
return 0;
}
PRACTICAL-11
Q11. Write a program to perform linear search in an array.
//11.Program to perform linear search in an array.
#include <iostream>
using namespace std;
int main()
{
int arr[5]={1,2,6,7,4};
int x;
cout<<"Enter element you want to search : ";
cin>>x;
13 | Page
int count=0;
for(int i=0;i<5;i++)
{
if(arr[i]==x)
{
cout<<"\nElement found ..."<<endl;
count=0;
break;
}
else
{
count++;
}
}
if(count>0)
{
cout<<"\nElement not found!!"<<endl;
}
return 0;
}
PRACTICAL-12
Q12. Write a program to perform Addition, Subtraction, Transpose and
Multiplication of two matrices.
//12.Program to perform Addition, Subtraction, Transpose and
Multiplication of two matrices.
#include<stdio.h>
int main()
{
int a[3][3],b[30][3],c[3][3],d[3][3],i,j,k;
printf("Enter the data in Matrix A:");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&a[i][j]);
14 | Page
}
}
printf("Enter the data in Matrix B:");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Addition of the two Matrix A and B is\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]=a[i][j]-b[i][j];
}
}
printf("Subtraction of the two Matrix A and B is\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("Transpose of Matrix C is\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
d[j][i]=c[i][j];
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d\t",d[i][j]);
}
printf("\n");
}
15 | Page
printf("Multiplication of Matrix A and B is\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]=0;
for(k=0;k<3;k++){
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
16 | Page
PRACTICAL-13
17 | Page
Q13. Write a program to perform stack operations.
//13. Program to perform stack operations.
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
18 | Page
} else {
printf("Could not insert data, Stack is full.\n");
}
}
/* Main function */
int main(){
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Element at top of the stack: %d\n" ,peek());
printf("Elements: \n");
PRACTICAL-14
Q14. Write a program to insert elements in a queue.
19 | Page
//14. Program to insert elements in queue.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isFull(){
return itemCount == MAX;
}
bool isEmpty(){
return itemCount == 0;
}
int removeData(){
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int main(){
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("\nQueue: ");
while(!isEmpty()) {
int n = removeData();
printf("%d ",n);
}
return 0;
20 | Page
}
PRACTICAL-15
Q15. Write a program to implement bubble sort in an array.
//15. Program to implement bubble sort in an array.
#include <stdio.h>
21 | Page
void bubble_sort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
PRACTICAL-16
Q16. Write a program to implement Linked List.
//16. Program to implement Linked List.
#include <stdio.h>
22 | Page
#include <stdlib.h>
// Creating a node
struct node {
int value;
struct node *next;
};
int main() {
// Initialize nodes
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
// Allocate memory
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;
// printing node-value
head = one;
printLinkedlist(head);
}
23 | Page
OUTPUT OF THE PROGRAM
24 | Page