0% found this document useful (0 votes)
23 views

Dsa File of Kushagra

Uploaded by

Kushagra Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Dsa File of Kushagra

Uploaded by

Kushagra Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Q1.WAP to find the factorial of a no. using recursion.

#include<stdio.h>
#include<conio.h>
int fact(int n);
void main(){
int num,r;
clrscr();
printf("\n enter any number : ");
scanf("%d",&num);
r=fact(num);
printf("\n factorial = %d",r);
getch();
}
int fact(int n)
{
if(n==1)
{
return 1;
}
else
{
return n*fact(n-1);
}
Q2.WAP to implement matrix multiplication.

#include <stdio.h>
#define MAX_SIZE 10

void multiplyMatrices(int firstMatrix[MAX_SIZE][MAX_SIZE], int


secondMatrix[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int
rowFirst, int colFirst, int rowSecond, int colSecond) {
int i, j, k;

// Initializing elements of result matrix to 0


for (i = 0; i<rowFirst; ++i) {
for (j = 0; j <colSecond; ++j) {
result[i][j] = 0;
}
}

// Multiplying firstMatrix and secondMatrix and storing in result


for (i = 0; i<rowFirst; ++i) {
for (j = 0; j <colSecond; ++j) {
for (k = 0; k <colFirst; ++k) {
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}

void displayMatrix(int matrix[MAX_SIZE][MAX_SIZE], int row, int col) {


int i, j;
for (i = 0; i< row; ++i) {
for (j = 0; j < col; ++j) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int firstMatrix[MAX_SIZE][MAX_SIZE], secondMatrix[MAX_SIZE][MAX_SIZE],
result[MAX_SIZE][MAX_SIZE];
int rowFirst, colFirst, rowSecond, colSecond;

printf("Enter rows and columns for first matrix: ");


scanf("%d %d", &rowFirst, &colFirst);

printf("Enter elements for matrix 1:\n");


for (int i = 0; i<rowFirst; ++i) {
for (int j = 0; j <colFirst; ++j) {
scanf("%d", &firstMatrix[i][j]);
}
}
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &rowSecond, &colSecond);

// Checking if matrices can be multiplied


if (colFirst != rowSecond) {
printf("Error! Matrices cannot be multiplied.\n");
return 1;
}

printf("Enter elements for matrix 2:\n");


for (int i = 0; i<rowSecond; ++i) {
for (int j = 0; j <colSecond; ++j) {
scanf("%d", &secondMatrix[i][j]);
}
}

multiplyMatrices(firstMatrix, secondMatrix, result, rowFirst, colFirst, rowSecond,


colSecond);

printf("Product of matrices:\n");
displayMatrix(result, rowFirst, colSecond);
return 0;
}
Q3.WAP to reverse the string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int i,j,len;
clrscr();
printf("\n enter the string");
gets(str1);
printf("\n original string = %s",str1);
len=strlen(str1);
for(i=0,j=len-1;j<len;i++,j--)
{
str2[i]=str1[j];
}
str2[i]='\0';
printf("\n reverse string = %s".str2);
getch();
}
Q4.WAP to find xy using recursion.

#include<stdio.h>
#include<conio.h>
int power(int n,int p);
void main(){
int n,p,r;
clrscr();
printf("\n enter the number: ");
scanf("%d",&n);
printf("enter power: ");
scanf("%d",&p);
r=power(n,p);
printf("\n result =%d",r);
getch();
}
int power(int n,int p)
{
if(p==1)
{
return n;
}
else{
return n*power(n,p-1);
}
Q5.WAP to insert and delete an element in an array at desired
position.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAXSIZE 100
int arr[MAXSIZE];
int n=0;
void insertion(int item,int pos);
void deletion(int pos);
void display();
void main()
{
int i,item,pos,choice;
printf("\n enter num of element : ");
scanf("%d",&n);
printf("\n enter elements in array : ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
do
{
printf("\n 1.insertion");
printf("\n 2.deletion");
printf("\n 3.display");
printf("\n 4.exit");
printf("\n enter your choice: ");
scanf("%d",&choice);
switch (choice)
{
case 1: printf("\n enter new element :");
scanf("%d",&item);
printf("\n enter position : ");
scanf("%d",&pos);
insertion(item,pos);
break;
case 2: printf("\n enter position : ");
scanf("%d",&pos);
deletion(pos);
break;
case 3: display();
break;
case 4: exit(0);
default:printf("\n you entered wrong choice");
break;
}
} while (1);
getch();
}
void insertion(int item,int pos)
{
int j;
if(n==MAXSIZE)
{
printf("\n array is full");
}
else
{
j=n;
while(j>=pos)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[pos]=item;
n=n+1;
}
}
void deletion(int pos)
{
int j,temp;
if(n==0)
{
printf("\n array is empty");
}
else
{
temp = arr[pos];
j=pos;
while(j<n)
{
arr[j]=arr[j+1];
j=j+1;
}
n=n-1;
}
}
void display()
{
int i;
printf("\n array elements are : ");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
Q6. write a program to push and pop operation on stack using ARRAY.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAXSIZE 100
int stk[MAXSIZE];
int top=0;
void push(int item);
void pop();
void display();
void main()
{
int item,choice,i;
//clrscr();
printf("\n enter the number of elements: ");
scanf("%d",&top);
printf("\n enter the elements in stack: ");
for(i=0;i<top;i++)
{
scanf("%d",&stk[i]);
}
do
{
printf("\n 1.insertion");
printf("\n 2.deletion");
printf("\n 3.display");
printf("\n 4.exit");
printf("\n enter your choice: ");
scanf("%d",&choice);
switch (choice)
{
case 1: printf("\n enter the item: ");
scanf("%d",&item);
push(item);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\n you entered wrong choice");
break;
}
} while (1);
getch();
}
void push(int item)
{
if(top==MAXSIZE)
{
printf("\n stack is full");
}
else
{
top=top+1;
stk[top-1]=item;
}
}
void pop()
{
int temp;
if(top==0)
{
printf("stack is empty");
}
else
{
temp=stk[top-1];
top=top-1;
}
}
void display()
{
int i;
printf("\n stack element are: ");
for(i=0;i<top;i++)
{
printf("%d\t",stk[i]);
}
}
Q7.write a program to push and pop operation on stack using
linkedlist.

#include <stdio.h>
#include <stdlib.h>
// Node structure for the linked list
struct Node {
int data;
struct Node* next;
};

// Function to create a new node with given data


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to push a new element onto the stack


void push(struct Node** top, int data) {
struct Node* newNode = createNode(data);
newNode->next = *top;
*top = newNode;
printf("%d pushed to the stack.\n", data);
}

// Function to pop an element from the stack


int pop(struct Node** top) {
if (*top == NULL) {
printf("Stack underflow. Cannot pop from an empty stack.\n");
exit(EXIT_FAILURE);
}
struct Node* temp = *top;
int poppedData = temp->data;
*top = temp->next;
free(temp);
return poppedData;
}

// Function to display the elements of the stack


void display(struct Node* top) {
printf("Stack: ");
while (top != NULL) {
printf("%d ", top->data);
top = top->next;
}
printf("\n");
}

int main() {
struct Node* top = NULL;

push(&top, 10);
push(&top, 20);
push(&top, 30);

display(top);

printf("Popped element: %d\n", pop(&top));

display(top);

return 0;
}
Q8. WAP to implement linear queue using array.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
int queue[MAXSIZE];
int front = 0;
int rear = 0;
void insertion(int item);
void deletion();
void display();
void main()
{
int item,choice,i;
clrscr();
printf("\n enter no of elements : ");
scanf("%d",&rear);
printf("\n enter elements in queue");
for(i=0;i<rear;i++)
{
scanf("%d",&queue[i]);
}
do
{
printf("\n 1.insertion");
printf("\n 2.deletion");
pritnf("\n 3.display");
printf("\n 4.exit");
printf("\n enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n enter the new item " );
scanf("%d",&item);
insertion(item);
break;
case 2: deletion();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\n you entered wrong choice");

}
} while(1);
getch();
}
void insertion(int item)
{
if(rear==MAXSIZE)
{
printf("queue is full");
}
else if(rear==0)
{
rear = 1;
}
else{
rear = rear +1;
}
queue[rear-1]=item;
}
void deletion()
{
int temp;
if(front==-1)
{
printf("queue is empty");
}
temp = queue[front-1];
if(front== rear)
{
front = rear = 0;
}
else{
front = front +1;
}
}
void display()
{
int i;
printf("\n queue elements are: ");
for(i=front;i<rear;i++)
{
printf("%d \t",queue[i]);
}
}
Q9. WAP to implement circular queue using array.

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
int cqueue[MAXSIZE];
int front=0;
int rear = 0;
void insertion(int item);
void deletion();
void display();
void main()
{
int item,choice,i;
clrscr();
printf("\n enter no of elements : ");
scanf("%d",&rear);
printf("\n enter elements in circular queue");
for(i=0;i<rear;i++)
{
scanf("%d",&cqueue[i]);
}
front=1;
do
{
printf("\n 1.insertion");
printf("\n 2.deletion");
pritnf("\n 3.display");
printf("\n 4.exit");
printf("\n enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n enter the new item " );
scanf("%d",&item);
insertion(item);
break;
case 2: deletion();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\n you entered wrong choice");

}
} while(1);
getch();
}
void insertion(int item)
{
if((front==1 && rear = MAXSIZE)|| (front==rear+1))
{
printf("\n circular queue is overflow ");
exit(0);
}
if(rear==0)
{
front=1;
rear=1;
}else if(rear==MAXSIZE)
{
rear = 1;
}
else{
rear = rear +1;
}
cqueue[rear-1]=item;
}
void deletion()
{
int temp;
if(front==0)
{
printf("\n circular queue is inderflow");
exit(0);
}
temp=cqueue[front-1];
if(front==rear)
{
front=0;
rear=0;
}
else if(front == MAXSIZE)
{
front =1;
}
else
{
front = front +1;
}
}
void display()
{
int i;
printf("\n circular queue elements are: ");
if(rear<front)
{
for(i=front-1;i<MAXSIZE;i++)
{
printf("%d\t",cqueue[i]);
}
for(i=0;i<rear;i++)
{
printf("%d\t",cqueue[i]);
}
}
else
{
for(i=front-1;i<rear;i++)
{
printf("%d\t",cqueue[i]);
}
}
}
Q10. WAP to insert a node at any location in singly linked list.
#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list * next;
};
struct list * start = NULL;
void insert_beg(int item);
void insert_pos(int item,int data);
void insert_end(int item);
void traverse();
void main()
{
int item,ch,data;
clrscr();
do
{
printf("\n 1.insertion at begenning");
printf("\n 2.insertion at any position");
printf("\n 3.insertion at end");
printf("\n 4.display");
printf("\n 5.exit");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1 :printf("\n enter new item");
scanf("%d",&item);
insert_beg(item);
break;
case 2 :printf("\n enter the new item");
scanf("%d",&item);
printf("\n enter the data");
scanf("%d",&data);
insert_pos(item,data);
break;
case 3 :printf("\n enter the new item");
scanf("%d",&item);
insert_end(item);
break;
case 4 : traverse();
break;
case 5 : exit(0);
default: printf("you entered wrong choice");
}
} while (1);
getch();
}
void insert_beg(int item){
struct list * p;
p=(struct list*)malloc(sizeof (struct list));
p->info = item;
p->next = start;
start = p;
}
void insert_pos(int item,int data)
{
struct list * p,* q,*r;
p=(struct list * )malloc(sizeof(struct list));
p->info=item;
while(q->info!=data)
{
q=q->next;
}
r=q->next;
q->next=p;
p->next =r;
}
void insert_end(int item)
{
struct list *p,*q;
p=(struct list*)malloc(sizeof(struct list));
p->info=item;
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next = p;
p->next = NULL;
}
void traverse()
{
struct list*q;
printf("\n list elements are: ");
q=start;
while(q->next!=NULL)
{
printf("%d\t",q->info);
q=q->next;
}
printf("%d\t",q->info);
}
Q11.WAP to insert a node at beginning in doubly linked list.
#include <stdio.h>
#include <stdlib.h>

// Node structure for doubly linked list


struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);

if (head == NULL) {
return newNode;
}

newNode->next = head;
head->prev = newNode;

return newNode;
}

// Function to display the doubly linked list


void displayList(struct Node* head) {
struct Node* current = head;

printf("Doubly Linked List: ");


while (current != NULL) {
printf("%d <-> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 30);
head = insertAtBeginning(head, 20);
head = insertAtBeginning(head, 10);
displayList(head);

return 0;
}
Q13. WAP to delete a node at beginning in singly linked list.
#include <stdio.h>
#include <stdlib.h>
// Node structure for singly linked list
struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to delete a node at the beginning of the singly linked list


struct Node* deleteAtBeginning(struct Node* head) {
if (head == NULL) {
printf("List is empty. Cannot delete from an empty list.\n");
return NULL;
}

struct Node* temp = head;


head = head->next;
free(temp);

return head;
}

// Function to display the singly linked list


void displayList(struct Node* head) {
struct Node* current = head;

printf("Singly Linked List: ");


while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;

// Insert nodes at the beginning


head = createNode(30);
head->next = createNode(20);
head->next->next = createNode(10);

// Display the original singly linked list


printf("Original ");
displayList(head);

// Delete node at the beginning


head = deleteAtBeginning(head);

// Display the modified singly linked list


printf("After deleting at the beginning ");
displayList(head);

return 0;
}
Q14.WAP to delete a node at any location in doubly linked list.
#include <stdio.h>
#include <stdlib.h>

// Node structure for doubly linked list


struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Function to delete a node at a given position in the doubly linked list
struct Node* deleteAtPosition(struct Node* head, int position) {
if (head == NULL) {
printf("List is empty. Cannot delete from an empty list.\n");
return NULL;
}

struct Node* current = head;


int count = 1;

// Traverse to the specified position


while (current != NULL && count < position) {
current = current->next;
count++;
}

if (current == NULL || position < 1) {


printf("Invalid position. Deletion failed.\n");
return head;
}

// Adjust pointers to skip the node at the specified position


if (current->prev != NULL) {
current->prev->next = current->next;
} else {
// If the node to be deleted is the head node
head = current->next;
}

if (current->next != NULL) {
current->next->prev = current->prev;
}

printf("Node at position %d deleted successfully.\n", position);


free(current);

return head;
}

// Function to display the doubly linked list


void displayList(struct Node* head) {
struct Node* current = head;
printf("Doubly Linked List: ");
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;

// Insert nodes in the doubly linked list


head = createNode(10);
head->next = createNode(20);
head->next->prev = head;
head->next->next = createNode(30);
head->next->next->prev = head->next;

// Display the original doubly linked list


printf("Original ");
displayList(head);

// Delete node at position 2


head = deleteAtPosition(head, 2);

// Display the modified doubly linked list


printf("After deleting at position 2 ");
displayList(head);
return 0;
}
15) WAP to implement linear search technique.
#include<stdio.h>
#include<conio.h>
void linearsearch(int arr[],int n,int item)
{
arr[n+1]=item;
int loc=1;
while(arr[loc]!=item)
{
loc=loc+1;
}
if(loc=n+1){
return loc;
}
}
void main()
{
int n;
printf("\n enter the no of elements: ");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int item;
printf("\n enter the item: ");
scanf("%d",&item);
linearsearch(arr,n,item);
getch();
}
16) WAP to implement binary search technique.
#include<stdio.h>
#include<conio.h>
void binaryserach(int arr[],int n,int item)
{
int beg = 0;
int end = n-1;
int mid=(beg+end)/2;
while(arr[mid]&item&beg<=end)
{
if(item<arr[mid])
{
end = mid-1;
}
else
{
beg = mid+1;
mid=(beg+end)/2;
}
if(item=arr[mid])
{
loc=mid;
}
else
{
loc = 0;
}
return loc;
}
}
void main()
{
int n;
printf("\n enter the no of elements: ");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int item;
printf("\n enter the item: ");
scanf("%d",&item);
binarysearch(arr,n,item);
getch();
}
17) WAP to implement bubble sort technique.
#include<stdio.h>
#include<conio.h>
int arr[MAXSIZE];
int n = 0;
void bubble_sort();
void display();
void main()
{
int i;
clrscr();
printf("\n enter no of elements : ");
scanf("%d",&n);
printf("\n enter the elements: ");
for(i=0;i<=n;i++)
{
scanf("%d",&arr[i]);
}
display();
bubble_sort();
display();
getch();
}
void bubble_sort()
{
int i,j,temp;
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void display()
{
int i;
printf("\n enter elements : ");
for(i=1;i<=n;i++)
{
printf("%d\t",arr[i]);
}
}
18) WAP to implement selection sort technique.
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
int arr[MAXSIZE];
int n=0;
void selection_sort();
void display();
void main()
{
int i;
clrscr();
printf("\n enter no of elements: ");
scanf("%d",&n);
printf("\n enter elements: ");
for(i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
display();
selection_sort();
display();
getch();
}
void selection_sort()
{
int i,j,temp,min;
for(i=1;i<n;i++)
{
min=i;
}
for(j=i+1;j<=n;j++)
{
if(arr[mid]>arr[j])
{
min=j;
}
}
if(i!=min)
{
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
void display()
{
int i;
printf("\n array elements are: ");
for(i=1;i<=n;i++)
{
printf("%d\t",arr[i]);
}
}
19) WAP to implement insertion sort technique.
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
int arr[MAXSIZE];
int n = 0;
void insert_sort();
void display();
void main()
{
int i;
clrscr();
printf("\n enter no of elements in array");
scanf("%d",&n);
printf("\n enter elements in array: ");
for(i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
display();
insert_sort();
display();
getch();

}
void insert_sort()
{
int i,j,temp;
for(i=1;i<=n;i++)
{
temp=arr[i];
j=i-1;
while(temp<arr[j]&&j>0)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
}
void display()
{
int i;
printf("\n array elements are: ");
for(i=1;i<=n;i++)
{
printf("%d\t",arr[i]);
}
}
20) WAP to implement quick sort technique.
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
int arr[MAXSIZE];
int n = 0;
void quick_sort(int beg,int end);
int partition(int beg,int end);
void display();
void main()
{
int i;
clrscr();
printf("\n enter no of elements in array: ");
scanf("%d",&n);
printf("\n enter elements in array: ");
for(i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
display();
quick_sort(l,n);
display();
getch();
}
void quick_sort(int beg,int end)
{
int temp,loc;
if(beg<end)
{
loc=partition(beg,end);
quick_sort(beg,loc-1);
quick_sort(loc+1,end);
}
}
int partition(int beg,int end)
{
int left = beg,right=end,loc=beg,temp;
while(beg<end)
{
while((arr[loc]<=arr[right])&&(loc!=right))
{
right=right-1;
}
if(loc==right)
{
return loc;
}
if(arr[loc]>arr[right])
{
temp=arr[loc];
arr[loc]=temp;
arr[right]=temp;
loc=right;
}
while((arr[loc]>arr[left])&&(loc!=left))
{
left = left +1;
}
if(loc==left)
{
return loc;
}
if(arr[loc]<arr[left])
{
temp=arr[loc];
arr[loc]=arr[left];
arr[left]=temp;
loc=left;
}
}
}
void display()
{
int i;
printf("\n array elements are: ");
for(i=1;i<n;i++)
{
printf("%d\t",arr[i]);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy