modified: as1/assignment01_159201.cpp

This commit is contained in:
mooophy
2014-12-04 00:17:05 +13:00
parent 8cd29e1bb9
commit 22527a36ae

View File

@@ -2,6 +2,8 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <stack>
namespace ads{ namespace ads{
@@ -115,6 +117,11 @@ public:
return do_print_data(); return do_print_data();
} }
~SparseMatrix()
{
deallocate_data();
}
private: private:
Node* head_; Node* head_;
Node* tail_; Node* tail_;
@@ -163,7 +170,8 @@ private:
std::ifstream& read_and_init_body(std::ifstream& ifs) std::ifstream& read_and_init_body(std::ifstream& ifs)
{ {
std::string line; std::string line;
for(IndexType r=0; r != rows_; ++r){ for(IndexType r=0; r != rows_; ++r)
{
std::getline(ifs,line); std::getline(ifs,line);
std::stringstream stream{line}; std::stringstream stream{line};
for(IndexType c=0; c != cols_; ++c) for(IndexType c=0; c != cols_; ++c)
@@ -211,6 +219,22 @@ private:
std::cout << curr->value << " "; std::cout << curr->value << " ";
return std::cout; return std::cout;
} }
/**
* @abstraction I
*
* @complx O(n)
*/
void deallocate_data()
{
if(not empty())
{
std::stack<Node*> stk;
for(auto poi = head_; poi; poi = poi->next) stk.push(poi);
for( ; not stk.empty(); stk.pop()) delete stk.top();
head_ = tail_ = nullptr;
}
}
}; };
@@ -223,7 +247,8 @@ std::ostream& operator<<(std::ostream& os, SparseMatrix<T> const& rhs)
{ {
for(IndexType c = 0; c != rhs.cols_; ++c) for(IndexType c = 0; c != rhs.cols_; ++c)
{ {
if(!curr or is_precedent(Node{r, c, 0, nullptr}, *curr)){ if(!curr or is_precedent(Node{r, c, 0, nullptr}, *curr))
{
os << 0 << " "; os << 0 << " ";
} }
else else