From 8ac3f2a609d2b7d3f508e84f4cd130da1e5f19c7 Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Sat, 23 May 2020 21:28:16 +0530 Subject: [PATCH 01/10] Create AscendingPriorityQueue.c --- data_structures/linked_list/APQ.C | 169 ++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 data_structures/linked_list/APQ.C diff --git a/data_structures/linked_list/APQ.C b/data_structures/linked_list/APQ.C new file mode 100644 index 00000000..5e49481d --- /dev/null +++ b/data_structures/linked_list/APQ.C @@ -0,0 +1,169 @@ +/* Ascending priority queue using Linked List - Program to implement Ascending priority queue using Linked List */ + +#include +#include +#define NULL 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") ; + getch(); + 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.") ; + getch() ; + 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->datadata ; + 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; +} + From 1494474db6e6167a4fee3e5127a4525835971dad Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Sat, 23 May 2020 21:28:35 +0530 Subject: [PATCH 02/10] Delete APQ.C --- data_structures/linked_list/APQ.C | 169 ------------------------------ 1 file changed, 169 deletions(-) delete mode 100644 data_structures/linked_list/APQ.C diff --git a/data_structures/linked_list/APQ.C b/data_structures/linked_list/APQ.C deleted file mode 100644 index 5e49481d..00000000 --- a/data_structures/linked_list/APQ.C +++ /dev/null @@ -1,169 +0,0 @@ -/* Ascending priority queue using Linked List - Program to implement Ascending priority queue using Linked List */ - -#include -#include -#define NULL 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") ; - getch(); - 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.") ; - getch() ; - 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->datadata ; - 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; -} - From 9ed17fc3c9c3ac4f7572cb266671894c67b57a30 Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Sat, 23 May 2020 21:29:54 +0530 Subject: [PATCH 03/10] Create Ascending Priority Queue Code to implement Ascending Priority Queue --- .../linked_list/ascendingPriorityQueue.c | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 data_structures/linked_list/ascendingPriorityQueue.c diff --git a/data_structures/linked_list/ascendingPriorityQueue.c b/data_structures/linked_list/ascendingPriorityQueue.c new file mode 100644 index 00000000..5e49481d --- /dev/null +++ b/data_structures/linked_list/ascendingPriorityQueue.c @@ -0,0 +1,169 @@ +/* Ascending priority queue using Linked List - Program to implement Ascending priority queue using Linked List */ + +#include +#include +#define NULL 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") ; + getch(); + 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.") ; + getch() ; + 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->datadata ; + 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; +} + From 2357e8bd69076199d0af849479f4985086bf7545 Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Sat, 23 May 2020 21:44:41 +0530 Subject: [PATCH 04/10] Create MergeNR.c Program to demonstrate Merge sort algorithm without recursion --- sorting/MERGENR.C | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 sorting/MERGENR.C diff --git a/sorting/MERGENR.C b/sorting/MERGENR.C new file mode 100644 index 00000000..81152949 --- /dev/null +++ b/sorting/MERGENR.C @@ -0,0 +1,82 @@ +/* Program to demonstrate non recursive merge sort */ + +#include + +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 Date: Tue, 26 May 2020 20:58:51 +0530 Subject: [PATCH 05/10] Update File A Demo Sample Test case added --- data_structures/linked_list/ascendingPriorityQueue.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/data_structures/linked_list/ascendingPriorityQueue.c b/data_structures/linked_list/ascendingPriorityQueue.c index 5e49481d..0088262d 100644 --- a/data_structures/linked_list/ascendingPriorityQueue.c +++ b/data_structures/linked_list/ascendingPriorityQueue.c @@ -1,4 +1,15 @@ /* Ascending priority queue using Linked List - Program to implement Ascending priority queue using Linked List */ +/** Input State of Queue + 1 + 2 2 + 1 + 3 2->3 + 2 3 + 3 + + Output + 2 +**/ #include #include From a652d51791fa0eeeb900bff776dec1bd5dc51d73 Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Tue, 26 May 2020 21:06:32 +0530 Subject: [PATCH 06/10] Update AscendingPriorityQueue.c Added the output of the file --- .../linked_list/ascendingPriorityQueue.c | 113 ++++++++++++++++-- 1 file changed, 102 insertions(+), 11 deletions(-) diff --git a/data_structures/linked_list/ascendingPriorityQueue.c b/data_structures/linked_list/ascendingPriorityQueue.c index 0088262d..e55b7e8f 100644 --- a/data_structures/linked_list/ascendingPriorityQueue.c +++ b/data_structures/linked_list/ascendingPriorityQueue.c @@ -1,15 +1,4 @@ /* Ascending priority queue using Linked List - Program to implement Ascending priority queue using Linked List */ -/** Input State of Queue - 1 - 2 2 - 1 - 3 2->3 - 2 3 - 3 - - Output - 2 -**/ #include #include @@ -178,3 +167,105 @@ int main() 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 + +*/ From 2bdf7732d8e3a2e315f2a57d2493ea299239d0cb Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Tue, 26 May 2020 21:11:22 +0530 Subject: [PATCH 07/10] Update MERGENR.C Added output of the program --- sorting/MERGENR.C | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sorting/MERGENR.C b/sorting/MERGENR.C index 81152949..3e32cad3 100644 --- a/sorting/MERGENR.C +++ b/sorting/MERGENR.C @@ -80,3 +80,22 @@ int main() //main function 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 +*/ From 2a4fd0b735711aeaafbeb5d6352ec9dbb9863d36 Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Thu, 28 May 2020 11:29:24 +0530 Subject: [PATCH 08/10] Update and rename ascendingPriorityQueue.c to ascendingpriorityqueue.c --- ...iorityQueue.c => ascendingpriorityqueue.c} | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) rename data_structures/linked_list/{ascendingPriorityQueue.c => ascendingpriorityqueue.c} (80%) diff --git a/data_structures/linked_list/ascendingPriorityQueue.c b/data_structures/linked_list/ascendingpriorityqueue.c similarity index 80% rename from data_structures/linked_list/ascendingPriorityQueue.c rename to data_structures/linked_list/ascendingpriorityqueue.c index e55b7e8f..fdce503b 100644 --- a/data_structures/linked_list/ascendingPriorityQueue.c +++ b/data_structures/linked_list/ascendingpriorityqueue.c @@ -1,8 +1,25 @@ /* 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 #include -#define NULL 0 +#define NULL ((void*)0) struct node { @@ -34,7 +51,6 @@ void insert(int x) if(pnode==NULL) { printf("Memory overflow. Unable to insert.\n") ; - getch(); exit(1) ; } @@ -58,7 +74,6 @@ int removes() if(empty()) { printf("\nQueue Underflow. Unable to remove.") ; - getch() ; exit(1) ; } From f4cbdd39cf846583ec179b2bea143e748a38486f Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Thu, 28 May 2020 11:40:03 +0530 Subject: [PATCH 09/10] Update and rename MERGENR.C to merge_sort_nr.c --- sorting/{MERGENR.C => merge_sort_nr.c} | 9 +++++++++ 1 file changed, 9 insertions(+) rename sorting/{MERGENR.C => merge_sort_nr.c} (64%) diff --git a/sorting/MERGENR.C b/sorting/merge_sort_nr.c similarity index 64% rename from sorting/MERGENR.C rename to sorting/merge_sort_nr.c index 3e32cad3..624b7411 100644 --- a/sorting/MERGENR.C +++ b/sorting/merge_sort_nr.c @@ -1,5 +1,14 @@ /* 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 void mergesort(int x[ ] , int n) ; From b86a4747ca0bee5498b6035beb3ec1bcd74a5398 Mon Sep 17 00:00:00 2001 From: Shubham Sah <42349247+shubhamsah@users.noreply.github.com> Date: Thu, 28 May 2020 12:26:56 +0530 Subject: [PATCH 10/10] Create queue_linked_list.c Implementation of Queue using Linked List. --- .../linked_list/queue_linked_list.c | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 data_structures/linked_list/queue_linked_list.c diff --git a/data_structures/linked_list/queue_linked_list.c b/data_structures/linked_list/queue_linked_list.c new file mode 100644 index 00000000..d9076fdb --- /dev/null +++ b/data_structures/linked_list/queue_linked_list.c @@ -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 +#include +#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; +}