1. front()
#include "DoublyList.h"
int DoublyList::front() const
{
return first -> getData();
}
2. back()
#include "DoublyList.h"
int DoublyList::back() const
{
return last -> getData();
}
3.insertBack()
#include "DoublyList.h"
void DoublyList::insertBack(int newData)
{
//Node* newNode = new Node(newData, nullptr);
if(first == nullptr)
{
first = new DLLNode(newData, nullptr, nullptr);
last = first;
}
else
{
last -> setNext(new DLLNode(newData, last, nullptr));
last = last -> getNext();
}
++count;
}
4)moveToList()
void DoublyList::moveToList(DoublyList& toList)
{
// Assume parameter object is empty.
// Do NOT assume the calling object is empty.
// Do nothing if the calling object is empty.
if (count != 0)
{
toList.first = first;
toList.last = last;
toList.count = count;
// Detach the calling object from the nodes that
// now belong to the parameter object.
first = nullptr;
last = nullptr;
count = 0;
// The calling object still exists, but it is empty
// as it was when it was created before inserting
// any elements.
}
}
5) deleteElem()
void DoublyList::deleteElement(int elementToDelete)
{
// Case 1: List is empty
if (first == nullptr) // OR: if (count == 0)
{
// Use cerr, rather than cout (why?)
cerr << "The list is empty.\n";
}
else
{
// Set a Boolean value to keep track of
// whether the item is found or not.
bool found = false;
// There is only one node in the list.
if (first == last) // OR: if (count == 1)
{
// Node to be deleted is the first node.
if (first->getData() == elementToDelete)
{
delete first;
first = last = nullptr;
found = true;
}
// No need to make a case if the only node
// does not store the data to delete.
}
else // If there is more than one node...
{
// Check if the first node or the last node needs
// to be deleted. Why should you check this
// separately? Because these are special case,
// where pointer first and last must be updated.
if (first->getData() == elementToDelete)
{
first = first->getNext();
delete first->getPrev();
first->setPrev(nullptr);
found = true;
}
else if (last->getData() == elementToDelete)
{
last = last->getPrev();
delete last->getNext();
last->setNext(nullptr);
found = true;
}
else // Node to delete might be somewhere in between
// the first node and the last node.
{
// Create a pointer to traverse the list.
// Start pointing at the second node, because
// you already know that it is not the first node,
// and stop before checking the last node for the
// same reason.
DLLNode* current = first->getNext();
while (!found && current != last)
// Don't let the WHILE loop continue if
// the element is found!
{
if (current->getData() == elementToDelete)
{
// connect previous and next node
current->getPrev()->setNext(current->getNext());
current->getNext()->setPrev(current->getPrev());
// NOTE: It would be more efficient to create
// a pointer pointing to the previous node and
// another pointer pointing to the next node,
// because we would not have to call too many
// functions, but this is good practice for the exam.
delete current;
current = nullptr;
// Don't leave a dangling pointer!
found = true;
}
else
current = current->getNext();
}
}
}
if (!found)
cout << "Element is not in the list." << endl;
else
--count; // This statement is needed only once.
}
}
'เซฎโหถแต แต แตหถโแโก > coding' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] Associative Containers(7)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.04 |
---|---|
[C++] Sequence Containers(6)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.02 |
[C++] SLL(3)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.01 |
[C++] STL Pair, Nested Containers ์ค์ฒฉ ์ปจํ ์ด๋ (0) | 2024.03.31 |
[C++] STL ์ฐ๊ด์ปจํ ์ด๋ STL Associative Container (sets and multisets, maps and multimaps) (3) | 2024.03.29 |