CircularSinglyLinkedList isEmpty
bool isEmpty() const
{
return tail == nullptr;
}CircularSinglyLinkedList addNodeFront
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;
}
}CircularSinglyLinkedList addNodeBack
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;
}
}CircularSinglyLinkedList addNodeAtPosition
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;
}
}CircularSinglyLinkedList deleteNodeFront
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;
}CircularSinglyLinkedList deleteNodeBack
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;
}
}
CircularSinglyLinkedList deleteNodeAtPosition
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;
}
CircularSinglyLinkedList search
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;
}
}CircularSinglyLinkedList display
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;
}CircularSinglyLinkedList reverse
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;
}
CircularSinglyLinkedList clear
void clear()
{
while (!isEmpty())
{
deleteNodeFront();
}
}