-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 28e51b3
Showing
73 changed files
with
2,774 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,77 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// Function to perform counting sort on a specific digit | ||
void countingSort(int arr[], int n, int exp) | ||
{ | ||
int output[n]; | ||
int count[10] = {0}; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
count[(arr[i] / exp) % 10]++;//find the last element | ||
} | ||
|
||
for (int i = 1; i < 10; i++) | ||
{ | ||
count[i] += count[i - 1];//counting sort | ||
} | ||
|
||
for (int i = n - 1; i >= 0; i--) | ||
{ | ||
output[count[(arr[i] / exp) % 10] - 1] = arr[i]; | ||
count[(arr[i] / exp) % 10]--; | ||
} | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
arr[i] = output[i]; | ||
} | ||
} | ||
|
||
// Radix Sort function | ||
void radixSort(int arr[], int n, int digits) | ||
{ | ||
int max = arr[0]; | ||
for (int i = 1; i < n; i++) | ||
{ | ||
if (arr[i] > max) | ||
{ | ||
max = arr[i]; | ||
} | ||
} | ||
|
||
for (int exp = 1; max / exp > 0; exp *= 10) | ||
{ | ||
countingSort(arr, n, exp); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
printf("Enter the number of elements: "); | ||
scanf("%d", &n); | ||
|
||
int arr[n]; | ||
printf("Enter %d elements:\n", n); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
scanf("%d", &arr[i]); | ||
} | ||
|
||
int digits; | ||
printf("Enter the number of digits in the maximum number: "); | ||
scanf("%d", &digits); | ||
|
||
radixSort(arr, n, digits); | ||
|
||
printf("Sorted array: "); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
printf("%d ", arr[i]); | ||
} | ||
printf("\n"); | ||
|
||
return 0; | ||
} |
Binary file not shown.
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,9 @@ | ||
#include<stdio.h> | ||
|
||
int main() { | ||
int a=56; | ||
int *b = &a;//b is pointer pointing to a | ||
printf("the address of a is %x\n", b); //%x is format specifier that gives hexa decimal | ||
printf("the value of a is %d\n", *b); | ||
|
||
} |
Binary file not shown.
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,161 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<math.h> | ||
#include<string.h> | ||
struct node{ | ||
int data; | ||
struct node *next; | ||
}; | ||
|
||
struct node *Create(char c){ | ||
struct node *ptr=malloc(sizeof(struct node)); | ||
ptr->data=c-'0'; | ||
ptr->next=NULL; | ||
return ptr; | ||
} | ||
|
||
void display(struct node *head){ | ||
struct node *temp=head; | ||
while(temp!=NULL){ | ||
printf("%d",temp->data); | ||
temp=temp->next; | ||
} | ||
printf("\n"); | ||
} | ||
|
||
struct node *rev(struct node *head){ | ||
struct node *temp; | ||
struct node *temp1; | ||
struct node *temp2; | ||
struct node *prev; | ||
temp=head; | ||
int count=0; | ||
while(temp!=NULL){ | ||
count++; | ||
temp=temp->next; | ||
} | ||
for(int i=0;i<count-1;i++){ | ||
temp=head;prev=head; | ||
for(int j=0;j<count-i-1;j++){ | ||
if(temp==head){ | ||
temp1=temp->next->next; | ||
temp2=temp->next; | ||
temp->next=temp1; | ||
temp2->next=temp; | ||
head=temp2; | ||
prev=head; | ||
} | ||
else{ | ||
temp1=temp->next; | ||
temp2=temp->next->next; | ||
prev->next=temp1; | ||
temp1->next=temp; | ||
temp->next=temp2; | ||
prev=temp1; | ||
} | ||
} | ||
} | ||
return head; | ||
} | ||
|
||
struct node *sum(struct node *head1,struct node *head2){ | ||
int carry=0; | ||
struct node *temp1,*temp2,*temp; | ||
temp1=head1; | ||
temp2=head2; | ||
int count1=0,count2=0; | ||
while (temp1!=NULL){ | ||
count1++; | ||
temp1=temp1->next; | ||
} | ||
while (temp2!=NULL){ | ||
count2++; | ||
temp2=temp2->next; | ||
} | ||
struct node *head; | ||
head=(struct node*)malloc(sizeof(struct node)); | ||
head->data=0; | ||
head->next=NULL; | ||
temp=head; | ||
if(count1==count2){ | ||
for(int i=0;i<count1;i++){ | ||
temp->next=Create('0'); | ||
temp=temp->next; | ||
} | ||
} | ||
else if(count1>count2){ | ||
for(int i=0;i<count1;i++){ | ||
temp->next=Create('0'); | ||
temp=temp->next; | ||
} | ||
temp2=head2; | ||
while (temp2->next!=NULL) | ||
temp2=temp2->next; | ||
for(int i=0;i<count1-count2;i++){ | ||
temp2->next=Create('0'); | ||
temp2=temp2->next; | ||
} | ||
} | ||
else if (count1<count2){ | ||
for(int i=0;i<count2;i++){ | ||
temp->next=Create('0'); | ||
temp=temp->next; | ||
} | ||
temp1=head1; | ||
while (temp1->next!=NULL) | ||
temp1=temp1->next; | ||
for(int i=0;i<count2-count1;i++){ | ||
temp1->next=Create('0'); | ||
temp1=temp1->next; | ||
} | ||
} | ||
temp1=head1; | ||
temp2=head2; | ||
temp=head; | ||
while(temp1!=NULL){ | ||
temp->data=(temp1->data+temp2->data+carry)%10; | ||
carry=(temp1->data+temp2->data+carry)/10; | ||
temp=temp->next; | ||
temp1=temp1->next; | ||
temp2=temp2->next; | ||
} | ||
if(carry) | ||
temp->data=carry; | ||
head=rev(head); | ||
if(head->data==0){ | ||
head=head->next; | ||
return head; | ||
} | ||
else if(head->data!=0) | ||
return head; | ||
} | ||
|
||
int main(){ | ||
char str1[1000]; | ||
printf("Enter first number: "); | ||
scanf("%s",str1); | ||
struct node *head1; | ||
head1=(struct node*)malloc(sizeof(struct node)); | ||
head1->data=str1[0]-'0'; | ||
struct node *temp=head1; | ||
for(int i=1;str1[i]!='\0';i++){ | ||
temp->next=Create(str1[i]); | ||
temp=temp->next; | ||
} | ||
struct node *head3=rev(head1); | ||
char str2[1000]; | ||
printf("Enter second number: "); | ||
scanf("%s",str2); | ||
struct node *head2; | ||
head2=(struct node*)malloc(sizeof(struct node)); | ||
head2->data=str2[0]-'0'; | ||
temp=head2; | ||
for(int i=1;str2[i]!='\0';i++){ | ||
temp->next=Create(str2[i]); | ||
temp=temp->next; | ||
} | ||
struct node *head4=rev(head2); | ||
struct node *result=sum(head3,head4); | ||
display(result); | ||
return 0; | ||
} |
Binary file not shown.
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,75 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
// Structure to represent a non-zero element in the sparse matrix | ||
struct Node { | ||
int row; | ||
int col; | ||
int value; | ||
struct Node* next; | ||
}; | ||
|
||
// Function to create a new node | ||
struct Node* createNode(int row, int col, int value) { | ||
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); | ||
if (newNode == NULL) { | ||
printf("Memory allocation failed\n"); | ||
exit(1); | ||
} | ||
newNode->row = row; | ||
newNode->col = col; | ||
newNode->value = value; | ||
newNode->next = NULL; | ||
return newNode; | ||
} | ||
|
||
// Function to insert a new node at the end of the linked list | ||
void insertNode(struct Node** head, int row, int col, int value) { | ||
struct Node* newNode = createNode(row, col, value); | ||
if (*head == NULL) { | ||
*head = newNode; | ||
} else { | ||
struct Node* current = *head; | ||
while (current->next != NULL) { | ||
current = current->next; | ||
} | ||
current->next = newNode; | ||
} | ||
} | ||
|
||
// Function to display the linked list representation of the sparse matrix | ||
void displaySparseMatrix(struct Node* head) { | ||
if (head == NULL) { | ||
printf("Sparse matrix is empty.\n"); | ||
return; | ||
} | ||
printf("Row\tColumn\tValue\n"); | ||
while (head != NULL) { | ||
printf("%d\t%d\t%d\n", head->row, head->col, head->value); | ||
head = head->next; | ||
} | ||
} | ||
|
||
int main() { | ||
int rows, cols; | ||
printf("Enter the number of rows and columns in the sparse matrix: "); | ||
scanf("%d %d", &rows, &cols); | ||
|
||
struct Node* sparseMatrix = NULL; | ||
|
||
printf("Enter the elements of the sparse matrix:\n"); | ||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
int value; | ||
scanf("%d", &value); | ||
if (value != 0) { | ||
insertNode(&sparseMatrix, i, j, value); | ||
} | ||
} | ||
} | ||
|
||
printf("\nLinked list representation of the sparse matrix:\n"); | ||
displaySparseMatrix(sparseMatrix); | ||
|
||
return 0; | ||
} |
Binary file not shown.
Oops, something went wrong.