Singly Linked List Flashcards

1
Q

SinglyLinkedList isEmpty

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

SinglyLinkedList AddNodeFront

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

SinglyLinkedList AddNodeBack

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

SinglyLinkedList AddNodeAtPosition

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

        Node<T>* newNode = new Node<T>(value);
        Node<T>* current = head;
        int index = 0;

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

        if (current == nullptr)
        {
            addNodeBack(value);
        }
        else
        {
            newNode->next = current->next;
            current->next = newNode;

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

SinglyLinkedList DeleteNodeFront

A
void deleteNodeFront()
    {
        if (isEmpty())
        {
            cout << "List is empty." << endl;
            return;
        }

        node *temp = head;
        head = head->next;
        delete temp;

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

SinglyLinkedList DeleteNodeBack

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

        if (head == tail)
        {
            delete head;
            head = tail = nullptr;
        }
        else
        {
            node *current = head;
            while (current->next != tail)
            {
                current = current->next;
            }
            delete tail;
            tail = current;
            tail->next = nullptr;
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

SinglyLinkedList DeleteNodeAtPosition

A

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

    Node<T>* current = head;
    Node<T>* previous = nullptr;
    int index = 0;

    while (current != nullptr && index < position)
    {
        previous = current;
        current = current->next;
        index++;
    }

    if (current == nullptr)
    {
        cout << "Position is out of range" << endl;
        return;
    }

    previous->next = current->next;
    if (current == tail)
        tail = previous;

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

SinglyLinkedList search

A
    void search(T value)
    {
        Node<T>* current = head;
        int index = 0;
        bool found = false;

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

        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

SinglyLinkedList display

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

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

SinglyLinkedList reverse

A
    void reverse()
    {
        if (isEmpty() || head == tail)
            return;

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

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

        head = prev;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

SinglyLinkedList clear

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