Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Linked List Sort Method
    text
    copied!<p>Hi I am making a custom linked list (for fun!) and want to implement a sort method that sorts the stored data using the stored types > and &lt; operators (will require the type to have these operators overloaded)</p> <p>What is the best way to do this? I guess the first thing one might jump to would be to compare the current node with the the node to its "right" see if one is greater than the other. Then keep iterating through the list until you don't swap any more nodes. However I am thinking there is probably a more efficient way to do this. Here is my code so far:</p> <pre><code>#ifndef LIST_HPP #define LIST_HPP #include &lt;string&gt; template &lt;typename T&gt; class Node; template &lt;typename T&gt; class List { public: List(); ~List(); bool isEmpty() const; T&amp; front(); T&amp; back(); inline unsigned int size() const; void pushFront(const T&amp; s); void removeElement(const T&amp; s); void insert(const T&amp; s); T popFront(); const T* each(); private: unsigned int size_; Node&lt;T&gt;* head_; Node&lt;T&gt;* current_; }; template &lt;typename T&gt; List&lt;T&gt;::List() : size_(0), head_(0), current_(0) { } template &lt;typename T&gt; List&lt;T&gt;::~List() { while (isEmpty()) { popFront(); } head_ = 0; } template &lt;typename T&gt; bool List&lt;T&gt;::isEmpty() const { return (!size_ || !head_); } template &lt;typename T&gt; T&amp; List&lt;T&gt;::front() { if (isEmpty()) throw std::string("List Empty"); return head_-&gt;data(); } template &lt;typename T&gt; T&amp; List&lt;T&gt;::back() { Node&lt;T&gt;* cur = head_; while (true) { if (cur-&gt;next() == 0) return cur-&gt;data(); cur = cur-&gt;next(); } } template &lt;typename T&gt; void List&lt;T&gt;::pushFront(const T&amp; s) { current_ = head_ = new Node&lt;T&gt;(s, head_); size_++; } template &lt;typename T&gt; T List&lt;T&gt;::popFront() { if (isEmpty()) { throw std::string("List Empty"); } Node&lt;T&gt;* current_head = head_; current_ = head_ = head_-&gt;next(); T temp = current_head-&gt;data(); delete current_head; size_--; return temp; } template &lt;typename T&gt; const T* List&lt;T&gt;::each() { if (isEmpty()) return 0; if (current_ == 0) { current_ = head_; return 0; } const T* ret_str = &amp;current_-&gt;data(); current_ = current_-&gt;next(); return ret_str; } template &lt;typename T&gt; void List&lt;T&gt;::removeElement(const T&amp; s) { if (isEmpty()) { throw std::string("List Empty"); return; } Node&lt;T&gt;* prev = 0, *rnode; rnode = head_; while (rnode != 0) { if (rnode-&gt;data() == s) break; prev = rnode; rnode = rnode-&gt;next(); } if (prev == 0) { Node&lt;T&gt;* temp = head_; current_ = head_ = head_-&gt;next(); delete temp; } else { prev-&gt;next(rnode-&gt;next()); delete rnode; } size_--; } template &lt;typename T&gt; void List&lt;T&gt;::insert(const T&amp; s) { if (isEmpty()) { pushFront(s); } else if (size_ == 1) { if (s &lt;= head_-&gt;data()) pushFront(s); else head_-&gt;next(new Node&lt;T&gt;(s, 0)); size_++; } else { Node&lt;T&gt;* current, *prev = 0, *new_node = 0; while (current != 0) { if (s &lt;= current-&gt;data()) break; prev = current; current = current-&gt;next(); } if (prev == 0) pushFront(s); else prev-&gt;next(new Node&lt;T&gt;(s, current)); size_++; } } template &lt;typename T&gt; unsigned int List&lt;T&gt;::size() const {return size_;} template &lt;typename T&gt; class Node { public: Node(const T&amp; data, Node&lt;T&gt;* next = 0); T&amp; data(); Node* next(); void next(Node&lt;T&gt;* n); private: T data_; Node* next_; }; template &lt;typename T&gt; Node&lt;T&gt;::Node(const T&amp; data, Node* next): data_(data), next_(next) { } template &lt;typename T&gt; void Node&lt;T&gt;::next(Node&lt;T&gt;* n) { next_ = n; } template &lt;typename T&gt; Node&lt;T&gt;* Node&lt;T&gt;::next() { return next_; } template &lt;typename T&gt; T&amp; Node&lt;T&gt;::data() { return data_; } #endif //LIST_HPP </code></pre> <p>Surely there is a way to sort with only one iteration through the list? Or maybe not.</p> <p>Thanks for any help in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload