C++

[Data Structure] Reviewing Up wrong C++ codes

곽가누 2024. 10. 4. 00:12

2024.10.04

1. Array cannot have member function. 

template<class ItemType>
class QueueType
{
private:
    ItemType *     data; // dynamic array implementation
    int         front; // the index of the front element
    int         rear; // the index of the rear element
    int         maxQueue; // the MAX size of the queue
    
public:
    QueueType(int maxQue);
    void        enqueue(ItemType value);
    ItemType    dequeue( );
    
    void        rotateFirstItem();
    
    
    void        clear( );
    bool        isFull( ) const;
    bool        isEmpty( ) const;
    void        printQueue( ) const;
};

template<class ItemType>
void QueueType<ItemType>::rotateFirstItem()
{
    /* Implement the function here (Exercise 3-1) */
    // This "rotates" the first item of the queue and returns NOTHING.
    // "rotate" means to move the first item to the last place.
    // For example:
    //   Que: [ 1    2    3    4    5     ]
    //       rotateFirstItem()
    //   Que: [ 2    3    4    5    1     ]
    
    // HINT: You can call "other member functions" of QueType here.
    
    if(isEmpty()){
        return;
    }
    ItemType temp = data[front + 1];
    dequeue();
    for (int i = 0; i < maxQueue; i++) {
        data[i+1] = data[i];

        if (data[i+1] == NULL) {
            break;
        }
        
    }
    
    
    
    
    
}

In this code, I cannot write code like

data.dequeue();

 

2. When you treat the index of queue (even though, it was not used in my hw)

ItemType temp = data[(front+1) % maxQueue];

you have to treat with " % maxQueue "

 

3. When you pop or dequeue, check whether data structure is empty.

if (!tempQueue.isEmpty()) {
    location current = tempQueue.dequeue();

 

4. in Queue, you cannot just circulate circular Queue by using 

for (int i = 0; i < maxQueue; i++)

because this code doesn't consider front and rear. 

it can access rear + 1 , or front -1 ..

 

You have to use like this

int index = front;
do {
    index = (index + 1) % maxQueue;
    if (smallest.priority > data[index].priority && data[index].description != "") {
        smallest = data[index];
    }


} while (index != rear);

Declare "index" and use do while grammar.  

 

5. while using modulo calculation in C++ by % operator, negative number doesn't become positive number. 

rear = (rear -1 ) % maxQueue;

When rear is 0, rear can be -1. So, you have to use code like this :

rear = (rear - 1 + maxQueue) % maxQueue;

 

2024.10.17

6. While declaring pointer, you have to assign the type of what the pointer is pointing

NodeType * tempPtr = topPtr;

9. If you want to append new node, you should write like this:

NodeType<ItemType>* tempNode = new NodeType<ItemType>;

Or, if you just want pointer to manage, you should write like this:

NodeType<ItemType>* tempNode ;

'C++' 카테고리의 다른 글

[Data Structure] Reviewing Up wrong C++ codes 2  (0) 2024.11.30
[Data Structure] 중간고사 정리  (2) 2024.10.20
[C++] 기말고사 공부 로그  (0) 2024.05.20
[C++] Vector와 Array  (0) 2024.04.21
[C++]중간고사 공부 로그  (0) 2024.03.15