// C++ implementation to sort a k sorted doubly
// linked list
#include <bits/stdc++.h>
using namespace std;
// a node of the doubly linked list
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// 'compare' function used to build up the
// priority queue
struct compare {
bool operator()(struct Node* p1, struct Node*
p2)
{
return p1->data >
p2->data;
}
};
// function to sort a k sorted doubly linked list
struct Node* sortAKSortedDLL(struct Node* head, int k)
{
// if list is empty
if (head == NULL)
return head;
// priority_queue 'pq' implemeted as min
heap with the
// help of 'compare' function
priority_queue<Node*, vector<Node*>,
compare> pq;
struct Node* newHead = NULL, *last;
// Create a Min Heap of first (k+1)
elements from
// input doubly linked list
for (int i = 0; head != NULL && i <= k;
i++) {
// push the node on to 'pq'
pq.push(head);
// move to the next
node
head = head->next;
}
// loop till there are elements in
'pq'
while (!pq.empty()) {
// place root or top of
'pq' at the end of the
// result sorted list so far having
the first node
// pointed to by 'newHead'
// and adjust the required
links
if (newHead == NULL) {
newHead =
pq.top();
newHead->prev
= NULL;
//
'last' points to the last node
// of the result
sorted list so far
last =
newHead;
}
else {
last->next =
pq.top();
pq.top()->prev = last;
last =
pq.top();
}
// remove element from
'pq'
pq.pop();
// if there are more
nodes left in the input list
if (head != NULL) {
// push the node
on to 'pq'
pq.push(head);
//
move to the next node
head =
head->next;
}
}
// making 'next' of last node point to
NULL
last->next = NULL;
// new head of the required sorted
DLL
return newHead;
}
// Function to insert a node at the beginning
// of the Doubly Linked List
void push(struct Node** head_ref, int new_data)
{
// allocate node
struct Node* new_node =
(struct Node*)malloc(sizeof(struct
Node));
// put in the data
new_node->data = new_data;
// since we are adding at the
begining,
// prev is always NULL
new_node->prev = NULL;
// link the old list off the new node
new_node->next = (*head_ref);
// change prev of head node to new
node
if ((*head_ref) != NULL)
(*head_ref)->prev =
new_node;
// move the head to point to the new
node
(*head_ref) = new_node;
}
// Function to print nodes in a given doubly linked
list
void printList(struct Node* head)
{
// if list is empty
if (head == NULL)
cout << "Doubly Linked list
empty";
while (head != NULL) {
cout << head->data
<< " ";
head = head->next;
}
}
// Driver program to test above
int main()
{
struct Node* head = NULL;
// Create the doubly linked list:
//
3<->6<->2<->12<->56<->8
push(&head, 8);
push(&head, 56);
push(&head, 12);
push(&head, 2);
push(&head, 6);
push(&head, 3);
int k = 2;
cout << "Original Doubly linked
list:n";
printList(head);
// sort the biotonic DLL
head = sortAKSortedDLL(head, k);
cout << "\nDoubly linked list after
sorting:n";
printList(head);
return 0;
}
-PLEASE PROVIDE CLEAR AND CONCISE C++ CODE THAT HAS BEEN TESTED AND WORKS PROPERLY -PLEASE PROVIDE...
PLEASE USE C++ Source Code
Attached is a linked list with 2 nodes. You can use this or
write a similar one. The assignment is to write 2 functions. One
function will add another node at the end of the list. The other
function will delete a node. Don't forget - No dangling pointers
!
Example linked source code attached below
#include<iostream>
using namespace std;
class Node
{
int data;
Node *next;
public:
void setdata(int d) {data = d;}
void...
C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or add any additional code. Do not use global variables. #include <iostream> #include <string> using std::endl; using std::cout; using std::cin; using std::string; void pressAnyKeyToContinue() { printf("Press any key to continue\n"); cin.get(); } //This helps with testing, do not modify. bool checkTest(string testName, int whatItShouldBe, int whatItIs) { if (whatItShouldBe == whatItIs) { cout << "Passed " << testName << endl; return true; } else...
C++ Error. I'm having trouble with a pointer. I keep getting the
error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)"
The objective of the assignment is to revise the public method
add in class template LinkedBag so that the new
node is inserted at the end of the linked chain instead of at the
beginning.
This is the code I have:
linkedBag-driver.cpp
BagInterface.hpp
LinkedBag.hpp
Node.hpp
int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...
Data Structures, algorithm, c++. Please explain if possible.
Thank you!
Complete the following function that returns the largest element of the binary tree rooted at root. template <typename T> T max (Node<T>root) if (root nullptr) return TO; T max1 = root->x; T max2 root->x; if (root->left != nullptr) maxlmax (root->left); if (root->right != nullptr) max2 max ( root->right); - if (root->x >= max1 && root->x >= max2) return __.-; if (max1 >= root->x && max1 >= max2) return-_-> return___--> Let...
PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...
Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...
Please answer in C++ Derive a class called Queue from the linked list described in Assignment 2 (list of Dates). This means the Queue class will inherit all the properties (data and functions) of the linked list. But, since a queue allows pushing only at the back and popping at the front of the list, you will need to prevent the addition in the front and removal at the back. To do this, you must derive the Queue class in...
Please answer in C++. Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions...
In this assignment, you will implement a sort method on
singly-linked and doubly-linked lists.
Implement the following sort member function on a singly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
Implement the following sort member function on a doubly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
The sort(…) methods take as a parameter a comparator function,
having a default assignment of defaultCompare, a
static function defined as follows:
template <typename T>
static bool defaultCompare(const...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...