Data Structure and Algorithms
Data Structure and Algorithms
PRACTICAL FILE
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.
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
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.
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);
}
Maximum number:
A maximum is the point at which a function's value is the greatest.
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;
}
Smallest number:
A smallest number is the point at which a function's value is the smallest.
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;
scanf("%d", &n);
scanf("%d", &arr[i]);
min=i;
if(arr[min]>arr[j])
min=j;
if(min!=i)
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
for(i=0;i<n;i++)
printf("%d\n", arr[i]);
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;
scanf("%d",&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]);
int main() {
int arr[100],n,i,j,temp;
scanf("%d",&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]);
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");
}
}
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");
}
}
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;
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);
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>
if(*start==NULL){
*start=new_node;
}else{
node *temp=*start;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=new_node;
}
}
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);
}
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;
}
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
}
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 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");
}
}
}
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;
}
}
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;
}
struct node {
int info;
struct node *link;
};
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;
}
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;
}
return;
}