minh_tran_782

Lab1SLL

Jul 15th, 2022 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.12 KB | None | 0 0
  1. void Polynomial::insertTerm(const Term& term) {
  2.     this->terms->add(term);
  3.     return;
  4. }
  5. void Polynomial::insertTerm(double coeff, int exp) {
  6.     if (coeff == 0) return;
  7.     Term myTerm(coeff,exp);
  8.             SLinkedList<Term>::Iterator it;
  9.   for (it = this->terms->begin(); it != this->terms->end(); it++) {
  10.             if ((*it).exp == exp) {
  11.                 (*it).coeff += coeff;
  12.                 if ((*it).coeff == 0) this->terms->removeItem(*it);
  13.                 return;
  14.             }
  15.         }
  16.     int size = terms->size();
  17.     if (size == 0) {
  18.             terms->add(myTerm);
  19.         return;
  20.     }
  21.     if (size == 1){
  22.         auto temp = terms->get(0);  
  23.         if (temp.exp < exp) terms->add(0,myTerm);
  24.         else terms->add(myTerm);
  25.         return;
  26.     }
  27.     int cnt = 0;
  28.       for (it = this->terms->begin(); it != this->terms->end(); it++) {
  29.           auto val = *it;
  30.           if (val.exp < exp)
  31.           {
  32.             terms->add(cnt,myTerm);
  33.             return;
  34.           }
  35.           ++cnt;
  36.         }
  37.         terms->add(myTerm);
  38.     return;
  39. }
  40. template <class T>
  41. SLinkedList<T>::Iterator::Iterator(SLinkedList<T>* pList, bool begin)
  42. {
  43.     this->pList = pList;
  44.     if (begin == true){
  45.         if (pList != NULL) {
  46.             current = pList->head;
  47.             index = 0;
  48.         }
  49.         else {
  50.             current = NULL;
  51.             index = -1;
  52.         }
  53.         return;
  54.     }
  55.     current = NULL;
  56.     if (pList != NULL) index = pList->size();
  57.     else index = 0;
  58.     return;
  59.     /*
  60.         Constructor of iterator
  61.         * Set pList to pList
  62.         * begin = true:
  63.         * * Set current (index = 0) to pList's head if pList is not NULL
  64.         * * Otherwise set to NULL (index = -1)
  65.         * begin = false:
  66.         * * Always set current to NULL
  67.         * * Set index to pList's size if pList is not NULL, otherwise 0
  68.     */
  69. }
  70.  
  71. template <class T>
  72. typename SLinkedList<T>::Iterator& SLinkedList<T>::Iterator::operator=(const Iterator& iterator)
  73. {
  74.     /*
  75.         Assignment operator
  76.         * Set this current, index, pList to iterator corresponding elements.
  77.     */
  78.     this->pList = iterator.pList;
  79.     this->current = iterator.current;
  80.     this->index = iterator.index;
  81.     return *this;
  82. }
  83.  
  84. template <class T>
  85. void SLinkedList<T>::Iterator::remove()
  86. {
  87.     if (current == NULL) throw std::out_of_range("Segmentation fault!");
  88.     SLinkedList<T>::Iterator it;
  89.     if (current->data == *(pList->begin()))
  90.     {
  91.         pList->removeAt(0);  
  92.         current = NULL;
  93.         index = -1;
  94.         return;
  95.     }
  96.     /*
  97.         Remove a node which is pointed by current
  98.         * After remove current points to the previous node of this position (or node with index - 1)
  99.         * If remove at front, current points to previous "node" of head (current = NULL, index = -1)
  100.         * Exception: throw std::out_of_range("Segmentation fault!") if remove when current is NULL
  101.     */
  102. }
  103.  
  104. template <class T>
  105. void SLinkedList<T>::Iterator::set(const T& e)
  106. {
  107.     /*
  108.         Set the new value for current node
  109.         * Exception: throw std::out_of_range("Segmentation fault!") if current is NULL
  110.     */
  111.     if (current == NULL)throw std::out_of_range("Segmentation fault!");
  112.     current->data = e;
  113.     return;
  114. }
  115.  
  116. template <class T>
  117. T& SLinkedList<T>::Iterator::operator*()
  118. {
  119.     /*
  120.         Get data stored in current node
  121.         * Exception: throw std::out_of_range("Segmentation fault!") if current is NULL
  122.     */
  123.      if (current == NULL)throw std::out_of_range("Segmentation fault!");
  124.      return current->data;
  125. }
  126.  
  127. template <class T>
  128. bool SLinkedList<T>::Iterator::operator!=(const Iterator& iterator)
  129. {
  130.     /*
  131.         Operator not equals
  132.         * Returns true if two iterators points the same node and index
  133.     */
  134.     if (iterator.index != this->index) return false;
  135.     if (iterator.current != this->current) return false;
  136.     return true;
  137. }
  138. // Prefix ++ overload
  139. template <class T>
  140. typename SLinkedList<T>::Iterator& SLinkedList<T>::Iterator::operator++()
  141. {
  142.     /*
  143.         Prefix ++ overload
  144.         * Set current to the next node
  145.         * If iterator corresponds to the previous "node" of head, set it to head
  146.         * Exception: throw std::out_of_range("Segmentation fault!") if iterator corresponds to the end
  147.     */
  148.     if (current == NULL)  throw std::out_of_range("Segmentation fault!");
  149.     else current = current->next;
  150.     return *this;
  151. }
  152. // Postfix ++ overload
  153. template <class T>
  154. typename SLinkedList<T>::Iterator SLinkedList<T>::Iterator::operator++(int)
  155. {
  156.     /*
  157.         Postfix ++ overload
  158.         * Set current to the next node
  159.         * If iterator corresponds to the previous "node" of head, set it to head
  160.         * Exception: throw std::out_of_range("Segmentation fault!") if iterator corresponds to the end
  161.     */
  162.                 Iterator iterator = *this;
  163.                 ++*this;
  164.                 return iterator;
  165. }
  166. template <class T>
  167. void SLinkedList<T>::add(const T& e) {
  168.     /* Insert an element into the end of the list. */
  169.     if (this->count == 0){
  170.         Node* newNode = new Node(e,NULL);
  171.         this->head = newNode;
  172.         this->tail = newNode;
  173.         this->head->next = this->tail;
  174.         ++this->count;
  175.         return;
  176.     }
  177.     if (this->count == 1){
  178.         Node* newNode = new Node(e,NULL);
  179.         this->tail = newNode;
  180.         tail->next = NULL;
  181.         this->head->next  = this->tail;
  182.         ++this->count;
  183.         return;
  184.     }
  185.             Node* newNode = new Node(e,NULL);
  186.             tail->next = newNode;
  187.             tail = newNode;
  188.             newNode->next = NULL;
  189.             ++this->count;
  190.             return;
  191. }
  192. template<class T>
  193. void SLinkedList<T>::add(int index, const T& e) {
  194.     /* Insert an element into the list at given index. */
  195.     if (index == 0){
  196.                 Node* newNode = new Node(e,NULL);
  197.                 newNode->next = head;
  198.                 head = newNode;
  199.                 if (this->count == 0) {
  200.                     tail = newNode;
  201.                     head->next = tail;
  202.                 }
  203.                 ++this->count;
  204.                 return;
  205.     }
  206.     if (index == this->count){
  207.         Node* newNode = new Node(e,NULL);
  208.         tail->next = newNode;
  209.         tail = newNode;
  210.         if (this->count == 1) this->head->next = tail;
  211.                         ++this->count;
  212.         return;
  213.     }
  214.     int idx = 0;
  215.     Node* curr = this->head;
  216.     Node* prev = NULL;
  217.     while (curr != NULL){
  218.         if (idx == index) break;
  219.         prev = curr;
  220.         curr = curr->next;
  221.         ++idx;
  222.     }
  223.     Node* newNode = new Node (e,NULL);
  224.     prev->next = newNode;
  225.     newNode->next = curr;
  226.     ++this->count;
  227.     return;
  228. }
  229.  
  230. template<class T>
  231. int SLinkedList<T>::size() {
  232.     /* Return the length (size) of list */
  233.     return this->count;
  234. }
  235. template<class T>
  236. T SLinkedList<T>::get(int index) {
  237.     int cnt = 0;
  238.     Node* list = this->head;
  239.     while (list != NULL){
  240.         if (cnt == index) break;
  241.         list = list->next;
  242.         ++cnt;
  243.     }
  244.     if (list == NULL) return -1;
  245.     return list->data;
  246. }
  247.  
  248. template <class T>
  249. void SLinkedList<T>::set(int index, const T& e) {
  250.     /* Assign new value for element at given index in the list */
  251.     int cnt = 0;
  252.     Node* list = this->head;
  253.     while (list != NULL){
  254.            if (cnt == index) break;
  255.         list  = list->next;
  256.         ++cnt;
  257.     }
  258.     if (list == NULL) return;
  259.     list->data = e;
  260.     return;
  261. }
  262.  
  263. template<class T>
  264. bool SLinkedList<T>::empty() {
  265.     /* Check if the list is empty or not. */
  266.     if (this->count == 0) return true;
  267.     return false;
  268. }
  269.  
  270. template<class T>
  271. int SLinkedList<T>::indexOf(const T& item) {
  272.     /* Return the first index wheter item appears in list, otherwise return -1 */
  273.     Node* list = this->head;
  274.     int cnt = 0;
  275.     while (list != NULL){
  276.         if (list->data == item) return cnt;
  277.         list = list->next;
  278.         ++cnt;
  279.     }
  280.     return -1;
  281. }
  282.  
  283. template<class T>
  284. bool SLinkedList<T>::contains(const T& item) {
  285.     /* Check if item appears in the list */
  286.      Node* list = this->head;
  287.     while (list != NULL){
  288.         if (list->data == item) return true;
  289.         list = list->next;
  290.     }
  291.     return false;
  292. }
  293. template <class T>
  294. T SLinkedList<T>::removeAt(int index)
  295. {
  296.     if (this->head == NULL) return -1;
  297.     if (index > this->count - 1) return -1;
  298.     if (index == 0){
  299.         Node* temp = head;
  300.         head = head->next;
  301.         T temp_data = temp->data;
  302.         delete temp;
  303.         --this->count;
  304.         return temp_data;
  305.     }
  306.     Node* curr = this->head;
  307.     Node*prev = NULL;
  308.     int cnt = 0;
  309.     while (curr != NULL){
  310.         if (cnt == index) break;
  311.         prev = curr;
  312.         curr = curr->next;
  313.         ++cnt;
  314.     }
  315.     if (index == this->count -1 ){
  316.         prev->next = curr->next;
  317.         tail = prev;
  318.         T temp_data = curr->data;
  319.         delete curr;
  320.         --this->count;
  321.         return temp_data;
  322.     }
  323.     prev->next = curr->next;
  324.     T temp_data = curr->data;
  325.     --this->count;
  326.     delete curr;
  327.     return temp_data;
  328. }
  329.  
  330. template <class T>
  331. bool SLinkedList<T>::removeItem(const T& item)
  332. {
  333.     /* Remove the first apperance of item in list and return true, otherwise return false */
  334.     if (this->head == NULL) return  false;
  335.     if (item == head->data){
  336.         Node* temp = head;
  337.         head = head->next;
  338.         //int temp_data = temp->data;
  339.         delete temp;
  340.         --this->count;
  341.         return true;
  342.     }
  343.     Node* curr = this->head;
  344.     Node*prev = NULL;
  345.     while (curr != NULL){
  346.         if (item == curr->data) break;
  347.         prev = curr;
  348.         curr = curr->next;
  349.     }
  350.     if (curr == NULL) return false;
  351.     if (curr->next == NULL){
  352.         prev->next = curr->next;
  353.         this->tail = prev;
  354.         delete curr;
  355.         --this->count;
  356.         return true;
  357.     }
  358.     prev->next = curr->next;
  359.     --this->count;
  360.     delete curr;
  361.     return true;
  362. }
  363.  
  364. template<class T>
  365. void SLinkedList<T>::clear(){
  366.     while (head != NULL){
  367.         Node* temp = head;
  368.         head = head->next;
  369.         delete temp;
  370.     }
  371.     this->count = 0;
  372.     this->head = this->tail = NULL;
  373. }
  374. vector<int> topologicalSorting(vector<vector<int>> graph)
  375. {
  376.     // Create a vector to store
  377.     // indegrees of all
  378.     // vertices. Initialize all
  379.     // indegrees as 0.
  380.     int size = graph.size();
  381.     vector<int> in_degree(size, 0);
  382.  
  383.     // Traverse adjacency lists
  384.     // to fill indegrees of
  385.     // vertices.  This step
  386.     // takes O(V+E) time
  387.     for (int u = 0; u < size; u++) {
  388.         vector<int>::iterator itr;
  389.         for (itr = graph[u].begin();
  390.              itr != graph[u].end(); itr++)
  391.             in_degree[*itr]++;
  392.     }
  393.  
  394.     // Create an queue and enqueue
  395.     // all vertices with indegree 0
  396.     queue<int> q;
  397.     for (int i = 0; i < size; i++)
  398.         if (in_degree[i] == 0)
  399.             q.push(i);
  400.  
  401.     // Initialize count of visited vertices
  402.     int cnt = 0;
  403.  
  404.     // Create a vector to store
  405.     // result (A topological
  406.     // ordering of the vertices)
  407.     vector<int> top_order;
  408.  
  409.     // One by one dequeue vertices
  410.     // from queue and enqueue
  411.     // adjacents if indegree of
  412.     // adjacent becomes 0
  413.     while (!q.empty()) {
  414.         // Extract front of queue
  415.         // (or perform dequeue)
  416.         // and add it to topological order
  417.         int u = q.front();
  418.         q.pop();
  419.         top_order.push_back(u);
  420.  
  421.         // Iterate through all its
  422.         // neighbouring nodes
  423.         // of dequeued node u and
  424.         // decrease their in-degree
  425.         // by 1
  426.         vector<int>::iterator itr;
  427.         for (itr = graph[u].begin();
  428.              itr != graph[u].end(); itr++)
  429.  
  430.             // If in-degree becomes zero,
  431.             // add it to queue
  432.             if (--in_degree[*itr] == 0)
  433.                 q.push(*itr);
  434.  
  435.         cnt++;
  436.     }
  437.     if (cnt != size){
  438.         vector<int> res;
  439.         res.push_back(-1);
  440.         return res;
  441.     }
  442.     return top_order;
  443. }
  444.  
  445.  
Add Comment
Please, Sign In to add comment