Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Data Structure and Algorithms

Uploaded by

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

Data Structure and Algorithms

Uploaded by

Shreya Katoch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 60

HIMACHAL PRADESH UNIVERSITY, SHIMLA

DEPARTMENT: Data Science and Artificial Intelligence


Academic Year 2024-2026

PRACTICAL FILE

Course : Data Structure and Algorithms – Lab


Name : Tanjan Koundal
Class : M.Sc. DS&AI
Roll No. : 24022
Submitted To : Mr. Pawan Kumar
INDEX

Seria NAME OF THE EXPERIMENT PAGE SIGNATURE


l No. NO.
1 WAP to find whether the entered number is 3-4
Armstrong or not.
2 WAP to find whether the entered number is 5-6
prime or not.
3 WAP to find the factorial value of any number. 7
4 Swap the value of two variables using call by 8-10
value& by reference.
5 WAP to find the maximum number in an array. 11-12
6 WAP to find out in a [25] how many are 13-14
positive, how many are negative, how many
are even and how many are odd.
7 WAP using pointers to find the smallest 15-16
number in an array of 25 integers.
8 Implement the Selection Sort, Bubble Sort and 17-22
Insertion Sort.
9 Implement the Linear Search and Binary 23-26
Search.
10 WAP to reverse a string. 27
11 Implement the following: 28-35
a) Inserting a node into the Linked List
(First Node, Last Node, and nth Node)
b) Deleting a node from the Linked List
(First Node, Last Node, and nth Node)
12 Implement the following using arrays and 36-54
Linked List:
a) Stack and its operations
b) Queue and its operations
13 Implement the following: 55-60
a) Creating a binary search tree and
traversing it using in order, preorder and
post order.

Experiment 1
WAP to find whether the entered number is Armstrong or not.

Armstrong Number:
An Armstrong number is a special kind of number in math. It's a number that equals the sum
of its digits, each raised to a power.
For example, if you have a number like 153, it's an Armstrong number because 1^3 + 5^3 +
3^3 equals 153.

Program to check whether the entered number is Armstrong or not is as following:


#include <math.h>
#include <stdio.h>
int main(){
int num, n=0, temp, rem, result=0;
printf("enter the value of number:\n");
scanf("%d", &num);
temp=num;
while(num>0)
{
n++;
num=num/10;
}
for(num=temp; num!=0; num=num/10)
{
rem=num%10;
result=result+pow(rem,n);
}
if(temp==result)
{
printf("number is armstrong number");
}
else
{
printf("number is not armstrong number");
}
}

After the execution of above code then if the number is Armstrong then:

After the execution of above code then if the number is not Armstrong then:

Experiment 2
WAP to find whether the entered number is prime or not.

Prime Number:
A whole number greater than 1 that cannot be exactly divided by any whole number other
than itself and 1.
For example: 2,3,5,7,11

Program to check whether the entered number is Prime or not is as following:


#include <stdio.h>
void main(){
int number, i, count=0;
printf("enter the value of number:\n");
scanf("%d", &number);
for(i=2; i<number; i++)
{
if(number%i==0)
{
count++;
break;
}
}
if(count>0)
{
printf("the number is not a prime number");
}
else
{
printf("the number is a prime number");
}
}

After the execution of above code then if the number is Prime then:

After the execution of above code then if the number is not Prime then:

Experiment 3
WAP to find the factorial value of any number.

Factorial value of any number:


The factorial of a whole number ‘n’ is defined as the product of that number with every whole
number less than or equal to’n’ till 1.
For example, the factorial of 4 is 4 × 3 × 2 × 1, which is equal to 24. It is represented using
the symbol '!'

Program to find the factorial is as follows:


#include <stdio.h>
int main() {
int num, count, fact=1;
printf("enter a number to find its factorial\n");
scanf("%d", &num);
for(count=1; count<=num; count++)
{
fact=fact*count;
}
printf("factorial of %d is %d\n", num,fact);
}

After the execution of the above code output is as follows:

Experiment 4
Swap the value of two variables using call by value & by reference.
Program to swap a value using call by value is as follows:
#include <stdio.h>
void swap(int, int);
int main(){
int a, b;
printf("enter the values of a and b:\n");
scanf("%d%d", &a,&b);
printf("values of a and b before swap: a=%d, b=%d\n", a,b);
swap(a,b);
printf("values of a and b after swap: a=%d, b=%d\n", a,b);
}
void swap(int x, int y)
{
int temp;
printf("values inside swap function before swap: x=%d, y=%d\n", x,y);
temp=x;
x=y;
y=temp;
printf("values inside swap function after swap: x=%d, y=%d\n", x,y);
}

After the execution of above code output is as follows:


Program to swap a value using call by refrence is as follows:
#include <stdio.h>
void swap(int*x,int*y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
int main()
{
int a,b;
printf("enter the value of a & b:\n");
scanf("%d%d",&a,&b);
printf("values before swap: a=%d, b=%d\n", a,b);
swap(&a,&b);
printf("values after swap: a=%d, b=%d\n", a,b);
}

After the execution of above code output is as follows:


Experiment 5
WAP to find the maximum number in an array.

Maximum number:
A maximum is the point at which a function's value is the greatest.

Program to find the maximum number in an array is as follows:


#include <stdio.h>
int main(){
int arr[100], n, i;
printf("enter the value of n:\n");
scanf("%d", &n);
printf("enter the value into array: \n");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
for(i=1; i<n; i++)
{
if(arr[0]<arr[i])
{
arr[0]=arr[i];
}
}
printf("the largest element=%d", arr[0]);
}
After the execution of the above code the final output will look like as follows:
Experiment 6
WAP to find out in a[25] how many are positive, how many are negative, how
many are even and how many are odd.

Positive Number:
A number which is greater than zero is known as positive number.
Negative Number:
A number which is less than zero is known as negative number.
Even Number:
Any number that can be exactly divided by 2 is known as even number.
Odd Number:
Any number that cannot be divided by 2 is known as odd number.

Program to find that how many are positive, negative, even and odd in an array is as follows:
#include <stdio.h>
int main() {
int arr[25];
int positiveCount=0, negativeCount=0, evenCount=0, oddCount=0;
printf("enter 25 integers:\n");
for(int i=0; i<25; i++)
{
scanf("%d",&arr[i]);
}
for(int i=0; i<25; i++)
{
if (arr[i]>0)
{
positiveCount++;
}
else if(arr[i]<0)
{
negativeCount++;
}
if(arr[i]%2==0)
{
evenCount++;
}
else
{
oddCount++;
}
}
printf("positive numbers:%d\n", positiveCount);
printf("negative numbers:%d\n", negativeCount);
printf("even numbers:%d\n", evenCount);
printf("odd numbers:%d\n", oddCount);
return 0;
}

After the execution of the above code output is as follows:


Experiment 7
WAP using pointers to find the smallest number in an array of 25 integers.

Smallest number:
A smallest number is the point at which a function's value is the smallest.

Program to find the smallest number in an array is as follows:


#include <stdio.h>

int main() {
int arr[25], i, *ptr;
printf("enter the values into array:\n");
for(i=0;i<25;i++)
scanf("%d", &arr[i]);
ptr=arr;
for(i=1;i<25;i++)
if(*ptr>*(ptr+i))
{
*ptr=*(ptr+i);
}
printf("the smallest value is %d", arr[0]);
}
After the execution of the above code output is as follows:

Experiment 8
Implement the Selection Sort, Bubble Sort and Insertion Sort.

Selection Sort
Program to sort an array using selection sort is as follows:
#include <stdio.h>

int main() {

int arr[100],n,i,j,min,temp;

printf("enter the value of n:\n");

scanf("%d", &n);

printf("enter the values into array:\n");

for(i=0; i<n; i++)

scanf("%d", &arr[i]);

for(i=0; i<n-1; i++)

min=i;

for(j=i+1; j<n; j++)

if(arr[min]>arr[j])

min=j;

if(min!=i)

temp=arr[i];

arr[i]=arr[min];

arr[min]=temp;
}

printf("sorted array is:\n");

for(i=0;i<n;i++)

printf("%d\n", arr[i]);

After the execution of the above code output is as follows:

Bubble Sort
Program to sort an array using bubble sort is as follows:
#include <stdio.h>
int main() {

int arr[100],n,i,j,temp;

printf("enter the value of n:\n");

scanf("%d",&n);

printf("enter the values into array:\n",n);

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)

if(arr[i]>arr[j])

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

printf("sorted array:\n");

for(i=0;i<n;i++)

printf("%d\n",arr[i]);

After the execution of the above code output is as follows:


Insertion Sort
Program to sort an array using insertion sort is as follows:
#include <stdio.h>

int main() {
int arr[100],n,i,j,temp;

printf("enter the value of n:\n");

scanf("%d",&n);

printf("enter the values into array:\n");

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

for(i=1;i<n;i++)

j=i;

while(j>0&&arr[j-1]>arr[j])

temp=arr[j];

arr[j]=arr[j-1];

arr[j-1]=temp;

j--;

printf("sorted array:\n");

for(i=0;i<n;i++)

printf("%d\n",arr[i]);

After the execution of the above code output is as follows:


Experiment 9
Implement the Linear Search and Binary Search.

Linear Search
Program to search a value using linear search is as follows:
#include <stdio.h>
int main() {
int arr[100],n,i,value, count=0;
printf("enter the value of n:\n");
scanf("%d",&n);
printf("enter the values into array:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("enter the searching value:\n");
scanf("%d",&value);
for(i=0;i<n;i++)
{
if(arr[i]==value)
{
count++;
break;
}
}
if(count>0)
{
printf("element found");
}
else
{
printf("element not found");
}
}

After the execution of the above code output is as follows:


If the element is found

If the element is not found

Binary Search
Program to search a value using binary search is as follows:
#include <stdio.h>
int main() {
int arr[100],n,i,first,last,mid,value;
printf("enter the value of n:\n");
scanf("%d",&n);
printf("enter the values into array:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
first=0;
last=n-1;
mid=(first+last)/2;
printf("enter the value to be search:\n");
scanf("%d",&value);
while(first<=last)
{
if(arr[mid]==value)
{
printf("value found");
break;
}
else if(arr[mid]<value)
{
first=mid+1;
}
else
{
last=mid-1;
}
mid=(first+last)/2;
}
if(first>last)
{
printf("element not found");
}
}

After the execution of the above code output is as follows:


If the element is found

If the element is not found

Experiment 10
WAP to reverse a string.
Program to reverse a string is as follows:
#include <stdio.h>

#include <string.h>

void main()

int l, i;

char s1[30], c;

printf("enter string: ");

scanf("%s", &s1);

l = strlen(s1);

for(i=0;i<=l/2;i++)

c = s1[i];

s1[i] = s1[l-1-i];

s1[l-1-i] = c;

printf("%s",s1);

After the execution of the above code output is as follows:

Experiment 11
Implement the following:
a) Inserting a node into the Linked List (First Node, Last Node, and nth
Node)
b) Deleting a node from the Linked List (First Node,Last Node, and nth
Node)

Program to insert and delete node into the Linked List (First Node, Last Node, and nth Node)
is as follows:
#include <stdio.h>
#include <stdlib.h>

typedef struct node {


int info;
struct node *next;
} node;

void add_beg(node**start, int value){


node *new_node=(node*)malloc(sizeof(node));
new_node->info=value;
new_node->next=*start;
*start=new_node;
}

void add_mid(node**start, int value){


node *new_node, *temp;
int addafter;
printf("enter the value after which to add: ");
scanf("%d", &addafter);
new_node=(node*)malloc(sizeof(node));
new_node->info=value;
if(*start==NULL){
new_node->next=NULL;
*start=new_node;
}
else{
temp=*start;
while(temp!=NULL &&temp->info!=addafter){
temp=temp->next;
}
if(temp==NULL){
printf("element not found in the list. Cannot add in the middle.\n");
free(new_node);
}else{
new_node->next=temp->next;
temp->next=new_node;
}
}
}

void add_end(node **start, int value){


node *new_node=(node*)malloc(sizeof(node));
new_node->info=value;
new_node->next=NULL;

if(*start==NULL){
*start=new_node;
}else{
node *temp=*start;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=new_node;
}
}

void delete_beg(node **startref){


if(*startref==NULL){
printf("List is empty. Cannot delete from the beginning.\n");
}else{
node *temp=*startref;
*startref=(*startref)->next;
free(temp);
}
}

void delete_mid(node**startref, int value){


if(*startref==NULL){
printf("The list is empty. Cannot delete from the middle.\n");
return;
}
node *p1=*startref;
node *p2=NULL;

while(p1 !=NULL &&p1->info!=value){


p2=p1;
p1=p1->next;
}
if(p2==NULL){
printf("Element not found in the list. Cannot delete from the middle.\n");
return;
}
if(p2==NULL){
*startref=p1->next;
}else{
p2->next=p1->next;
}
free(p1);
}

void delete_end(node **startref){


if(*startref==NULL){
printf("List is empty. Cannot delete from the end.\n");
return;
}

node *p1=*startref;
node *p2=NULL;

while(p1->next!=NULL){
p2=p1;
p1=p1->next;
}
if(p2==NULL){
*startref=NULL;
}else{
p2->next=NULL;
}
free(p1);
}

void display_list(node *start){


if(start==NULL){
printf("The list is empty.\n");
return;
}

node *p1=start;
printf("Linked List:");
while(p1!=NULL){
printf("%d->", p1->info);
p1=p1->next;
}
printf("NULL\n");
}

int main(){
int ch, value;
node *start=NULL;

do{
printf("Enter 1 for add, 2 for delete, 0 to exit:");
scanf("%d", &ch);

switch(ch){
case 0:
//Free memory and exit
break;
case 1:
printf("Enter a value for add:");
scanf("%d", &value);
printf("1 for adding at the beginning, 2 for adding in the middle, 3 for adding at the
end:");
scanf("%d", &ch);
switch(ch){
case 1:
add_beg(&start, value);
break;
case 2:
add_mid(&start, value);
break;
case 3:
add_end(&start, value);
break;
default:
printf("Invalid choice for adding\n");
}
break;
case 2:
printf("1 for delete at the beginning, 2 for delete in the middle, 3 for delete at the
end:");
scanf("%d", &ch);
switch(ch){
case 1:
delete_beg(&start);
break;
case 2:
printf("Enter the value to be deleted: ");
scanf("%d", &value);
delete_mid(&start, value);
break;
case 3:
delete_end(&start);
break;
default:
printf("Invalid choice for deleting\n");
}
break;
}
display_list(start);
}while(ch!=0);
//Free any remaining memory
while(start!=NULL){
delete_beg(&start);
}
return 0;
}

After the execution of the above code output is as follows:


If the linked list is empty
If the linked list is not empty

Experiment 12
Implement the following using arrays and Linked List:
a) Stack and its operations
b) Queue and its operations.
a) Stack and its operations

i) Implementation of Stack and its operations using arrays:


Program to implement stack using arrays is as follows:
#include <stdio.h>// stack using array
#define N 5
int stack[N];
int top=-1;
void push()
{
int x;
printf(" enter the data ");
scanf(" %d",&x);
if (top==N-1)
{
printf(" overflow");
}
else{
top++;
stack[top]=x;
}

}
void pop()
{
int item;
if (top==-1)
{
printf(" underflow");
}
else
{
item=stack[top];
top--;
printf(" %d",item);
}
}
void peek()
{
if(top==-1)
{
printf("stack is empty");
}
else
{
printf(" stack[top]");
}
}
void display()
{
int i;
for (i=top;i>=0;i--)
{
printf(" %d",stack[i]);
}
}
void main()
{
int ch;
do
{
printf(" enter choice : 1: push, 2:pop,3:peek 4:display");
scanf(" \n%d",&ch);
switch (ch)
{
case 1:{
push();
break;}
case 2:{
pop();
break;}
case 3:{
peek();
break;}
case 4:{
display();
break;}
default:
printf(" invalid choice");
}
}
while (ch!=0);
//getch();
}
After the execution of above code output is as follows:
ii) Implementation of Stack and its operations using linked list:
Program to implement stack using linked list is as follows:
#include <stdio.h>
#include <stdlib.h>

struct node {
int info;
struct node* link;
} *top = NULL;

void push(int item);


int pop();
int peek();
int isEmpty();
void display();

void main() {
int choice, item;
while(1) {
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display item at the top\n");
printf("4. Display all items of the stack\n");
printf("5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the item to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2:
item = pop();
if (item != -1) {
printf("Popped item is: %d\n", item);
}
break;
case 3:
item = peek();
if (item != -1) {
printf("Item at the top is: %d\n", item);
}
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Wrong choice. Please try again.\n");
}
}
}

void push(int item) {


struct node* temp = (struct node*)malloc(sizeof(struct node));
if (temp == NULL) {
printf("Stack overflow\n");
return;
}
temp->info = item;
temp->link = top;
top = temp;
}

int pop() {
if (isEmpty()) {
printf("Stack underflow\n");
return -1;
}
struct node* temp = top;
int item = temp->info;
top = top->link;
free(temp);
return item;
}

int peek() {
if (isEmpty()) {
printf("Stack underflow\n");
return -1;
}
return top->info;
}

int isEmpty() {
return (top == NULL);
}

void display() {
struct node* ptr = top;
if (isEmpty()) {
printf("Stack is empty\n");
return;
}
printf("Stack elements:\n");
while (ptr != NULL) {
printf("%d\n", ptr->info);
ptr = ptr->link;
}
}

After the execution of above code output is as follows:


b) Queue and its operations
i) Implementation of Queue and its operations using arrays:
Program to implement queue using arrays is as follows:
#include <stdio.h>
#define N 7

int queue[N];
int front=-1;
int rear=-1;

void enqueue(int x) {
if(rear==N-1) {
printf("\nOverflow!\n");
}
else if(front==-1 && rear==-1) {
front=rear=0;
queue[rear]=x;
}
else {
rear++;
queue[rear]=x;
}
}

void dequeue() {
if(front==-1 && rear==-1) {
printf("\nUnderflow!\n");
}
else if(front==rear) {
front=rear=-1;
}
else {
printf("\nElement dequeued: %d",queue[front]);
front++;
}
}

void display() {
int x;
if(front==-1 && rear==-1) {
printf("\nQueue is empty!\n");
}
else {
printf("\nElements in queue are(front to rear): ");
for(x=front; x<rear+1; x++) {
printf("%d ",queue[x]);
}
printf("\n");
}
}

void peek() {
if(front==-1 && rear==-1) {
printf("\nQueue is empty!\n");
}
else {
printf("\nElement at the front of the queue is: %d", queue[front]);
}
}
void main() {
int choice, item;

while(1) {
printf("Select operation to perform:\n1.Enqueue\n2.Dequeue\n3.Display front element\
n");
printf("4.Display all items of the queue\n5.Quit\n>Enter your choice: ");
scanf("%d",&choice);

switch (choice)
{
case 1:
printf("\n>Enter the item to be pushed: ");
scanf("%d",&item);
enqueue(item);
break;

case 2:
dequeue();
break;

case 3:
peek();
break;

case 4:
display();
break;
case 5:
return;

default:
break;
}
}

return;
}

After the execution of above code output is as follows:


ii) Implementation of Queue and its operations using linked list:
#include <stdio.h>
#include <stdlib.h>

struct node {
int info;
struct node *link;
};

typedef struct node node;

node *front=NULL, *rear=NULL;

void enqueue(int item);


int dequeue();
int peek();
int isEmpty();
void display();

void main() {
int choice, item;

while(1) {
printf("Select operation to perform:\n1.Enqueue\n2.Dequeue\n3.Display front element\
n");
printf("4.Display all items of the queue\n5.Quit\n>Enter your choice: ");
scanf("%d",&choice);

switch (choice)
{
case 1:
printf("\n>Enter the item to be pushed: ");
scanf("%d",&item);
enqueue(item);
break;

case 2:
printf("\nDeleted element is %d\n",dequeue());
break;

case 3:
printf("Element at the front of the queue is: %d\n",peek());
break;

case 4:
display();
break;

case 5:
return;

default:
break;
}
}

return;
}

void enqueue(int item) {


struct node *temp;
temp=(node*)malloc(sizeof(node*));
if(temp==NULL) {
printf("Memory not available!\n");
return;
}

temp->info=item;
temp->link=NULL;

if(front==NULL) {
front=temp;
rear=temp;
}
else {
rear->link=temp;
rear=temp;
}
}

int dequeue() {
node *temp;
int item;
if(isEmpty()) {
printf("\nQueue underflow!\n");
exit(1);
}

temp=front;
item=temp->info;
front=front->link;

return item;
}

int isEmpty() {
if(front==NULL) {
return 1;
}
else {
return 0;
}
}

int peek() {
if(isEmpty()) {
printf("\nQueue is empty!\n");
exit(1);
}

return front->info;
}

void display() {
node *ptr;
ptr=front;

if(isEmpty()) {
printf("\nQueue is empty!\n");
return;
}

printf("\nElements in queue are: ");


while(ptr!=NULL) {
printf("%d ",ptr->info);
ptr=ptr->link;
}
printf("\n");

return;
}

After the execution of above code output is as follows:


Experiment 13
Implement the following:
a) Creating a binary search tree and traversing it using in order, preorder
and post order.

Program to create a binary search tree is as follows:


#include <stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *right;
struct node*left;
};
struct node*createBST (struct node*,int);
void inorder(struct node*);
void preorder(struct node*);
void postorder(struct node*);
int main()
{
struct node*root=NULL;
int choice,item,n,i;
do
{
printf("binary search tree operations\n");
printf("1.creation of BST:\n");
printf("2.traverse of inorder:\n");
printf("3.traverse in preorder:\n");
printf("4.traverse in postorder:\n");
printf("5.exit\n");
printf("enter choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
root=NULL;
printf("BST for how many node?");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter data for node: ",i);
scanf("%d",&item);
root=createBST(root,item);
}
printf("\nBST with %d nodes is ready to use\n",n);
break;
case 2:
printf("BST traversal in inorder\n");
inorder(root);
break;
case 3:
printf("BST traversal in preorder\n");
preorder(root);
break;
case 4:
printf("BST traversal in postorder\n");
postorder(root);
break;
case 5:
printf("terminating \n");
break;
default:
printf("invalid option\n");
break;
}
}while(choice!=5);
return 0;
}
struct node*createBST(struct node* root,int item )
{
if(root==NULL)
{
root=(struct node*)malloc(sizeof (struct node));
root->left=root->right=NULL;
root->data=item;
return root;
}
else
{
if(item<root->data)
{
root->left=createBST(root->left,item);
}
else if(item>root->data)
{
root->right=createBST(root->right,item);
}
else
{
printf("duplicate element !! not allowed!!");
}
return (root);
}
}
void inorder (struct node*root)
{
if(root!=NULL)
{
inorder (root->left);
printf("%d",root->data);
inorder(root->right);
}
}
void preorder(struct node*root)
{
if(root!=NULL)
{
printf("%d",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node*root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d",root->data);
}
}

After the execution of the above code output is as follows:

You might also like