profile-img
์ง€๋ฐ์ด์˜ ํ‹ฐ์Šคํ† ๋ฆฌ
images/slide-image

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 ์‚ฌ์šฉ๋ฒ•>

  1. Node* newNode = new Node(newData, nullptr) <- ๊ธฐ๋ณธ ํ˜•ํƒœ ! 
  2. ์šฐ์„  ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€, ์•„๋‹ˆ๋ฉด ๋ญ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ํ•ด์•ผ๋จ 
    1. ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ๋‹ค -> first๋Š” ์ƒˆ๋กœ์šด ๋…ธ๋“œ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค
    2. ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•ˆ๋น„์–ด์žˆ๋‹ค -> iteratorํˆด๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋ถ€ ๋Œ๋ ค์„œ ๋งจ๋(back)์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ํ•œ๋‹ค 
  3. ๋งˆ์ง€๋ง‰์— count++; ์žŠ์ง€์•Š๊ธฐ 

2)

#include "AnyList.h"

int AnyList::getNumOfElements() const
{
	return count; 
}

 

-> const ๋ฅผ ์–ธ์ œ์–ธ์ œ ๋„ฃ์–ด์•ผ๋˜๋Š”์ง€, ๋‹ฌ๋ผ์ง€๋Š” ์ ์ด ๋ญ”์ง€ ์•Œ์•„๋‘์ž ! 

 

3)

 

  1. while๋ฌธ(๋งŒ์•ฝ์— element์„ ์ฐพ์œผ๋ฉด) ์œผ๋กœ true ์ด๋ฉด return ํ•˜๊ณ  ๋จธ ์ด๋Ÿฐ์‹์œผ๋กœ 
  2. parameter์€ int, return ๊ฐ’์„ boolean. 
  3. ์ผ๋‹จ ๋ชจ๋“  elem์„ ๋‹ค ๋Œ์•„๋ด์•ผ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ ์จ์•ผํ•จ
    1. while(int elem : ์ „์ฒด? ) ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋ฐ‘์— if else ์œผ๋กœ bool ๊ฐ’ ์ง€์ • ( count๋งŒํผ ๋ฐ˜๋ณต๋  ์˜ˆ์ •) 
    2. *** ์ด๊ฑด 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' Related Articles +