C++ 如何在C++;?
我创建了一个LinkedList类,它有一个函数用于删除列表中的第一个元素,还有一个函数用于删除列表中的最后一个元素。第一个很简单,删除元素后,我将其设置为指向下一个元素。效果很好。但是,当我删除最后一个元素时,我必须将它指向列表中的上一个元素,该元素在该点成为最后一个元素。我不知道怎么做。请告知。代码如下:C++ 如何在C++;?,c++,linked-list,C++,Linked List,我创建了一个LinkedList类,它有一个函数用于删除列表中的第一个元素,还有一个函数用于删除列表中的最后一个元素。第一个很简单,删除元素后,我将其设置为指向下一个元素。效果很好。但是,当我删除最后一个元素时,我必须将它指向列表中的上一个元素,该元素在该点成为最后一个元素。我不知道怎么做。请告知。代码如下: void LinkedList::pop_front() { mFront->data = NULL; mFront = mFront->next; }
void LinkedList::pop_front()
{
mFront->data = NULL;
mFront = mFront->next;
}
如何获得一个函数来删除最后一个元素,但将尾部重置为指向新的尾部
void LinkedList::pop_back()
{
mBack->data = NULL;
...
}
class LinkedList
{
public:
// Default Constructor
// Purpose: Initializes an empty list
// Parameters: none
// Returns: none
LinkedList();
// The push_front function
// Purpose: add an item to the front of the list
// Parameters: a int item for the front
// Returns: none
void push_front(int data);
// The push_back function
// Purpose: insert an item into the back of the list
// Parameters: int item to add the the back
// Returns: none
void push_back(int data);
// The pop_front function
// Purpose: delete the item in the front of the list
// Parameters: none
// Returns: none
void pop_front();
// the pop_back function
// Purpose: remove the item at the end of the list
// Parameters: none
// Returns: none
void pop_back();
// The getFirst function
// Purpose: print the first item in the list
// Parameters: none
// Returns: none
void getFirst();
// the GetLast function
// Purpose: return the last item in the list
// Parameters: none
// Returns: none
void getLast();
void printList();
// the clear function
// Purpose: clear the list, free memory
// Parameters: none
// Returns: none
void clear();
// Destructor
// Purpose: clear up memory
// Parameters: none
// Returns: none
~LinkedList();
private:
LinkedList *mFront; // point to the front of our list
LinkedList *mBack; // point to the back of our list
LinkedList *next; // the next node
LinkedList *previous; // the previous node
int data; // our list data manipulator
单链表不提供对最后一个元素的O(1)删除。您必须从头到尾遍历整个列表,才能找到倒数第二个元素
Node* i = mFront;
while ( i->next != mBack ) i = i->next;
mBack = i;
单链表不提供对最后一个元素的O(1)删除。您必须从头到尾遍历整个列表,才能找到倒数第二个元素
Node* i = mFront;
while ( i->next != mBack ) i = i->next;
mBack = i;
如果列表不是双链接的,则必须从第一个元素开始查找最后一个元素:
void LinkedList::pop_back()
{
mBack->data = NULL;
current = mFront;
do{
current = current->next
} while ( current->next );
mBack = current;
}
非常重要-由于数据
似乎是一个指针,您可能遇到内存泄漏。仅设置data=NULL
并不能释放内存,您必须显式删除它:
delete mFront->data;
如果列表不是双链接的,则必须从第一个元素开始查找最后一个元素:
void LinkedList::pop_back()
{
mBack->data = NULL;
current = mFront;
do{
current = current->next
} while ( current->next );
mBack = current;
}
非常重要-由于数据
似乎是一个指针,您可能遇到内存泄漏。仅设置data=NULL
并不能释放内存,您必须显式删除它:
delete mFront->data;
如果要删除O(1)元素,则需要(即所有节点上的
next
和prev
指针)。如果这是作业,则最好添加combiness
标记,以便人们可以尝试给出更多的解释和更少的代码。如果要删除O(1)元素,则需要(即所有节点上的next
和prev
指针)。如果这是家庭作业,最好添加家庭作业
标记,这样人们可以尝试给出更多解释,减少代码。它表示节点未定义。我是初学者,是否需要在那里引用该类的数据成员?或者,创建新指针?我编辑了上面的代码以显示我的类名和数据成员。它表示节点未定义未定义。我是初学者,是否需要引用该类的数据成员?或者,创建一个新指针?我编辑了上面的代码以显示我的类名和数据成员。您的代码也给了我错误。我不确定当前引用的是什么,我更改了上面的代码以表示我的类。您的代码也给了我错误。我不确定是什么t current正在引用,我更改了上面的代码以表示我的类