Nosacījums: Uzrakstīt funkciju, kas pārvieto saraksta n-to elementu
uz saraksta beigām. Darbība jāveic, pārkabinot saites, nevis
pārrakstot elementu vērtības.
#include <iostream>
using namespace std;
// Klase Elem - saraksta elements
class Elem
{
public:
int num;
Elem *next;
Elem (int n) { num = n; next = NULL; };
};// Klase List - saistiits vienvirziena saraksts
class List
{
protected:
Elem *first, *last;
public:
Elem *current;
public:
List () { first = last = current = NULL; };
void add_element (int n); // pievieno beigaas
void delete_element (); // izmet no saakuma
int is_empty () { return (first == NULL); };
void start () { current = first; };
int end () { return (current == NULL); };
void next(){if (!end())current = current -> next;};
void print();
void liktuzBeigam(int n);
};int main()
{
List l;
int k;
cout << "Ievadi saraksta elementu (0,lai beigtu):\n";
cin >> k;
while (k!=0)
{
l.add_element (k);
cout << "Ievadi saraksta elementu (0, lai beigtu):\n";
cin >> k;
};
l.liktuzBeigam(2);
l.print();
while (!l.is_empty())
{
l.delete_element ();
};};
void List::add_element (int n)
{ // maina current vertibu, lai raadiitu uz jauno elementu
Elem *p = new Elem (n);
if (first == NULL) first = last = p;
else last = last -> next = p;
current =p;
};void List::delete_element ()
{ // saglabaa current veertiibu (nomaina uz saakumu,ja raadiija uz izmetamo)
Elem *p = first;
if(!is_empty())
{ if (current == first) current = first-> next;
first = first -> next;
delete p;
if(is_empty())last = NULL;
}
};
void List:: print()
{
for (start(); !end(); next())
cout << current->num << endl;
cout << endl;
}












