pop_back and push_back

Have a final project on Linked List. I know that there are some errors in the push_back and pop_back since because of them .cpp driver does not work, but I don't know what is wrong.

Commented pop_back function looks more efficient, but it also has some error.

Help to fix, please) As well, maybe somebody know how to reference to CarPart class through List class in printList (driver.cpp)? What is specified in the conditions for loop? Thank you!

Here is all files of this project: https://onedrive.live.com/redir?resid=A1457FA0EE18C466!1010&authkey=!AMD9L35Fs-nN9S4&ithint=folder%2ccpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "List.h"
 
 
// A push_back function that takes a Node* as a parameter and adds the node pointed 
// to by the pointer to the end of the list.
 
 
void List::push_back(Node* n)
 
{
        if (first == nullptr)
        {
            first = n;
            last = first;
            //  head = new Node<T>(n);
        }
    else
    {
        Node *cursor = first;
        while (cursor->getNext() != NULL)
        {
            cursor = cursor->getNext();
        }
        cursor = NULL;
    
    }
}
    //  A push_front function that takes a Node* as a parameter and adds the node pointed to by the pointer to the front of the list.
 
 
void List::push_front(Node* n)
{
    if (first == nullptr)
    {
        first = n;
        last = first;
        number++;
    }
 
    else
    {
        n->setNext(first);
        first = n;
        number++;
    }
}
 
    //  A pop_back function that removes the last node from the list, 
    //  and returns a pointer to this node.Important, do not delete the node in this function.
    //  Simply unlink it from the list.
 
//Node* List::pop_back()
//{ 
//  Node* temp = first;
//  if (first == nullptr)
//  {
//      return nullptr;
//  }
//
//  else if (first == last)
//  {
//      first = nullptr;
//      last = nullptr;
//
//  }
//
//  else
//  {
//      while (!(temp.getNext = last))
//      {
//          temp = temp.getNext();
//      }
//      last.setNext(nullptr);
//      temp = temp.getNext();
//      return temp;
//  }
//}
 
Node* List::pop_back()
{
    Node *cursor = first;
        if (first == nullptr)
        {
            return nullptr;
        }
 
    while (cursor->getNext())
    {
        cursor = cursor->getNext();
    }
    delete cursor->getNext();
    return nullptr;
}
//  A pop_front function that removes the first node from the list, and returns a pointer to this node.
 //  Important, do not delete the node in this function.Simply unlink it from the list.
 
 
Node* List::pop_front()
{
    if (first == nullptr)
        return nullptr;
    else 
    {
        Node* temp = first;
        first = first->getNext();
        number--;
        return temp;
    }
 
Topic archived. No new replies allowed.