diff --git a/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp b/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp new file mode 100644 index 0000000..00fdb3e --- /dev/null +++ b/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp @@ -0,0 +1,85 @@ + + +#include +using namespace std; + +// node creation +class Node +{ + public: + int data; + Node *next; +}; + +/* Function to insert a node at the beginning */ +void push(Node ** head_ref, int new_data) +{ + Node* new_node = new Node(); + new_node->data = new_data; + new_node->next = (*head_ref); + (*head_ref) = new_node; +} + +/* Utility function to print a singly linked list */ +void printList(Node *head) +{ + Node *temp = head; + while (temp != NULL) + { + cout<data<<" "; + temp = temp->next; + } + cout<next; + q_next = q_curr->next; + + // Make q_curr as next of p_curr + q_curr->next = p_next; // Change next pointer of q_curr + p_curr->next = q_curr; // Change next pointer of p_curr + + // Update current pointers for next iteration + p_curr = p_next; + q_curr = q_next; + } + + *q = q_curr; // Update head pointer of second list +} + + +int main() +{ + Node *p = NULL, *q = NULL; + push(&p, 3); + push(&p, 2); + push(&p, 1); + cout<<"First Linked List:\n"; + printList(p); + + push(&q, 8); + push(&q, 7); + push(&q, 6); + push(&q, 5); + push(&q, 4); + cout<<"Second Linked List:\n"; + printList(q); + + merge(p, &q); + + cout<<"Modified First Linked List:\n"; + printList(p); + + cout<<"Modified Second Linked List:\n"; + printList(q); + + return 0; +} \ No newline at end of file diff --git a/CPP/DataStructures/max_area_histogram.cpp b/CPP/DataStructures/max_area_histogram.cpp new file mode 100644 index 0000000..188a3c7 --- /dev/null +++ b/CPP/DataStructures/max_area_histogram.cpp @@ -0,0 +1,73 @@ +// C++ program to find maximum rectangular area in +// linear time +#include +using namespace std; + +// The main function to find the maximum rectangular +// area under given histogram with n bars +int getMaxArea(int hist[], int n) +{ + // Create an empty stack. The stack holds indexes + // of hist[] array. The bars stored in stack are + // always in increasing order of their heights. + stack s; + + int max_area = 0; // Initialize max area + int tp; // To store top of stack + int area_with_top; // To store area with top bar + // as the smallest bar + + // Run through all bars of given histogram + int i = 0; + while (i < n) { + // If this bar is higher than the bar on top + // stack, push it to stack + if (s.empty() || hist[s.top()] <= hist[i]) + s.push(i++); + + // If this bar is lower than top of stack, + // then calculate area of rectangle with stack + // top as the smallest (or minimum height) bar. + // 'i' is 'right index' for the top and element + // before top in stack is 'left index' + else { + tp = s.top(); // store the top index + s.pop(); // pop the top + + // Calculate the area with hist[tp] stack + // as smallest bar + area_with_top + = hist[tp] + * (s.empty() ? i : i - s.top() - 1); + + // update max area, if needed + if (max_area < area_with_top) + max_area = area_with_top; + } + } + + // Now pop the remaining bars from stack and calculate + // area with every popped bar as the smallest bar + while (s.empty() == false) { + tp = s.top(); + s.pop(); + area_with_top + = hist[tp] * (s.empty() ? i : i - s.top() - 1); + + if (max_area < area_with_top) + max_area = area_with_top; + } + + return max_area; +} + +// Driver code +int main() +{ + int hist[] = { 6, 2, 5, 4, 5, 1, 6 }; + int n = sizeof(hist) / sizeof(hist[0]); + + // Function call + cout << "Maximum area is " << getMaxArea(hist, n); + return 0; +}