Circular Singly Linked List Flashcards

1
Q

CircularSinglyLinkedList isEmpty

A
bool isEmpty() const
    {
        return tail == nullptr;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

CircularSinglyLinkedList addNodeFront

A
void addNodeFront(T value)
    {
        Node<T>* newNode = new Node<T>(value);
        if (isEmpty())
        {
            tail = newNode;
            tail->next = tail;
        }
        else
        {
            newNode->next = tail->next;
            tail->next = newNode;
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

CircularSinglyLinkedList addNodeBack

A
void addNodeBack(T value)
    {
        Node<T>* newNode = new Node<T>(value);
        if (isEmpty())
        {
            tail = newNode;
            tail->next = tail;
        }
        else
        {
            newNode->next = tail->next;
            tail->next = newNode;
            tail = newNode;
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

CircularSinglyLinkedList addNodeAtPosition

A
void addNodeAtPosition(T value, int position)
    {
        if (position <= 0 || isEmpty())
        {
            addNodeFront(value);
            return;
        }

        Node<T>* newNode = new Node<T>(value);
        Node<T>* current = tail->next;

        int count = 0;
        while (count < position - 1 && current != tail)
        {
            current = current->next;
            count++;
        }

        newNode->next = current->next;
        current->next = newNode;

        if (current == tail)
        {
            tail = newNode;
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

CircularSinglyLinkedList deleteNodeFront

A
void deleteNodeFront()
    {
        if (isEmpty())
        {
            cout << "Sarasas yra tuscias. Negalima pasalinti elemento." << endl;
            return;
        }

        Node<T>* temp = tail->next;
        if (tail->next == tail)
        {
            tail = nullptr;
        }
        else
        {
            tail->next = temp->next;
        }

        delete temp;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

CircularSinglyLinkedList deleteNodeBack

A
void deleteNodeBack()
    {
        if (isEmpty())
        {
            cout << "Sarasas yra tuscias. Negalima pasalinti elemento." << endl;
            return;
        }

        Node<T>* current = tail->next;
        if (current == tail)
        {
            delete tail;
            tail = nullptr;
        }
        else
        {
            while (current->next != tail)
            {
                current = current->next;
            }

            current->next = tail->next;
            delete tail;
            tail = current;
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

CircularSinglyLinkedList deleteNodeAtPosition

A
void deleteNodeAtPosition(int position)
    {
        if (isEmpty())
        {
            cout << "Sarasas yra tuscias. Negalima pasalinti elemento." << endl;
            return;
        }

        if (position <= 0)
        {
            deleteNodeFront();
            return;
        }

        Node<T>* current = tail->next;
        int count = 0;

        while (count < position - 1 && current->next != tail->next)
        {
            current = current->next;
            count++;
        }

        Node<T>* temp = current->next;
        if (temp == tail)
        {
            tail = current;
        }
        current->next = temp->next;

        delete temp;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

CircularSinglyLinkedList search

A
void search(T value)
    {
        if (isEmpty())
        {
            cout << "Sarasas yra tuscias." << endl;
            return;
        }

        Node<T>* current = tail->next;
        int index = 0;
        bool found = false;

        do
        {
            if (current->data == value)
            {
                cout << "Reiksme " << value << " rasta pozicijoje: " << index << endl;
                found = true;
            }
            current = current->next;
            index++;
        } while (current != tail->next);

        if (!found)
        {
            cout << "Reiksme " << value << " nerasta sarase." << endl;
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

CircularSinglyLinkedList display

A
void display() const
    {
        if (isEmpty())
        {
            cout << "Sarasas yra tuscias." << endl;
            return;
        }

        Node<T>* current = tail->next;
        do
        {
            cout << current->data << " ";
            current = current->next;
        } while (current != tail->next);
        cout << endl;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

CircularSinglyLinkedList reverse

A
void reverse()
    {
        if (isEmpty() || tail->next == tail)
            return;

        Node<T>* prev = tail;
        Node<T>* current = tail->next;
        Node<T>* next = nullptr;

        do
        {
            next = current->next;
            current->next = prev;
            prev = current;
            current = next;
        } while (current != tail->next);

        tail->next = prev;
        tail = current;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

CircularSinglyLinkedList clear

A
void clear()
    {
        while (!isEmpty())
        {
            deleteNodeFront();
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly