-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
369 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,60 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#define max 5 | ||
int arr[max],front=-1,rear=-1; | ||
void enq(int n); | ||
void dq(); | ||
void display(); | ||
int main(void){ | ||
int ty; | ||
while(1){ | ||
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n"); | ||
scanf("%d",&ty); | ||
if(ty==1){ | ||
int num; | ||
printf("Enter the element: "); | ||
scanf("%d",&num); | ||
enq(num); | ||
} | ||
else if(ty==2) dq(); | ||
|
||
else if(ty==3) display(); | ||
else if(ty==4) break; | ||
} | ||
} | ||
void enq(int n){ | ||
if(front==-1 || rear==-1 ){ | ||
front=0;rear=0; | ||
arr[rear]=n; | ||
} | ||
else{ | ||
int srear=(rear 1)%max; | ||
if(srear==front) printf("Queue Full\n"); | ||
else { | ||
rear=srear; | ||
arr[rear]=n; | ||
} | ||
} | ||
} | ||
|
||
void dq(){ | ||
if(front==-1 ||rear==-1) printf("Queue Empty"); | ||
else { | ||
printf("%d\n",arr[front]); | ||
if(front==rear) front=-1; | ||
else front=(front 1)%max; | ||
} | ||
} | ||
|
||
void display(){ | ||
int i; | ||
if(front==-1 ||rear==-1) printf("Queue Empty"); | ||
else{ | ||
if(front<=rear) for(i=front;i<=rear;i ) printf("%d ",arr[i]); | ||
else { | ||
for(i=front;i!=rear;i=(i 1)%max) printf("%d ",arr[i]); | ||
printf("%d ",arr[i]); | ||
} | ||
printf("\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,79 @@ | ||
#include <stdio.h> | ||
#define size 4 | ||
#define max 10 | ||
int visited[size]; | ||
int stack[max]; | ||
int queue[max]; | ||
int top=-1,front=-1,rear=-1; | ||
int ar[size][size]; | ||
void dfs(int i); | ||
void bfs(int i); | ||
void main() | ||
{ | ||
printf("ENTER THE MATRIX"); | ||
printf("\n"); | ||
for(int i=0;i<size;i ) | ||
{ | ||
for(int j=0;j<size;j ) | ||
{ | ||
scanf("%d",&ar[i][j]); | ||
} | ||
} | ||
for(int i=0;i<size;i ) | ||
visited[i]=0; | ||
dfs(0); | ||
for(int i=0;i<size;i ) | ||
visited[i]=0; | ||
bfs(0); | ||
|
||
} | ||
void dfs(int i) | ||
{ | ||
|
||
stack[ top]=i; | ||
int x; | ||
printf("The DFS RESULT IS: "); | ||
while(top>=0) | ||
{ | ||
x=stack[top--]; | ||
|
||
if(visited[x]==0) | ||
{ | ||
printf("%d ",x); | ||
visited[x]=1; | ||
|
||
for(int j=0;j<size;j ) | ||
{ | ||
if(ar[x][j]==1) | ||
stack[ top]=j; | ||
} | ||
} | ||
} | ||
} | ||
void bfs(int i) | ||
{ | ||
printf("\n\n"); | ||
printf("THE BFS RESULT IS: %d ",i); | ||
visited[i]=1; | ||
queue[ rear]=i; | ||
front ; | ||
int x; | ||
while(front<size) | ||
{ | ||
|
||
x=queue[front]; | ||
front ; | ||
for(int j=0;j<size;j ) | ||
{ | ||
if(ar[x][j]==1) | ||
{ | ||
if (visited[j]==0) | ||
{ | ||
printf("%d ",j); | ||
queue[ rear]=j; | ||
visited[j]=1; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,54 @@ | ||
#include <stdio.h> | ||
#define max 5 | ||
int a[max]; | ||
void heapsort( int n); | ||
void heapify( int i,int n); | ||
void main() | ||
{ | ||
printf("Enter the elements of the array : "); | ||
for(int i=0;i<max;i ) | ||
scanf("%d",&a[i]); | ||
|
||
heapsort(max); | ||
printf("The sorted array: "); | ||
for(int i=0;i<max;i ) | ||
printf("%d ",a[i]); | ||
} | ||
|
||
void heapsort(int n) | ||
{ | ||
for(int i= n/2-1 ;i>=0;i--) | ||
{ | ||
heapify(i,n); | ||
} | ||
for(int i=n-1 ;i>=0;i--) | ||
{ | ||
int temp=a[0]; | ||
a[0]=a[i]; | ||
a[i]=temp; | ||
heapify(0,i); | ||
} | ||
} | ||
void heapify( int i,int n) | ||
{ int largest,lc,rc,temp; | ||
largest=i; | ||
lc=2*i 1 ; | ||
rc=2*i 2; | ||
if((a[lc]>a[largest])&&(lc<n)) | ||
{ | ||
largest=lc; | ||
} | ||
if((a[rc]>a[largest])&&(rc<n)) | ||
{ | ||
largest=rc; | ||
} | ||
if(i!=largest) | ||
{ | ||
temp=a[i]; | ||
a[i]=a[largest]; | ||
a[largest]=temp; | ||
heapify(largest,n); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,54 @@ | ||
#include<stdio.h> | ||
void mergesort(int arr[],int l,int r); | ||
void merge(int arr[],int l,int mid,int r); | ||
int main(void){ | ||
int arr[10],n; | ||
printf("Enter the number of elements to be Inserted\n"); | ||
scanf("%d",&n); | ||
printf("Enter the elements of array\n"); | ||
for(int i=0;i<n;i ) scanf("%d",&arr[i]); | ||
mergesort(arr,0,n-1); | ||
for(int i=0;i<n;i ) printf("%d ",arr[i]); | ||
} | ||
|
||
void mergesort(int arr[],int l,int r){ | ||
if(l<r){ | ||
int mid=(l r)/2; | ||
mergesort(arr,l,mid); | ||
mergesort(arr,mid 1,r); | ||
merge(arr,l,mid,r); | ||
} | ||
} | ||
|
||
void merge(int arr[],int l,int mid,int r){ | ||
int i=l,j=mid 1,temp[r-l 1],k=0; | ||
while(i<=mid && j<=r){ | ||
if(arr[i]>=arr[j]){ | ||
temp[k]=arr[j]; | ||
k ; | ||
j ; | ||
} | ||
else{ | ||
temp[k]=arr[i]; | ||
k ; | ||
i ; | ||
} | ||
} | ||
while(i<=mid){ | ||
temp[k]=arr[i]; | ||
i ; | ||
k ; | ||
} | ||
while(j<=r){ | ||
temp[k]=arr[j]; | ||
j ; | ||
k ; | ||
} | ||
i=0; | ||
j=l; | ||
while(i<k){ | ||
arr[j]=temp[i]; | ||
i ; | ||
j ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,85 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#define max 10 | ||
void insert(int n); | ||
int search(int n); | ||
int display(); | ||
typedef struct node{ | ||
int data; | ||
struct node* link; | ||
}node; | ||
node* arr[max]; | ||
int main(void){ | ||
for(int i=0;i<max;i ) arr[i]=NULL; | ||
int ty,k,t; | ||
printf("\tMENU\n"); | ||
while(1){ | ||
printf("1.INSERT\n2.SEARCH\n3.TRAVERSE\n"); | ||
scanf("%d",&ty); | ||
switch(ty){ | ||
case 1:{ | ||
k=0; | ||
printf("Enter the element to be Inserted\n"); | ||
scanf("%d",&t); | ||
if(search(t)!=-1) printf("The element already Exists\n"); | ||
else insert(t); | ||
break; | ||
} | ||
case 2:{ | ||
printf("Enter the element to be searched\n"); | ||
scanf("%d",&t); | ||
k=search(t); | ||
if(k==-1) printf("Element not found\n"); | ||
else printf("Element found at index %d\n",k); | ||
break; | ||
} | ||
case 3:{ | ||
k=display(); | ||
if(k==-1) printf("Array empty"); | ||
printf("\n"); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void insert(int n){ | ||
int mod=n%10; | ||
node* newnode=malloc(sizeof(node*)); | ||
newnode->data=n; | ||
newnode->link=NULL; | ||
node* temp=arr[mod]; | ||
if(temp==NULL) arr[mod]=newnode; | ||
else { | ||
while(temp->link!=NULL) temp=temp->link; | ||
temp->link=newnode; | ||
} | ||
} | ||
|
||
int search(int n){ | ||
int mod=n%10; | ||
node* temp=arr[mod]; | ||
while(temp!=NULL){ | ||
if(temp->data==n) return mod; | ||
temp=temp->link; | ||
} | ||
return -1; | ||
} | ||
|
||
int display(){ | ||
int i=0,flag=0; | ||
node* temp; | ||
while(i<max){ | ||
temp=arr[i]; | ||
printf("[%d] ",i); | ||
while(temp!=NULL){ | ||
printf("%d -->",temp->data); | ||
flag=1; | ||
temp=temp->link; | ||
} | ||
printf("\n"); | ||
i ; | ||
} | ||
if(flag==1) return 1; | ||
else return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,37 @@ | ||
#include<stdio.h> | ||
void quicksort(int arr[],int low,int high); | ||
int partition(int arr[],int low,int high); | ||
int main(void){ | ||
int arr[10],n; | ||
printf("Enter the number of elements to be Inserted\n"); | ||
scanf("%d",&n); | ||
printf("Enter the elements of array\n"); | ||
for(int i=0;i<n;i ) scanf("%d",&arr[i]); | ||
quicksort(arr,0,n-1); | ||
for(int i=0;i<n;i ) printf("%d ",arr[i]); | ||
} | ||
|
||
void quicksort(int arr[],int low ,int high){ | ||
if(low<high){ | ||
int p=partition(arr,low,high); | ||
quicksort(arr,low,p-1); | ||
quicksort(arr,p 1,high); | ||
} | ||
} | ||
|
||
int partition(int arr[],int low,int high){ | ||
int pivot=arr[high]; | ||
int i=low-1; | ||
for(int j=low;j<high;j ){ | ||
if(arr[j]<pivot){ | ||
i ; | ||
int temp=arr[i]; | ||
arr[i]=arr[j]; | ||
arr[j]=temp; | ||
} | ||
} | ||
int temp=arr[i 1]; | ||
arr[i 1]=arr[high]; | ||
arr[high]=temp; | ||
return i 1; | ||
} |