本文整理汇总了C++中DLNode::setPrevious方法的典型用法代码示例。如果您正苦于以下问题:C++ DLNode::setPrevious方法的具体用法?C++ DLNode::setPrevious怎么用?C++ DLNode::setPrevious使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DLNode
的用法示例。
在下文中一共展示了DLNode::setPrevious方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: insert
//create new DLNode with newContents and insert in ascending (based on newContents) order
void DLList::insert (int newContents)
{
if(head == NULL || newContents < head->getContents())
{
pushFront(newContents);
return;
}
DLNode* temp = head;
while(temp->getNext() != NULL)
{
DLNode* nextTemp = temp->getNext();
if(nextTemp->getContents() > newContents)
{
DLNode* inserted = new DLNode(newContents);
inserted->setPrevious(temp);
inserted->setNext(nextTemp);
temp->setNext(inserted);
nextTemp->setPrevious(inserted);
size++;
return;
}
temp = temp->getNext();
}
pushBack(newContents);
}
示例2: insert
void DLList::insert(int newValue) {
if (head == NULL)
pushFront(newValue);
else if (newValue <= head->getContents())
pushFront(newValue);
else if (newValue >= tail->getContents())
pushBack(newValue);
else {
DLNode* spot = head;
while (spot->getNext() != NULL && newValue > spot->getContents())
spot = spot->getNext();
if (spot->getNext() == NULL && newValue >= spot->getContents())
pushBack(newValue);
else {
DLNode* newNode = new DLNode(newValue);
DLNode* trailer = spot->getPrevious();
trailer->setNext(newNode);
newNode->setNext(spot);
newNode->setPrevious(trailer);
spot->setPrevious(newNode);
size++;
}
}
}
示例3: removeFirst
bool DLList::removeFirst(int target) {
DLNode* toRemove = NULL;
DLNode* nodeMark = head;
DLNode* lagMark = NULL;
for (unsigned int i = 0; i < count; i++){
if (nodeMark->getContents() != target && nodeMark->getNext() != NULL){
lagMark = nodeMark;
nodeMark = nodeMark->getNext();
} else if (nodeMark->getContents() == target){
toRemove = nodeMark;
}
}
if (toRemove == NULL) {
return false;
}
if (lagMark == NULL){
popFront();
} else if (toRemove->getNext() == NULL) {
popBack();
} else {
count--;
nodeMark = nodeMark->getNext();
lagMark->setNext(nodeMark);
nodeMark->setPrevious(lagMark);
delete toRemove;
}
toRemove = NULL;
nodeMark = NULL;
lagMark = NULL;
return true;
}
示例4: pushFront
/**
* creates a new dynamic DLNode with the contents of
* the parameter and attaches as the new head of the list
* @param string contents
*/
void DLList::pushFront(string contents)
{
DLNode* temp = new DLNode(contents);
temp->setNext(head_);
temp->setPrevious(tail_);
head_ = temp;
size_ += 1;
if (tail_ == NULL)
tail_ = head_;
}
示例5: pushBack
void DLList::pushBack(int value) {
if (head == NULL) {
pushFront(value);
} else {
DLNode* newTail = new DLNode(value);
newTail->setPrevious(tail);
tail->setNext(newTail);
tail = newTail;
size++;
}
}
示例6: pushBack
void DLList::pushBack(int newContents) {
if (head == NULL) {
pushFront(newContents);
} else {
DLNode* newTail = new DLNode(newContents);
if (head->getNext() == NULL) {
head->setNext(newTail);
newTail->setPrevious(head);
} else {
DLNode* oldTail = head->getNext();
for (unsigned int i = 1; i < count; i++){
if (oldTail-> getNext() != NULL){
oldTail = oldTail->getNext();
}
}
oldTail->setNext(newTail);
newTail->setPrevious(oldTail);
oldTail = NULL;
}
tail = newTail;
newTail = NULL;
count++;
}
}
示例7: pushBack
//create new DLNode with newContents and attach at tail
void DLList::pushBack (int newContents)
{
DLNode* temp = new DLNode(newContents);
temp->setPrevious(tail);
tail = temp;
size++;
if(tail->getPrevious() == NULL)
{
head = tail;
}
else
{
tail->getPrevious()->setNext(tail);
}
}
示例8: insert
void DLList::insert(int newContents) {
if (head == NULL) {
pushFront(newContents);
} else {
count++;
DLNode* newNode = new DLNode(newContents);
if (head->getContents() > newContents){
DLNode* oldHead = head;
head = newNode;
head->setNext(oldHead);
oldHead->setPrevious(head);
oldHead = NULL;
} else {
if (head->getNext() == NULL) {
head->setNext(newNode);
newNode->setPrevious(head);
tail = newNode;
} else {
DLNode* nodeMark = head->getNext();
DLNode* lagMark = NULL;
for (unsigned int i = 1; i < count; i++) {
if (nodeMark->getContents() < newContents && nodeMark->getNext() != NULL) {
lagMark = nodeMark;
nodeMark = nodeMark->getNext();
}
}
if ((lagMark == NULL) && (nodeMark->getNext() != NULL || nodeMark->getContents() > newContents)) {
head->setNext(newNode);
newNode->setNext(nodeMark);
nodeMark->setPrevious(newNode);
newNode->setPrevious(head);
} else if (nodeMark->getNext() == NULL && newContents > nodeMark->getContents()) {
nodeMark->setNext(newNode);
newNode->setPrevious(nodeMark);
tail = newNode;
} else {
lagMark->setNext(newNode);
newNode->setNext(nodeMark);
nodeMark->setPrevious(newNode);
newNode->setPrevious(lagMark);
}
nodeMark = NULL;
lagMark = NULL;
}
}
newNode = NULL;
}
}
示例9: removeFirst
//remove the first instance of a DLNode containing target; do nothing if target is not found
bool DLList::removeFirst (int target)
{
if(head == NULL)
{
return false;
}
else if(head->getContents()==target)
{
popFront();
return true;
}
else
{
DLNode* temp = head;
while(temp->getNext() != NULL)
{
DLNode* nextTemp = temp->getNext();
if(temp->getContents() == target)
{
temp->getPrevious()->setNext(nextTemp);
nextTemp->setPrevious(temp->getPrevious());
delete temp;
size--;
if(size == 1)
{
head = tail;
}
return true;
}
temp = temp->getNext();
}
if(temp->getContents() == target)
{
popBack();
return true;
}
return false;
}
}
示例10: removeFirst
bool DLList::removeFirst(string target) {
if(head_node == NULL) {
return false;
}
DLNode* temp = head_node;
DLNode* previous = temp;
if(head_node->getContents() == target) {
cout << "Eliminated " << target << endl;
popFront();
return true;
}
if(tail_node->getContents() == target) {
cout << "Eliminated " << target << endl;
popBack();
return true;
}
// There is no reason why this loop would be endless, so there won't be a catch.
// The incoming TARGET string is only called when getStop finishes.
while(temp->getContents() != target) {
// Get next node and continue, and keep previous 1 behind.
previous = temp;
temp = temp->getNext();
}
if(temp->getContents() == target) {
cout << "Eliminated " << target << endl;
// Delete Node
// Setting previous' node to the next node of temp
previous->setNext(temp->getNext());
// Setting temp's next node's PREVIOUS to temp's previous NODE
temp->setPrevious(temp->getPrevious());
delete temp;
temp = NULL;
node_count--;
return true;
} else {
cout << "RemoveFirst returned false entirely (bad?)"<<endl;return false;}
}