Question

Can anyone help me to complete this C plus plus program? Please notice: You are required...

Can anyone help me to complete this C plus plus program?

Please notice:
You are required to implement insertion sort by using smart pointer.
Please notice:
You are required to implement insertion sort by using smart pointer.
Please notice:
You are required to implement insertion sort by using smart pointer.
Please notice:
You are required to implement insertion sort by using smart pointer.
Please notice:
You are required to implement insertion sort by using smart pointer.


//Implment insertion sort using smart pointers.
//You are not allowed to swap node values; instead, you are only
//allowed to change pointer addresses.
//Will explain more on this in class.


#include <iostream>
#include <memory>
using namespace std;

class node {
public:
        int value;
        //node * next;
        //node * previous;
        shared_ptr<node> next;
        weak_ptr<node> previous;
        //node(int i) { value = i; next = previous = nullptr; }
        node(int i) { value = i; }
        //node() { next = previous = nullptr; }
        node() {}
};

class doubly_linked_list {
public:
        //node * head;
        //node * tail;
        shared_ptr<node> head, tail;
        //doubly_linked_list() { head = tail = nullptr; }
        doubly_linked_list() {}
        void make_random_list(int m, int n);
        void print_forward();
        void print_backward();
        
        //***************************
        //You need to implement insertion_sort following the 
        //special requirements.
        void insertion_sort();
};

void doubly_linked_list::make_random_list(int m, int n) {

        for (int i = 0; i < m; i++) {
                //node * p1 = new node(rand() % n);
                shared_ptr<node> p1 = make_shared<node>(rand() % n);
                p1->previous = tail;
                //if (tail != nullptr ) tail->next = p1;
                if (tail) tail->next = p1;
                tail = p1;
                //if (head == nullptr) head = p1;
                if (!head) head = p1;
        }
}

void doubly_linked_list::print_forward() {
        cout << endl;
        //node * p1 = head;
        shared_ptr<node> p1 = head;
        //while (p1 != nullptr) {
        while (p1) {
                cout << p1->value << " ";
                p1 = p1->next;
        }
}

void doubly_linked_list::print_backward() {
        cout << endl;
        //node * p1 = tail;
        shared_ptr<node> p1 = tail;
        //while (p1 != nullptr) {
        while (p1) {
                cout << p1->value << " ";
                p1 = p1->previous.lock();
        }
}


int main() {
        doubly_linked_list d1;
        d1.make_random_list(30, 10);
        d1.print_forward();
        d1.print_backward();
        d1.insertion_sort();
        d1.print_forward();
        d1.print_backward();
        getchar();
        getchar();
        return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
Can anyone help me to complete this C plus plus program? Please notice: You are required...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • Fill the code in list.cpp app.cpp: #include "list.hpp" int main() {         LinkedList aList; bool success =...

    Fill the code in list.cpp app.cpp: #include "list.hpp" int main() {         LinkedList aList; bool success = false;         aList.evensFrontOddsEnd(3);         aList.evensFrontOddsEnd(10);         aList.evensFrontOddsEnd(6);         aList.evensFrontOddsEnd(9);         aList.evensFrontOddsEnd(17);         aList.evensFrontOddsEnd(12);         aList.printForward(); // output should be: 12 6 10 3 9 17         aList.printBackward(); // output should be: 17 9 3 10 6 12         success = aList.remove(17); if(success) cout << "17 Successfully Removed." << endl;         success = aList.remove(10); if(success) cout << "10 Successfully Removed." << endl; success = aList.remove(7); // 7 is not in the list, so success should...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario...

    Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario There are some classic data structures in computer science. One example of this that you've seen in the lectures is called the stack. In the next few exercises, we'll build yet another classic data structure, the linked list. To be exact, we'll be building the singly linked list. The building block of a singly linked list is a node, which consists of two parts:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT