DS Lab file
DS Lab file
To transpose a 2D array.
2. 4
1
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++I)
{
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);
if (j == c - 1)
{
printf("\n\n")
}
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(int k=0;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
2
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
3
LAB -2
2.Write a program to implement transpose of a 2d array.
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c;s
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n"); }
}
for (int i = 0; i < r; ++i)
{
4
for (int j = 0; j < c; ++j)
{
transpose[j][i] = a[i][j];
}
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
{
for (int j = 0; j < r; ++j)
{
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
}
return 0;
}
OUTPUT
5
LAB - 3
3.Write a program to implement stack using array.
#include<stdio.h>
#include<stdio.h>
#define size 4
int top=-1,stack[size];
void push()
{
int x;
if(top==size-1)
{
printf("\noverflow");
}
else
{
printf("enter the elements to be inserted\n");
scanf("%d",&x);
top=top+1; stack[top]=x;
}
}
void pop()
{
if(top==-1)
{
printf("\nunderflow");
}
else
{
6
printf("\n popped element:%d",stack[top]);
top=top-1;
}
}
void display()
{
if(top==-1)
{
printf("\n underflow");
}
else
{
printf("\nelements present in the stack\n");
for(int i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
int main()
{
int choice=1,x;
while(choice<4||choice!=0)
{
printf("\n 1:insert an element");
printf("\n 2:delete an element");
printf("\n 3:display all the elements");
printf("\n 4:enter your choice");
scanf("%d",&choice);
switch(choice)
7
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default:printf("invalid choice");
}
}
}
OUTPUT
8
LAB -4
4.Write a program to implement stack using linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node *next;
};
struct node *head;
void push () {
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL) {
printf("not able to push the element");
}
else {
printf("Enter the value");
scanf("%d",&val);
if(head==NULL) {
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else {
ptr->val = val;
ptr->next = head;
head=ptr;
}
9
printf("Item pushed");
}}
void pop() {
int item;
struct node *ptr;
if (head == NULL) {
printf("Underflow");
}
else {
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display() {
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL) {
printf("Stack is empty\n");
}
else {
printf("Printing Stack elements \n");
while(ptr!=NULL) {
printf("%d\n",ptr->val);
ptr = ptr->next;
10
}
}
}
int main () {
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4) {
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice) {
case 1: {
push();
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
printf("Exiting....");
break;
11
}
default:
{
printf("Please Enter valid choice ");
}
}
}
}
OUTPUT
12
LAB -5
5.Write a program to implement queue using array.
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main ()
{
int choice;
printf("\n1.Insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
while(choice != 4)
{
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
13
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
14
queue[rear] = item;
printf("\nValue inserted ");
}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
}
void display()
{
15
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}
OUTPUT
16
LAB -6
6. Write a program to implement queue using linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
while(choice != 4)
{
printf("\nEnter your choice\n");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
17
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice\n");
} }
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
18
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}}
void display()
19
{ struct node *ptr;
ptr = front;
if(front == NULL)
{ printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}}}
OUTPUT
20
LAB – 7
7.Write a program to implement circular queue using array.
#include <stdio.h>
# define max 6
int queue[max];
int front=-1;
int rear=-1;
void enqueue(int element)
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
int dequeue()
{
21
if((front==-1) && (rear==-1))
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
22
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x;
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
while(choice<4 && choice!=0)
{
printf("\nEnter your choice");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}
23
}
return 0;
}
OUTPUT
24
LAB – 8
8. Write a program to implement circular queue using linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *f = NULL;
struct node *r = NULL;
void enqueue(int d)
{
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((r==NULL)&&(f==NULL))
{
f = r = n;
r->next = f;
}
else
{
r->next = n;
r = n;
n->next = f;
}
25
}
void dequeue()
{
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else if(f == r){
f = r = NULL;
free(t);
}
else{
f = f->next;
r->next = f;
free(t);
}
}
void print(){
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else{
do{
printf("\n%d",t->data);
t = t->next;
}while(t != f);
}
26
}
int main()
{
int opt,n,i,data;
printf("Enter Your Choice:-");
do{
printf("\n\n1 for Insert the Data in Queue\n2 for show the Data in Queue \n3 for
Delete the data from the Queue\n0 for Exit");
scanf("%d",&opt);
switch(opt){
case 1:
printf("\nEnter the number of data");
scanf("%d",&n);
printf("\nEnter your data");
i=0;
while(i<n){
scanf("%d",&data);
enqueue(data);
i++;
}
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 0:
27
break;
default:
printf("\nIncorrect Choice");
}
}while(opt!=0);
return 0;
}
OUTPUT
28
LAB-9
9. To implement Binary Search
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
29
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
OUTPUT
30
LAB - 10
10. To implement Linear Search
#include <stdio.h>
int linearSearch(int a[], int n, int val) {
// Going through array sequencially
for (int i = 0; i < n; i++) {
if (a[i] == val)
return i+1; }
return -1; }
int main() {
int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; // given array
int val = 41; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = linearSearch(a, n, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", res);
return 0; }
OUTPUT
31