Please read the instructions carefully i need main.cpp sequence.cpp and sequence
ID: 3816104 • Letter: P
Question
Please read the instructions carefully
i need main.cpp sequence.cpp and sequence.h
You need to use template classes to:
1. Do the assignment 3 with single connected linked list and make the value semantics safe to use.
2. Create iterator for your sequence class (You may need to modify the node class).
3. Do the assignment 3 with the doubly connected linked list. Do you see any advantage of doing that, write your answer as a comment on top of your main.cpp file.
Assignment 3 (Due on April 2) Create a sequence data structure using dynamic arrays. Sequence is similar to the bag but the order is important in it (1, 2, 3 is different than 2, 1,3) Implement the erase first and erase last which respectively remove the first and the last occurrence of a value Implement the erase occurrence which removes the occurrence of a value (e.x. Erase the second occurrence of the value 4) Implement the erase from function which accepts INDEX of an element and erases the element f Implement the insert, count and size functions that respectively insert to the end of the sequence, count number of occurrence of a value and returns number of elements in the sequence Implement the insert first function that insert an element as the first index Implement the insert at function which insert some element at a specific index. If you have 3 values then you may have 4 places to insert at have 1, 2, 3 and you want to insert 4 in the sequence, then you may insert at index 0, 1, 2 3 and the result will be 4,1,2,3 or for example if you 1,4,2,3 or 1,2,4,3 or 1,2,3,4 respectively Implement the operator that connects two sequences and create the third one, Overload the operator such that it connects a sequence to a number and return the result. Implement the as a member function that it will add a sequence to this sequence and the overloaded version which adds a number to this sequence Implement the operator this time try to return a sequence& instead of a sequence This is the most accurate version of the operator) Implement the operator which checks the equality of two sequences Overload ostream (e.x. cout s and istream (e.x. cin s)operator so you may print a sequence or insert a sequence by console. In the main try to create a loop with 1,000,000,000 iterations. Try to create an instance of a sequence class in the loop and add one element to that. Run your program and check the memory usage. Add a destructor for to your sequence class and try the test again. What happens to the memory? Don't forget namespace and and macro-guardExplanation / Answer
sequence.h
====
/*
#endif /* LLIST_H_ */
sequence.cpp
====
};
RjExceptions.cpp
====
/* }
* Llist.h * * Created on: 13-Apr-2017 * Author: Rj */ #ifndef LLIST_H_ #define LLIST_H_ #include <iostream> #include "RjExceptions.cpp" template <typename T> struct Node { Node<T> *next; T val; }; /** * Creates a singly linked list with a **head** and a **last**. **head** will be * used to store the address of the first node ever created and the latest will * be stored in the last. */ template <typename T> class Llist { // This will store the first node's address. Node<T> *head; // This is going to hold the latest node's address. Node<T> *last; public: // CONSTRUCTOR Llist(); // Adds a val to list. void add(T val); // Add object at given index. void add(int indx, T val); // Add to start. void add_front(T val); // Iterate. void iterate(); // Reverse iterate. void iterate_reverse(); // Get the element at index. T at(int indx); // Add object at given index. void update(int indx, T val); // Delete void remove(int indx); void pop_front(); void pop_back(); };#endif /* LLIST_H_ */
sequence.cpp
====
/* * Llist.cpp * * Created on: 13-Apr-2017 * Author: Rj */ #include "sequence.h" /** * Constructor function * This function Initializes head and last to NULL. */ template <typename T> Llist<T>::Llist() : head{ NULL }, last{ NULL } {}; /** * Adds a val to the list. * @param { T } val - val of the object to add. */ template <typename T> void Llist<T>::add(T val) { // If it is the first node // Just normal initializing does not produce new objects every time the method is // called. Node<T> *nd1 = new Node<T>; nd1->next = NULL; if (head == NULL) { head = nd1; } else { last->next = nd1; } nd1->val = val; last = nd1; }; /** * @Overload * Add given object at a given index. * @param { int } indx - Index to append at. * @param { T } val - Object to append. */ template <typename T> void Llist<T>::add(int indx, T val) { Node<T> *temp1{ head }; Node<T> *temp2{ NULL }; int i{ 0 }; do { if (i == indx && i != 0) { Node<T> *nd1 = new Node<T>; // Copy Previous one's. nd1->next = temp1; nd1->val = val; temp2->next = nd1; } ++i; temp2 = temp1; temp1 = temp1->next; } while(temp1 != NULL); } /** * Iterates over the list from start to end and prints to the screen. */ template <typename T> void Llist<T>::iterate() { if (head == NULL) { list_un_inited err; throw err; } // Iterate through the list until you hit a NULL. Node<T> *temp{ head }; do { std::cout << temp->val << std::endl; temp = temp->next; } while(temp != NULL); }; /** * Returns the object at the given index. * @param { int } indx - Index of the object to get. */ template <typename T> T Llist<T>::at(int indx) { Node<T> *temp{ head }; int i{ 0 }; do { if (i == indx) { return temp->val; } ++i; temp = temp->next; } while(temp != NULL); out_of_bounds_index err; throw err; } /** * Adds an object to the start of an array. * @param { T } val - Object to add. */ template <typename T> void Llist<T>::add_front(T val) { // Create a new node. Node<T> *nd1 = new Node<T>; // Set its prev to NULL. nd1->next = head; nd1->val = val; // Change others head = nd1; }; /** * Adds an object to the start of an array. * @param { int } indx - Index of object to change. * @param { T } val - Object to change index to. */ template <typename T> void Llist<T>::update(int indx, T val) { Node<T> *temp{ head }; int i{ 0 }; do { if (i == indx) { temp->val = val; } ++i; temp = temp->next; } while(temp != NULL); }; /** * Deletes an object at given index * @param { int } indx - Index of object to remove. */ template <typename T> void Llist<T>::remove(int indx) { Node<T> *temp1{ head }; Node<T> *temp2{ NULL }; int i{ 0 }; do { if (i != 0 && i == indx) { temp2->next = temp1->next; } ++i; temp2 = temp1; temp1 = temp1->next; } while(temp1 != NULL); temp2->next = NULL; }; /** * Deletes the first element of the array. */ template <typename T> void Llist<T>::pop_front() { head = head->next; }; /** * Deletes the last element of an array. */ template <typename T> void Llist<T>::pop_back() { Node<T> *temp1{ head }; Node<T> *temp2{ NULL }; do { temp2 = temp1; temp1 = temp1->next; } while(temp1->next != NULL); temp2->next = NULL;};
RjExceptions.cpp
====
/* }
* RjExceptions.cpp * * Created on: 13-Apr-2017 * Author: Rj */ #include <exception> /** * Array index goes out of bounds! Then throw this error. */ class out_of_bounds_index : public std::exception { public: const char* what() const throw() { return "The index you provided was out of bounds!"; } }; /** * Use when methods are called on empty lists. */ class list_un_inited : public std::exception { public: const char* what() const throw() { return "The index you provided was out of bounds!"; }; SinglyLinkedListDriver.cpp==== #include "Llist.cpp" int main() { Llist<char> mylist1{}; mylist1.add('a'); mylist1.add('c'); mylist1.add('d'); mylist1.iterate(); std::cout << "====" << std::endl; mylist1.add(1, 'b'); mylist1.add_front('o'); mylist1.update(1, 'o'); mylist1.iterate(); std::cout << "====" << std::endl; mylist1.pop_front(); mylist1.pop_back(); mylist1.iterate(); return 0; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.