In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of November 9th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #05 exercise sheet]
In this problem you should only submit a singlyLinkedList.h file with a self-containing definition of a class SinglyLinkedList<T>
Use as a basis the class SinglyLinkedList<T> (see code | download code) which implements a generic singly linked list similar to what was done on classes, with methods to add and remove an element in the end, return the size, knowing if the list is empty or printing the list.
Add a new method T & get(int pos) to the class. The function should return a reference to the value of the node in position pos (with the positions starting in zero). You may assume that your method will always be called with a valid position, that is, it always happen that 0<pos<length.
You should submit the file singlyLinkedList.h with the method get added to SinglyLinkedList<T> as requested (and without removing any of the other existing methods). You can however create additional methods, if you need them.
Mooshak will use the following code to link to your class, read the inputs, build the lists and call your method, printing its result.
#include <iostream> #include <string> #include "singlyLinkedList.h" // Read n and then n elements to add to the list using addFirst, followed by // Read q and then q queries q_i to call get(q_i) template <typename T> void read() { int n; std::cin >> n; SinglyLinkedList<T> lst; for (int i=0; i<n; i++) { T v; std::cin >> v; lst.addFirst(v); } std::cout << "lst = " << lst.toString() << " | size = " << lst.size() << std::endl; std::cin >> n; for (int i=0; i<n; i++) { int q; std::cin >> q; std::cout << "lst.get(" << q << ") = " << lst.get(q) << std::endl; } } int main() { int n; std::cin >> n; for (int i=0; i<n; i++) { std::string type; std::cin >> type; if (type == "int") read<int>(); else if (type == "char") read<char>(); else if (type == "string") read<std::string>(); } return 0; }
Example Input | Example Output |
3 int 6 10 20 30 40 50 60 6 0 1 2 3 4 5 char 5 E D C B A 4 0 2 1 3 string 4 structures data and algorithms 4 3 0 1 2 |
lst = {60,50,40,30,20,10} | size = 6 lst.get(0) = 60 lst.get(1) = 50 lst.get(2) = 40 lst.get(3) = 30 lst.get(4) = 20 lst.get(5) = 10 lst = {A,B,C,D,E} | size = 5 lst.get(0) = A lst.get(2) = C lst.get(1) = B lst.get(3) = D lst = {algorithms,and,data,structures} | size = 4 lst.get(3) = structures lst.get(0) = algorithms lst.get(1) = and lst.get(2) = data |