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
  • 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 =...

  • 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;...

  • 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:...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    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...

  • C++ assignment about doubly linked list

    You are required to write the following functions using this class: class Doubly_linked_list // Use a class Doubly_linked_list to represent an object {     public:     // constructor initialize the nextPtr     Doubly_linked_list()     {         prevPtr = 0; // point to null at the beginning         nextPtr = 0; // point to null at the beginning     }     // get a number     int GetNum()     {         return number;     }     // set a number     void SetNum(int num)     {         number = num;     }     // get the prev pointer     Doubly_linked_list ...

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
Active Questions
ADVERTISEMENT