Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions Circular_Linked_List.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include<stdio.h>
#include<stdlib.h>

struct node
{ int data;
struct node * link;
}*f=NULL,*e=NULL,*temp; //'f' points to first and 'e' points to last node

void insert(int data)
{
temp=(struct node *)malloc(sizeof(struct node)); //allocate node
temp->data=data; //put in data
if(f==NULL) //if list is empty make 'f' and 'e' point to temp
f=e=temp;
else //if list is not empty
{ e->link=temp;
e=temp;
}
e->link=f;
}


void del(int data)
{ int fl=0;
struct node *t;
if(f==NULL)
printf("Empty\n");
else if(f==e)
{ t=f;
f=e=NULL;
free(t);
}
else if(f->data==data)
{ t=f;
f=f->link;
e->link=f;
free(t);
}
else
{ temp=f;
while(1)
{ t=temp->link;
if(t!=f && t->data==data)
{ temp->link=t->link;
if(t==e)
e=temp;
free(t);
fl=2;
break;
}
else if(t==f)
break;
temp=temp->link;
fl=1;
}
if(fl==1)
printf("Element not found\n");
}
}

void display()
{ int fl=0;
if(f==NULL)
printf("Empty\n");
else
{ temp=f;
while(1)
{ printf("%d\n",temp->data);
if(f==e||(temp->link==f && fl==1))
break;
temp=temp->link;
fl=1;
}
}
}

void main()
{ int ch,prev,data;

while(1)
{ printf("\nMENU \n1.Insertion \n2.Deletion \n3.Exit \nEnter choice:");
scanf("%d",&ch);
if(ch==1)
{ printf("Enter element to be inserted:");
scanf("%d",&data);
insert(data);
display();
}
else if(ch==2)
{ printf("Enter element to be deleted:");
scanf("%d",&data);
del(data);
display();
}
else if(ch==3)
break;
else
printf("Invalid choice\n");
}
}
166 changes: 166 additions & 0 deletions Doubly_Linked_List.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#include<stdio.h>
#include<stdlib.h>

struct node
{ int data;
struct node *pre,*next;
}*f=NULL,*e=NULL,*temp;

void in_beg(int data)
{ /* 1. allocate node */
temp=(struct node *)malloc(sizeof(struct node));
/* 2. put in the data */
temp->data=data;
/* 3. Make next of new node as head and previous as NULL */
temp->pre=NULL;
if(f==NULL) //in case list is currently empty
{ temp->next=NULL;
e=temp;
}
else
{ temp->next=f;
f->pre=temp;
}
/* 5. move the head to point to the new node */
f=temp;
}

void in_end(int data)
{ temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
if(f==NULL)
{ temp->pre=NULL;
f=temp;
}
else
{ temp->pre=e;
e->next=temp;
}
e=temp;
}

void in_mid(int data,int prev)
{ int fl=0;
struct node *t;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;
if(f==NULL)
printf("Empty");
else
{ t=f;
while(t!=NULL)
{ if(t->data==prev)
{ temp->next=t->next;
t->next->pre=temp;
temp->pre=t;
t->next=temp;
fl=1;
break;
}
t=t->next;
}
if(fl==0)
printf("Node not found\n");
}
}

void del(int data)
{ struct node *t;
int fl=0;
if(f==NULL)
printf("Empty");
else
{
if(f==e && f->data==data)
{ t=f;
f=NULL;
e=NULL;
free(t);
}
else if(f->data==data)
{ t=f;
f=f->next;
f->pre=NULL;
free(t);
}
else if(e->data==data)
{ t=e;
e=e->pre;
e->next=NULL;
free(t);
}
else
{ temp=f;
while(temp!=NULL)
{ if(temp->data==data)
{ t=temp;
temp->pre->next=temp->next;
temp->next->pre=temp->pre;
free(t);
fl=1;
break;
}
temp=temp->next;
}
if(fl==0)
printf("Node not found\n");
}
}
}

void display()
{
if(f==NULL)
printf("Empty\n");
else
{ temp=f;
while(temp!=NULL)
{ printf("%d\n",temp->data);
temp=temp->next;
}
}
}

void main()
{ int i,data,ch,prev;

while(1)
{ printf("\n\nMENU \n1.Insertion at the begining \n2.Insertion at the end");
printf("\n3.Insertion at the middle \n4.Deletion \n5.Exit");
printf("\nEnter a choice:");
scanf("%d",&ch);
if(ch==1)
{ printf("Enter data:");
scanf("%d",&data);
in_beg(data);
display();
}
else if(ch==2)
{ printf("Enter data:");
scanf("%d",&data);
in_end(data);
display();
}
else if(ch==3)
{ printf("Enter data:");
scanf("%d",&data);
printf("Enter Previous data:");
scanf("%d",&prev);
in_mid(data,prev);
display();
}
else if(ch==4)
{ printf("Enter data to be deleted:");
scanf("%d",&data);
del(data);
display();
}
else if(ch==5)
break;
else
printf("Invalid choice");
}
}


5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ Use this repo to study or review your knowledge and don't forget to star and col
- Binary Search
- Linear Search

## Sorting Algorithms
- Bubble Sort
- Selection Sort
- Heap Sort

# How to contribute
Please comment your code thoroughly as to make it possible for anyone to understand. If possible, check your code using unit tests. Avoid all the bad implementations, make your code as clean as possible. After that, find the folder that fits the category of your code and submit a PR. Star this repo if the information here is useful to you.q
38 changes: 38 additions & 0 deletions Searching Algorithms/Selection_Sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning.
The algorithm maintains two subarrays in a given array.

1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.
*/


#include<stdio.h>

void main()
{ int a[100],n,i,j,temp; //'a' is an integer array of size 100
printf("Enter no. of elements: ");
scanf("%d",&n); //'n' stores the number of elements in the array
printf("Enter array:");
for(i=0;i<n;i++) //for loop to input elements of the array
scanf("%d",&a[i]);

//Sorting

for(i=0;i<n-1;i++)
{ for(j=i+1;j<n;j++)
{ if(a[i]>a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

printf("Sorted array :");
for(i=0;i<n;i++) //for loop to print sorted array
printf("%d ",a[i]);
printf("\n");
}
36 changes: 36 additions & 0 deletions Sieve/sieve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//This code compute the list of primes between 2 and N - 1 using the sieve algorithm that runs in O(nlogn)
#include <stdio.h>

#define N 100010
int is_composite[N];
int sz = 0;
int primes[N];

void sieve(){
for(int i = 2 ; i * i < N ; i++){//Run until square root of N
if(!is_composite[i]){//if 'i' is a prime number then mark all multiples of 'i' as a composite number
for(int j = i + i ; j < N ; j += i){
is_composite[j] = 1;
}
}
}

for(int i = 2 ; i < N ; i++){
if(!is_composite[i]){
primes[sz++] = i;
}
}
}

int main(){
sieve();

printf("we have %d primes between(2..%d)\n", sz, N - 1);
printf("Showing the 40 first primes....:\n");

for(int i = 0 ; i < 40 ; i++){
printf("%d\n", primes[i]);
}

return 0;
}
Loading