Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Linked List Implementation in C++
    primarykey
    data
    text
    <p>I'm a programming student in my first C++ class, and recently we covered linked lists, and we were given an assignment to implement a simple one. I have coded everything but my <code>pop_back()</code> function, which is supossed to return a pointer to the <code>Node</code> that needs to be deleted in <code>Main()</code>. No <code>Node</code> deletion is to be done in the actual function. So my question is:</p> <p>Would you be willing to help point me in the right direction for my <code>pop_back()</code> function? Also, if you notice anything else that I'm doing wrong, let me know.</p> <p>Also, this linked list is just to work with strings. In this case, a grocery list, so one string for the quantity of the item(1,2), and one string for the item type. (Milk, Eggs, etc.) </p> <p>Below I've included my List &amp; Node class implementations, so you can get an idea of what I've done so far.</p> <p><strong>Node.cpp</strong></p> <pre><code>Node::Node(void) { descrip = " "; quantity = " "; previous = NULL; next = NULL; } Node::Node(string q, string d) { descrip = d; quantity = q; previous = NULL; next = NULL; } Node* Node::GetNext() { return next; } Node* Node::GetPrevious() { return previous; } void Node::SetNext(Node * setter) { next = setter; } void Node::SetPrevious(Node * setter) { previous = setter; } </code></pre> <hr> <p><strong>List.cpp</strong></p> <pre><code>List::List(void) { first = NULL; last = NULL; numNodes = 0; } Node* List::GetFirst() { return first; } Node* List::GetLast() { return last; } void List::SetFirst(Node* setter) { first = setter; } void List::SetLast(Node* setter) { last = setter; } int List::GetNumNodes() { return numNodes; } void List::push_front(Node* item) { if (first == NULL) { first = item; last = item; } else { Node* pFirst = first; item-&gt;SetNext(pFirst); first = item; numNodes++; } } void List::push_back(Node * item) { if (last == NULL) { first = item; last = item; } else { last-&gt;SetNext(item); last = item; numNodes++; } } Node* List::pop_front() { Node* temp = first; first = first-&gt;GetNext(); if (first == NULL) { temp = first-&gt;GetNext(); first = p; } if (first == NULL) { last = NULL; } if (numNodes &gt; 0) { numNodes--; } return temp; } Node* List::pop_back() // this whole function may be wrong, this is just my attempt at it { Node* temp; temp = first; while((temp-&gt;GetNext()) != NULL) // im stuck here } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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