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");
}
65 changes: 65 additions & 0 deletions Sorting Algorithm/Heap_Sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Heap sort is a comparison based sorting technique based on Binary Heap data structure.
It is similar to selection sort where we first find the maximum element and place the maximum element at the end.
The same process is repeated for remaining elements.

A Binary Heap is a Complete Binary Tree where items are stored in a special order such that value in a parent node is greater(or smaller) than the values in its two children nodes.
The former is called as max heap and the latter is called min heap. The heap can be represented by binary tree or array.

Since a Binary Heap is a Complete Binary Tree, it can be easily represented as array and array based representation is space efficient.
If the parent node is stored at index I, the left child can be calculated by 2 * I + 1 and right child by 2 * I + 2 (assuming the indexing starts at 0).

Heap Sort Algorithm for sorting in increasing order:
1. Build a max heap from the input data.
2. At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of tree.
3. Repeat above steps while size of heap is greater than 1.
*/

#include <stdio.h>

void swap(int *a, int *b) //function to swap 2 values
{
int t=*b;
*b=*a;
*a=t;
}

void buildHeap(int a[], int curr, int size) //function to build heap
{
int left = 2 * curr + 1, right = 2 * curr + 2, largest = curr;
if(left < size)
largest = a[largest] > a[left] ? largest : left;
if(right < size)
largest = a[largest] > a[right] ? largest : right;
if( largest != curr )
{
swap(&a[largest], &a[curr]);
buildHeap(a,largest,size);
}
}

void HeapSort(int a[], int size) //function to sort
{
int i;
for(i= size/2 - 1;i >= 0; --i)
buildHeap(a,i,size);
for (i = size - 1; i >= 0; i--)
{
swap(&a[0], &a[i]);
buildHeap(a, 0, i);
}
}

void main()
{
int size, a[200],i;
printf("Enter the size of the array :");
scanf("%d",&size);
printf("Enter the elements :\n");
for(i = 0; i < size; ++i)
scanf("%d",&a[i]);
HeapSort(a,size);
printf("Sorted Array : \n");
for(i = 0; i < size; ++i)
printf("%d ",a[i]);
}
Loading