本文整理汇总了C++中ListElement::setNext方法的典型用法代码示例。如果您正苦于以下问题:C++ ListElement::setNext方法的具体用法?C++ ListElement::setNext怎么用?C++ ListElement::setNext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ListElement
的用法示例。
在下文中一共展示了ListElement::setNext方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
bool LinkedList<T>::insertAtPos(T data, int pos)
{
ListElement<T> *nodeToInsert = new ListElement<T>(data);
if (nodeToInsert == nullptr) return false;
// If list is empty and position is not the
// begining of the list return false.
if (isEmpty() && pos != 0) {
return false;
}
ListElement<T> *curr = _head;
// iterate until we are at pos - 1
while (curr != nullptr && pos != 1) {
curr = curr->getNext();
pos--;
}
// insert the ListElement<T>
if (pos == 1) {
nodeToInsert->setNext(curr->getNext());
curr->setNext(nodeToInsert);
return true;
}
return false;
}
示例2: addAfterN
void List::addAfterN(int nPos, int inValue)
{
if (mLength >= nPos) {
ListElement *additional = new ListElement(inValue);
ListElement *current = mHead;
for (int i = 1; i < nPos; i++) {
current = current->getNext();
}
additional->setNext(current->getNext());
current->setNext(additional);
mLength++;
}
}
示例3: append
void List::append(int inValue)
{
ListElement *updateHead = new ListElement(inValue);
if (mLength == 0) {
mHead = updateHead;
mTail = updateHead;
}
else {
updateHead->setNext(mHead);
mHead = updateHead;
}
mLength++;
}
示例4: remove
void List::remove(string str) throw (string)
{
if (size >= 2)
{
if (head->getStr() == str)
{
ListElement *toDel = head;
head = head->getNext();
head->setPrev(NULL);
toDel->setNext(NULL);
delete toDel;
size--;
return;
}
ListElement *temp = head->getNext();
while(temp->getNext() != NULL)
{
if (temp->getStr() == str)
{
temp->getNext()->setPrev(temp->getPrev());
temp->getPrev()->setNext(temp->getNext());
delete temp;
size--;
return;
}
temp = temp->getNext();
}
if (tail->getStr() == str)
{
ListElement *toDel = tail;
tail = tail->getPrev();
tail->setNext(NULL);
size--;
delete toDel;
return;
}
throw string("No such word!");
}
else if (size == 1)
{
if (head->getStr() == str)
{
delete head;
size--;
return;
}
throw string("No such word!");
}
else
throw string("List is Empty!");
}
示例5: revert
// Before revert List : x, y, z, w
// After revert List : w, z, y, x
void List::revert()
{
// "Write" List in the end
ListElement *lastModified = mTail;
ListElement *step;
// for all elements befor tail
for (int i = 1; i < mLength; i++) {
step = mHead;
// Gets pre lastModified value
while (step->getNext() != lastModified) {
step = step->getNext();
}
lastModified->setNext(step);
lastModified = step;
}
mHead = mTail;
mTail = lastModified;
mTail->setNext(nullptr);
}
示例6: revertNew
void List::revertNew()
{
ListElement *previous = new ListElement(mHead->getValue());
// Sets new Tail
mTail = previous;
ListElement *step = mHead->getNext();
while (step) {
ListElement *clone = new ListElement(step->getValue());
clone->setNext(previous);
previous = clone;
step = step->getNext();
}
// Head of reverted List now in previous
while (mHead) {
step = mHead;
mHead = mHead->getNext();
delete step;
}
mHead = previous;
}