Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

vectors class #include #include using namespace std; int main() { vector v; vector ::iterator it; int c, i; while (1) { cout > c; switch

vectors class

#include #include using namespace std; int main() { vector v; vector::iterator it; int c, i; while (1) { cout > c; switch (c) { case 1: // Asks the user vector size and its valuses cout > i; v.push_back(i); break; case 3: cout

book class

#include #include using namespace std;

//defined exception class myexception1 : public exception { virtual const char* what() const throw() { return " !!!!ERROR!!!!! Empty String"; } } EmptyStringException;

//user defined exception class myexception2 : public exception { virtual const char* what() const throw() { return " !!!!ERROR!!!! Less than 1900"; } } InvalidPublishYearException;

//class Book class Book { private: string name; string author; int publishYear; int pages;

public:

Book(string name, string author, int publishYear, int pages) //constructors { //check if name is Empty if (name == "") { throw EmptyStringException; } //check if author is Empty if (author == "") { throw EmptyStringException; } //check if publishYear is less than 1900 if (publishYear author = author; this->name = name; this->publishYear = publishYear; this->pages = pages; cout

string getName() //return values { return name; } string getAuthor() { return author; } int getPublishYear() { return publishYear; } int getPages() { return pages; }

};

int main() { string name; string author; int year; int pages; char ans;

do { cout > name; cout > author; cout > year; cout > pages; cout

try {

Book Book(name, author, year, pages); } catch (exception & e) { //prints the error message cout

cout > ans; cout

return 0; }

I need help completing the link list program at the top

Expert Answer

  • image text in transcribedcorrection answered this

    Was this answer helpful?

    0

    0

    228 answers

    Node.h

    ================

    #pragma once

    template class Node { private: Node* next; // A pointer to the next Node(if any) in the chain of Nodes. Node* prev; // A pointer to the previous Node(if any) in the chain of Nodes. T data; // data stored in current node public: // constructor creates a node with next and prev set to null Node(T e) { data = e; next = nullptr; prev = nullptr; } // default constructor Node() { data = NULL; next = nullptr; prev = nullptr; } // constructor with all parameters Node(T e, Node* next, Node* prev) { data = e; this->next = next; this->prev = prev; } // getter functions T getdata() { return data; } Node* getNext() { return next; } Node* getPrev() { return prev; } // setter functions void setData(T e) { this.data = e; } void setNext(Node* next) { this->next = next; } void setPrev(Node* prev) { this->prev = prev; } };

    ===============================

    LinkedList.h

    ===============================

    #pragma once #include "Node.h" #include

    template class LinkedList { private: Node* mHead; // Points the first Node in the chain Node* mTail; // Points the last Node in the chain int mSize; // Keeps a count of the number of Nodes in the chain public: // A default constructor sets both pointers to null and sets the size to 0 LinkedList() { mHead = nullptr; mTail = nullptr; mSize = 0; } // A copy constructor that takes a LinkedList object and performs a deep copy of the passed list into this list LinkedList(const LinkedList& rhs) { // copy data members mSize = rhs.mSize; mHead = new Node(rhs.mHead->data, nullptr, mHead); Node* itr1 = mHead; Node* itr2 = rhs.mHead; while (itr2->next != nullptr) { itr1->next = new Node(itr2->next->data, nullptr, itr1); } mTail = itr1; } // A destructor that performs pop_front on the list while the list is not empty ~LinkedList(){ while (mSize > 0) { this->pop_front(); } } // LinkedList & An overloaded assignment operator that deletes the current array // and replaces it with deep copy of the passed list LinkedList& operator=(const LinkedList& rhs){ }

    // Returns the size of the LinkedList int size() const { return mSize; }

    // Creates a new Node and assigns it to the end of the list of Nodes while updating the size by 1 void push_back(const T e){ // check if list is empty if (mTail == nullptr) { mTail = new Node(e, nullptr, nullptr); mHead = mTail; mSize++; } else { mTail->setNext(new Node(e, nullptr, mTail)); mTail = mTail->getNext(); mSize++; } }

    // Creates a new Node and assigns it to the front of the list of Nodes while updating the size by 1 void push_front(const T e){ // check if list is empty if (mHead == nullptr) { mHead = Node(e, nullptr, nullptr); mTail = mHead; mSize++; } else { mHead->setPrev(new Node(e, mHead, nullptr)); mHead = mHead->getPrev(); mSize++; } }

    // Deletes the Node at the end of the list(or throws an error if this is not possible) // Decreases size by 1 // Returns the deleted element T pop_back(){ if (mSize == 0) { throw std::invalid_argument("Index out of bounds."); } Node* temp = mTail; mTail = mTail->getPrev(); mTail->setNext(nullptr); T elem = temp->getdata(); delete temp; mSize--; return elem; }

    // Deletes the Node at the front of the list(or throws an error if this is not possible) // Decreases size by 1 // Returns the deleted element T pop_front(){ if (mSize == 0) { throw std::invalid_argument("Index out of bounds."); } Node* temp = mHead; mHead = mHead->getNext(); mHead->setPrev(nullptr); T elem = temp->getdata(); delete temp; mSize--; return elem; }

    // Returns the element stored at the specified index // If the index is out - of - bounds(either for being negative or greater than / equal the size of the list), // throw invalid\_argument("Index out of bounds."); //This will require you to include the stdexcept library.This method should operate at O(1) //for accessing the first or last element in a listand at O(n) for any element other than the first or the last. T at(int i) const{ if (i = mSize) { throw std::invalid_argument("Index out of bounds."); } Node* itr = mHead; for (int j = 0; j getNext(); } return itr->getdata(); } // Returns the first element T front() const{ return mHead->getdata(); }

    // Returns the last element T back() const{ return mTail->getdata(); }

  • can I get this code translated to c++ please

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Issues In Database Management A Refernce For The Thinking Practitioner

Authors: Fabian Pascal

1st Edition

0201485559, 978-0201485554

More Books

Students also viewed these Databases questions