Merge pull request #538 from shubhamsah/master

Added Priority Queue, Queue (link list) and Merge Sort
This commit is contained in:
Sombit Bose 2020-05-28 15:12:18 +05:30 committed by GitHub
commit 1c07d61ce3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 545 additions and 0 deletions

View File

@ -0,0 +1,286 @@
/* Ascending priority queue using Linked List - Program to implement Ascending priority queue using Linked List */
/*A priority queue is a special type of queue in which each element is associated with a priority and is served according
to its priority. If elements with the same priority occur, they are served according to their order in the queue.
Generally, the value of the element itself is considered for assigning the priority.
For example: The element with the highest value is considered as the highest priority element. However, in other cases,
we can assume the element with the lowest value as the highest priority element. In other cases,
we can set priorities according to our needs.
In a queue, the first-in-first-out rule is implemented whereas, in a priority queue, the values are removed on the basis of priority.
The element with the highest priority is removed first.
insert() - Would insert an element in a queue
delete() - Would delete the smallest element in the queue
*/
#include <stdio.h>
#include <stdlib.h>
#define NULL ((void*)0)
struct node
{
int data ;
struct node *next ;
} ;
struct node *front , *rear ;
/* This function initializes the queue to empty by making both front and rear as NULL */
void createqueue()
{
front=rear=NULL ;
}
int empty()
{
if(front==NULL)
return 1 ;
else
return 0 ;
}
void insert(int x)
{
struct node *pnode ;
pnode=(struct node*)malloc(sizeof(struct node)) ;
if(pnode==NULL)
{
printf("Memory overflow. Unable to insert.\n") ;
exit(1) ;
}
pnode->data=x ;
pnode->next=NULL; /* New node is always last node */
if(empty())
front=rear=pnode ;
else
{
rear->next=pnode ;
rear=pnode ;
}
}
int removes()
{
int min ;
struct node *follow , *follow1 , *p , *p1 ;
if(empty())
{
printf("\nQueue Underflow. Unable to remove.") ;
exit(1) ;
}
/* finding the node with minimum value in the APQ.*/
p=p1=front ;
follow=follow1=NULL ;
min=front->data ;
while(p!=NULL)
{
if(p->data<min)
{
min=p->data ;
follow1=follow ;
p1=p ;
}
follow=p ;
p=p->next ;
}
/* Deleting the node with min value */
if(p1==front) /* deleting first node.*/
{
front=front->next ;
if(front==NULL) /* Deleting the only one node */
rear=NULL ;
}
else if(p1==rear) /* Deleting last node */
{
rear=follow1 ;
rear->next=NULL ;
}
else /* deleting any other node.*/
follow1->next=p1->next ;
free(p1) ;
return min ; /* DONT FORGET LAST 2 STATEMENTS.*/
}
void show()
{
struct node *p ;
if(empty())
printf("Queue empty. No data to display \n") ;
else
{
printf("Queue from front to rear is as shown: \n") ;
p=front ;
while(p!=NULL)
{
printf("%d ",p->data) ;
p=p->next ;
}
printf("\n") ;
}
}
void destroyqueue()
{
front=rear=NULL ;
}
int main()
{
int x , ch ;
createqueue() ;
do
{
printf("\n\n Menu: \n") ;
printf("1:Insert \n") ;
printf("2:Remove \n") ;
printf("3:exit \n") ;
printf("Enter your choice: ") ;
scanf("%d",&ch) ;
switch(ch)
{
case 1:
printf("Enter element to be inserted: ") ;
scanf("%d",&x) ;
insert(x) ;
show() ;
break ;
case 2:
x=removes() ;
printf("Element removed is: %d\n",x) ;
show() ;
break ;
case 3:
break ;
}
}
while(ch!=3) ;
destroyqueue() ;
return 0;
}
/* Output of the Program*/
/*
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 12
Queue from front to rear is as shown:
12
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 1
Queue from front to rear is as shown:
12 1
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 14
Queue from front to rear is as shown:
12 1 14
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 3
Queue from front to rear is as shown:
12 1 14 3
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 5
Queue from front to rear is as shown:
12 1 14 3 5
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 1
Queue from front to rear is as shown:
12 14 3 5
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 3
Queue from front to rear is as shown:
12 14 5
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 5
Queue from front to rear is as shown:
12 14
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 12
Queue from front to rear is as shown:
14
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 14
Queue empty. No data to display
*/

View File

@ -0,0 +1,149 @@
/* Queue using Linked List - Program to create a queue ADT using linked list. ADT should support the following operations
1) Createqueue
2) Insert into the queue
3) Delete from the queue
4) destroyqueue
*/
/* queue q declared globally */
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
struct node
{
int data ;
struct node *next ;
} ;
struct queue
{
struct node *front , *rear ;
} ;
struct queue q ;
/* This function initializes the queue to empty by making both front and rear as NULL */
void createqueue()
{
q.front=q.rear=NULL ;
}
int empty()
{
if(q.front==NULL)
return 1 ;
else
return 0 ;
}
void insert(int x)
{
struct node *pnode ;
pnode=(struct node*)malloc(sizeof(struct node)) ;
if(pnode==NULL)
{
printf("Memory overflow. Unable to insert.\n") ;
exit(1) ;
}
pnode->data=x ;
pnode->next=NULL ; /* New node is always last node */
if(empty())
q.front=q.rear=pnode ;
else
{
(q.rear)->next=pnode ;
q.rear=pnode ;
}
}
int removes()
{
int x ;
struct node *p ;
if(empty())
{
printf("Queue Underflow. Unable to remove.\n") ;
exit(1) ;
}
p=q.front ;
x=(q.front)->data ;
q.front=(q.front)->next ;
if(q.front==NULL) /* Queue contained only one node */
q.rear=NULL ;
free(p) ;
return x ;
}
void show()
{
struct node *p ;
if(empty())
printf("Queue empty. No data to display \n") ;
else
{
printf("Queue from front to rear is as shown: \n") ;
p=q.front ;
while(p!=NULL)
{
printf("%d ",p->data) ;
p=p->next ;
}
printf("\n") ;
}
}
void destroyqueue()
{
q.front=q.rear=NULL ;
}
int main()
{
int x , ch ;
createqueue() ;
do
{
printf("\n\n Menu: \n") ;
printf("1:Insert \n") ;
printf("2:Remove \n") ;
printf("3:exit \n") ;
printf("Enter your choice: ") ;
scanf("%d",&ch) ;
switch(ch)
{
case 1:
printf("Enter element to be inserted: ") ;
scanf("%d",&x) ;
insert(x) ;
show() ;
break ;
case 2:
x=removes() ;
printf("Element removed is: %d\n",x) ;
show() ;
break ;
case 3:
break ;
}
}
while(ch!=3) ;
destroyqueue() ;
return 0;
}

110
sorting/merge_sort_nr.c Normal file
View File

@ -0,0 +1,110 @@
/* Program to demonstrate non recursive merge sort */
/* Merge sort is an effective sorting algorithm which falls under divide and conquer paradigm and produces a stable sort.
Merge sort repeatedly breaks down a list into several sublists until each sublist consists of a single element and merging those
sublists in a manner that results into a sorted list.
Bottom-Up Merge Sort Implementation:
The Bottom-Up merge sort approach uses iterative methodology. It starts with the single-element array, and combines two adjacent elements and
also sorting the two at the same time.
The combined-sorted arrays are again combined and sorted with each other until one single unit of sorted array is achieved. */
#include <stdio.h>
void mergesort(int x[ ] , int n) ;
void show(int x[ ] , int n) ;
void mergesort(int x[ ] , int n)
{
int temp[50] , i , j , k , lb1 , lb2 , ub1 , ub2 , size ;
size=1 ;
while(size<n)
{
lb1=0 ;
k=0 ;
while(lb1+size<n)
{
lb2=lb1+size ;
ub1=lb2-1 ;
if(ub1+size<n)
ub2=ub1+size ;
else
ub2=n-1 ;
i=lb1 ;
j=lb2 ;
while(i<=ub1&&j<=ub2)
if(x[i]<x[j])
temp[k++]=x[i++] ;
else
temp[k++]=x[j++] ;
while(i<=ub1)
temp[k++]=x[i++] ;
while(j<=ub2)
temp[k++]=x[j++] ;
lb1=ub2+1 ;
}
for(i=0 ; i<=ub2 ; i++)
x[i]=temp[i] ;
size=size*2 ;
show(x,n) ;
}
}
// function to show each pass
void show(int x[ ] , int n)
{
int i ;
for(i=0 ; i<n ; i++)
printf("%d " , x[i]) ;
printf("\n\n") ;
}
int main() //main function
{
int i , n , x[20] ;
printf("Enter the number of elements: ") ;
scanf("%d",&n) ;
printf("Enter the elements:\n") ;
for(i=0 ; i<n ; i++)
scanf("%d",&x[i]) ;
mergesort(x,n) ;
printf("Sorted array is as shown:\n") ;
for(i=0 ; i<n ; i++)
printf("%d " , x[i]) ;
return 0;
}
/* Output of the Program*/
/*
Enter the number of elements: 5
Enter the elements:
15
14
13
12
11
14 15 12 13 11
12 13 14 15 11
11 12 13 14 15
Sorted array is as shown:
11 12 13 14 15
*/