Dsa File of Kushagra
Dsa File of Kushagra
#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
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;
};
int main() {
struct Node* top = NULL;
push(&top, 10);
push(&top, 20);
push(&top, 30);
display(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>
if (head == NULL) {
return newNode;
}
newNode->next = head;
head->prev = newNode;
return newNode;
}
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;
};
return head;
}
int main() {
struct Node* head = NULL;
return 0;
}
Q14.WAP to delete a node at any location in doubly linked list.
#include <stdio.h>
#include <stdlib.h>
if (current->next != NULL) {
current->next->prev = current->prev;
}
return head;
}
}
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]);
}
}