I participate in programming in my first C ++ class, and recently we looked at linked lists, and we were given the task of implementing a simple one. I encoded everything, but my function is pop_back() , which fusses to return a pointer to a Node that needs to be removed in Main() . There is no Node removal in a valid function. So my question is:
Could you help me point me in the right direction for my pop_back() function? Also, if you notice anything else that I'm doing wrong, let me know.
In addition, this linked list is designed to work with strings. In this case, the list of products, so that one line for the number of elements (1,2) and one line for the type of element. (Milk, eggs, etc.)
Below I have included an implementation of the List and Node classes, so you can get an idea of ββwhat I have done so far.
Node.cpp
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; }
List.cpp
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->SetNext(pFirst); first = item; numNodes++; } } void List::push_back(Node * item) { if (last == NULL) { first = item; last = item; } else { last->SetNext(item); last = item; numNodes++; } } Node* List::pop_front() { Node* temp = first; first = first->GetNext(); if (first == NULL) { temp = first->GetNext(); first = p; } if (first == NULL) { last = NULL; } if (numNodes > 0) { numNodes--; } return temp; } Node* List::pop_back()
source share