1)
#include "AnyList.h"
// 1. insertBack -> ๋ฆฌ์คํธ์ ์๋ ๋ชฉ๋ก์ ๋งจ๋์ val์ ๋ฃ๋ ํจ์
// 2. new node ๋ฅผ ๋ง๋ค์ด์ ๊ทธ ์์ parameter ์ ์ ์ฅ๋ ๊ฐ์ ์ ์ฅ -> Node* newNode = new Node(val);
// 3. insertBack() ์ฌ์ฉ๋ฒ !!
void AnyList::insertBack(int val)
{
Node* newNode = new Node(val, nullptr);
if(first == nullptr)
{
first = newNode;
}
else
{
Node* current = first;
for(int i = 1; i < count ;++i)
{
current = current -> getNext();
}
current ->setNext(newNode);
}
count++;
}
<insertBack ์ฌ์ฉ๋ฒ>
- Node* newNode = new Node(newData, nullptr) <- ๊ธฐ๋ณธ ํํ !
- ์ฐ์ ๋ฆฌ์คํธ๊ฐ ๋น์ด์๋์ง, ์๋๋ฉด ๋ญ๊ฐ ์๋์ง ํ์ธ ํด์ผ๋จ
- ๋ฆฌ์คํธ๊ฐ ๋น์ด์๋ค -> first๋ ์๋ก์ด ๋ ธ๋๊ฐ ๋ค์ด๊ฐ๋ค
- ๋ฆฌ์คํธ๊ฐ ์๋น์ด์๋ค -> iteratorํด๋ก ๋ฆฌ์คํธ๋ฅผ ์ ๋ถ ๋๋ ค์ ๋งจ๋(back)์ผ๋ก ๋ค์ด๊ฐ๊ฒ ํ๋ค
- ๋ง์ง๋ง์ count++; ์์ง์๊ธฐ
2)
#include "AnyList.h"
int AnyList::getNumOfElements() const
{
return count;
}
-> const ๋ฅผ ์ธ์ ์ธ์ ๋ฃ์ด์ผ๋๋์ง, ๋ฌ๋ผ์ง๋ ์ ์ด ๋ญ์ง ์์๋์ !
3)
- while๋ฌธ(๋ง์ฝ์ element์ ์ฐพ์ผ๋ฉด) ์ผ๋ก true ์ด๋ฉด return ํ๊ณ ๋จธ ์ด๋ฐ์์ผ๋ก
- parameter์ int, return ๊ฐ์ boolean.
- ์ผ๋จ ๋ชจ๋ elem์ ๋ค ๋์๋ด์ผ๋๊ธฐ ๋๋ฌธ์ ๋ฐ๋ณต๋ฌธ ์จ์ผํจ
- while(int elem : ์ ์ฒด? ) ๊ทธ๋ฆฌ๊ณ ๊ทธ ๋ฐ์ if else ์ผ๋ก bool ๊ฐ ์ง์ ( count๋งํผ ๋ฐ๋ณต๋ ์์ )
- *** ์ด๊ฑด node ์ด๊ธฐ ๋๋ฌธ์ "-> getData" ์ด๋ฐ๊ฑฐ ๋ค์จ์ผ๋!!!!!! " ***
bool AnyList::search(int elem) const
{
if(count == 0)
{
cerr << "The list is empty." << endl;
return false;
}
else
{
Node* current = first;
while(current != nullptr)
{
if(current -> getData() == elem)
{
return true;
}
else
{
current = current -> getNext();
}
}
return false;
}
}
4)
void AnyList::replaceData(int oldKey, int newKey)
{
bool found = false; // to stop the loop when key is found
Node* current = first;
while (current != nullptr && !found)
{
if (current->getData() == oldKey)
found = true;
else
current = current->getNext();
}
if (current == nullptr) // key was not found
cout << "Element is not in the list." << endl;
else
current->setData(newKey);
}
// ๋ด๊ฐ ์๊ฐํ๊ฑฐ ๊น์ง๋
void AnyList::replaceData(int oldData, int newData)
{
Node* current = first;
while(current != nullptr)
{
if(current ->getData() == oldData)
{
current -> setData(newData);
}
else
{
cout << "Element is not in the list." << endl;
current = current -> getNext();
}
}
} // ์ด๊ฑฐ์์ด ๊ทผ๋ฐ,, ์ด๋ฌ๋ฉด element is not in the list ๊ฐ ๊ฐ๋ง์ด ๋์ด ์๋๋ฉด์ ์๋์ฌ๋๋ง๋ค ๊ณ์ ๋์ผ๋๋๊น ...
... ํ๋๊ทธ ์ฐ๋๊ฑฐ ๋๋ฌด ์ด๋ ต๋ค ใ .... ์ฐ์ตํ์ !
5)
void AnyList::deleteElem(int elem)
{
if (count == 0) // OR: (first == nullptr)
{
// Review the Course Style Guide to learn when to use cerr/cout.
cerr << "The list is empty." << endl;
}
else
{
// Search if the elem is in the list.
// When found, delete the node.
// if it is the first node
if (first->getData() == elem)
{
Node* current = first;
first = first->getNext();
delete current;
current = nullptr;
--count;
}
// else (could be any of the other nodes)
else
{
bool found = false;
Node* trailCurrent = first;
Node* current = first->getNext();
// OR: Node* current = trailCurrent->getNext();
while (!found && current != nullptr)
{
if (current->getData() == elem)
{
trailCurrent->setNext(current->getNext());
delete current;
current = nullptr;
--count;
found = true;
}
else
{
trailCurrent = current;
current = current->getNext();
}
}
if (!found) // Do NOT write (found == false) !!!
// Redundant statement.
cout << "Element " << elem
<< " is not in the list.\n";
}
}
}
์ด๊ฒ๋ ์ด๋ ต๋ค .. ๋ค์๋ค์ ใ
'เซฎโหถแต แต แตหถโแโก > coding' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] Sequence Containers(6)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.02 |
---|---|
[C++] DLL(4)!! ์ฐ์ตํ๊ธฐ ^ใ ^ (0) | 2024.04.02 |
[C++] STL Pair, Nested Containers ์ค์ฒฉ ์ปจํ ์ด๋ (0) | 2024.03.31 |
[C++] STL ์ฐ๊ด์ปจํ ์ด๋ STL Associative Container (sets and multisets, maps and multimaps) (3) | 2024.03.29 |
[C++] STL ์์ฐจ ์ปจํ ์ด๋ STL Sequence Container (Vector, List) (0) | 2024.03.29 |