From 3e0fc320e9b799898f46df0cc35166ff4e78c223 Mon Sep 17 00:00:00 2001 From: surbhi <20bcs114@nith.ac.in> Date: Mon, 25 Oct 2021 08:51:00 +0530 Subject: [PATCH 1/4] reverse-link-list --- Reverse-link_list/Iterative.cpp | 59 ++++++++++++++++++++++++++++ Reverse-link_list/Recursive.cpp | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 Reverse-link_list/Iterative.cpp create mode 100644 Reverse-link_list/Recursive.cpp diff --git a/Reverse-link_list/Iterative.cpp b/Reverse-link_list/Iterative.cpp new file mode 100644 index 0000000..6417028 --- /dev/null +++ b/Reverse-link_list/Iterative.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +struct node { + int data; + struct node *next; +}; + +// To create a demo we have to construct a linked list and this +// function is to push the elements to the list. +void push(struct node **head_ref, int data) { + struct node *node; + node = (struct node*)malloc(sizeof(struct node)); + node->data = data; + node->next = (*head_ref); + (*head_ref) = node; +} + +// Function to reverse the list +void reverse(struct node **head_ref) { + struct node *temp = NULL; + struct node *prev = NULL; + struct node *current = (*head_ref); + while(current != NULL) { + temp = current->next; + current->next = prev; + prev = current; + current = temp; + } + (*head_ref) = prev; +} + +// To check our program +void printnodes(struct node *head) { + while(head != NULL) { + cout<data<<" "; + head = head->next; + } +} + +// Driver function +int main() { + struct node *head = NULL; + push(&head, 0); + push(&head, 1); + push(&head, 8); + push(&head, 0); + push(&head, 4); + push(&head, 10); + cout << "Linked List Before Reversing" << endl; + printnodes(head); + reverse(&head); + cout << endl; + cout << "Linked List After Reversing"<data = data; + next = NULL; + } +}; + +struct LinkedList { + Node* head; + LinkedList() + { + head = NULL; + } + + Node* reverse(Node* head) + { + if (head == NULL || head->next == NULL) + return head; + // Recursive call + Node* rest = reverse(head->next); + head->next->next = head; + + head->next = NULL; + + return rest; + } + + void print() + { + struct Node* temp = head; + while (temp != NULL) { + cout << temp->data << " "; + temp = temp->next; + } + } + + void push(int data) + { + Node* temp = new Node(data); + temp->next = head; + head = temp; + } +}; + +int main() +{ + LinkedList ll; + ll.push(320); + ll.push(34); + ll.push(315); + ll.push(385); + + cout << "Linked List Before Reversing\n"; + ll.print(); + + ll.head = ll.reverse(ll.head); + + cout << "\nLinked List After Reversing \n"; + ll.print(); + return 0; +} From 9a1343253ef8eaae222d6944ad3c57ba73ce1ef4 Mon Sep 17 00:00:00 2001 From: surbhi <20bcs114@nith.ac.in> Date: Mon, 25 Oct 2021 08:58:30 +0530 Subject: [PATCH 2/4] Merge-sort --- Merge-sort/Merge-sort.cpp | 86 +++++++++++++++++++++++++++++++++++++++ New Bitmap Image.bmp | 0 2 files changed, 86 insertions(+) create mode 100644 Merge-sort/Merge-sort.cpp create mode 100644 New Bitmap Image.bmp diff --git a/Merge-sort/Merge-sort.cpp b/Merge-sort/Merge-sort.cpp new file mode 100644 index 0000000..77db92f --- /dev/null +++ b/Merge-sort/Merge-sort.cpp @@ -0,0 +1,86 @@ +// Merge sort in C++ + +#include +using namespace std; + +// Merge two subarrays L and M into arr +void merge(int arr[], int p, int q, int r) { + + // Create L ? A[p..q] and M ? A[q+1..r] + int n1 = q - p + 1; + int n2 = r - q; + + int L[n1], M[n2]; + + for (int i = 0; i < n1; i++) + L[i] = arr[p + i]; + for (int j = 0; j < n2; j++) + M[j] = arr[q + 1 + j]; + + // Maintain current index of sub-arrays and main array + int i, j, k; + i = 0; + j = 0; + k = p; + + // Until we reach either end of either L or M, pick larger among + // elements L and M and place them in the correct position at A[p..r] + while (i < n1 && j < n2) { + if (L[i] <= M[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = M[j]; + j++; + } + k++; + } + + // When we run out of elements in either L or M, + // pick up the remaining elements and put in A[p..r] + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + while (j < n2) { + arr[k] = M[j]; + j++; + k++; + } +} + +// Divide the array into two subarrays, sort them and merge them +void mergeSort(int arr[], int l, int r) { + if (l < r) { + // m is the point where the array is divided into two subarrays + int m = l + (r - l) / 2; + + mergeSort(arr, l, m); + mergeSort(arr, m + 1, r); + + // Merge the sorted subarrays + merge(arr, l, m, r); + } +} + +// Print the array +void printArray(int arr[], int size) { + for (int i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + +// Driver program +int main() { + int arr[] = {6, 5, 12, 10, 9, 1}; + int size = sizeof(arr) / sizeof(arr[0]); + + mergeSort(arr, 0, size - 1); + + cout << "Sorted array: \n"; + printArray(arr, size); + return 0; +} + diff --git a/New Bitmap Image.bmp b/New Bitmap Image.bmp new file mode 100644 index 0000000..e69de29 From fe84cc97a8b52f07481d0209478c56e0c45688f0 Mon Sep 17 00:00:00 2001 From: surbhi <20bcs114@nith.ac.in> Date: Mon, 25 Oct 2021 09:14:44 +0530 Subject: [PATCH 3/4] bubble-sort --- Bubble-sort/bubble-sort.cpp | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Bubble-sort/bubble-sort.cpp diff --git a/Bubble-sort/bubble-sort.cpp b/Bubble-sort/bubble-sort.cpp new file mode 100644 index 0000000..778a021 --- /dev/null +++ b/Bubble-sort/bubble-sort.cpp @@ -0,0 +1,47 @@ +// Bubble sort in C++ + +#include +using namespace std; + +// perform bubble sort +void bubbleSort(int array[], int size) { + + // loop to access each array element + for (int step = 0; step < (size-1); ++step) { + + // loop to compare array elements + for (int i = 0; i < size - (step-1); ++i) { + + // compare two adjacent elements + // change > to < to sort in descending order + if (array[i] > array[i + 1]) { + + // swapping elements if elements + // are not in the intended order + int temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + } + } + } +} + +// print array +void printArray(int array[], int size) { + for (int i = 0; i < size; ++i) { + cout << " " << array[i]; + } + cout << "\n"; +} + +int main() { + int data[] = {-2, 45, 0, 11, -9}; + + // find array's length + int size = sizeof(data) / sizeof(data[0]); + + bubbleSort(data, size); + + cout << "Sorted Array in Ascending Order:\n"; + printArray(data, size); +} From ae8fa2386dd44b40e5702dbf5dcbd07b70e181c2 Mon Sep 17 00:00:00 2001 From: surbhi <20bcs114@nith.ac.in> Date: Mon, 25 Oct 2021 09:19:18 +0530 Subject: [PATCH 4/4] nodes --- .../deleted-node_without-head_pointer.cpp | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Deleted-nodes_without head-pointer.cpp/deleted-node_without-head_pointer.cpp diff --git a/Deleted-nodes_without head-pointer.cpp/deleted-node_without-head_pointer.cpp b/Deleted-nodes_without head-pointer.cpp/deleted-node_without-head_pointer.cpp new file mode 100644 index 0000000..8c30bf1 --- /dev/null +++ b/Deleted-nodes_without head-pointer.cpp/deleted-node_without-head_pointer.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; + +class ListNode{ + public: + int val; + ListNode* next; +}; + +ListNode* creatnode(int d){ + ListNode* temp=(ListNode*)malloc(sizeof(ListNode)); + temp->val=d; + temp->next=NULL; + return temp; +} +void traverse(ListNode* head){ + ListNode* current=head; // current node set to head + int count=0; // to count total no of nodes + + printf("displaying the list\n"); + //traverse until current node isn't NULL + while(current!=NULL){ + count++; //increase node count + if(current->next) + printf("%d->",current->val); + else + printf("NULL\n"); + current=current->next; // go to next node + } + printf("total no of nodes : %d\n",count); +} + +void deleteNode(ListNode* node) { + //deleting node without head pointer + int temp; + ListNode* pre=node; + + while(node->next){ + pre = node; + node->val = (node->next)->val; + node=node->next; + } + pre->next=NULL; + free(node); +} + +int main(){ + ListNode* head=creatnode(4); + + head->next=creatnode(5); + head->next->next=creatnode(1); + head->next->next->next=creatnode(6); + head->next->next->next->next=creatnode(7); + head->next->next->next->next->next=creatnode(8); + + ListNode* todelete=head->next->next; //1 + + cout<<"deleting node with value 1\n"; + + cout<<"before deletion:\n"; + traverse(head); + + deleteNode(todelete); + + cout<<"after deletion:\n"; + traverse(head); + + return 0; +}