Skip to content

Instantly share code, notes, and snippets.

@Glavak
Last active October 23, 2017 09:08
Show Gist options
  • Save Glavak/c438d855687c09fc257d313196bb5969 to your computer and use it in GitHub Desktop.
Save Glavak/c438d855687c09fc257d313196bb5969 to your computer and use it in GitHub Desktop.
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
template<typename T>
class list
{
private:
struct list_item
{
public:
T value;
list_item * next;
};
list_item * first;
public:
list()
{
first = nullptr;
}
list(list & other)
{
cout << "Copy ctor forbidden!" << endl;
first = nullptr;
}
list & operator=(list & other)
{
cout << "Assingment operator forbidden!" << endl;
return *this;
}
void AddToBegin(int item) //добавление в начало
{
list_item * new_item = new list_item();
new_item->value = item;
new_item->next = first;
first = new_item;
}
void RemoveFromBegin() //удаление первого
{
if (first != nullptr)
{
list_item * old = first;
first = first->next;
delete old;
}
}
~list()
{
while (first != nullptr)
{
RemoveFromBegin();
}
}
void Insert(T element, int place) //вставка в список
{
if (place == 0)
{
AddToBegin(element);
return;
}
list_item * item = first;
int i = 1;
while (i != place)
{
item = item->next;
i++;
}
list_item * new_item = new list_item();
new_item->value = element;
new_item->next = item->next;
item->next = new_item;
}
void AddToEnd(T element) //добавление в конец
{
if (first == nullptr)
{
AddToBegin(element);
return;
}
list_item * item = first;
while (item->next != nullptr)
{
item = item->next;
}
list_item * new_item = new list_item();
new_item->value = element;
new_item->next = nullptr;
item->next = new_item;
}
void RemoveFromEnd() //удаление последнего
{
if (first->next == nullptr)
{
delete first;
first = nullptr;
return;
}
list_item * item = first;
while (item->next->next != nullptr)
{
item = item->next;
}
delete item->next;
item->next = nullptr;
}
void Show()
{
list_item * temp = first;
while (temp != NULL)
{
cout << temp->value << ' ';
temp = temp->next;
}
cout << endl;
}
void ShowBetween(T start, T end)
{
list_item *temp = first;
while (temp->value != start)
{
temp = temp->next;
}
while (temp->value != end)
{
cout << temp->value << ' ';
temp = temp->next;
}
cout << temp->value << endl;
}
bool IsExist(T element)
{
list_item *temp = first;
while (temp != nullptr)
{
if(temp->value == element)
{
cout << "found: " << temp->value << endl;
return true;
}
temp = temp->next;
}
return false;
}
};
int main()
{
list<char> primeNumbers;
primeNumbers.AddToBegin('a');
primeNumbers.AddToBegin('d');
primeNumbers.AddToBegin('b');
primeNumbers.AddToBegin('c');
primeNumbers.AddToBegin('e');
primeNumbers.AddToBegin('h');
primeNumbers.RemoveFromBegin();
primeNumbers.Show();
// e c b d a
primeNumbers.Insert('h', 1);
primeNumbers.Show();
// e h c b d a
primeNumbers.AddToEnd('g');
primeNumbers.Show();
// e h c b d a g
primeNumbers.RemoveFromEnd();
primeNumbers.Show();
// e h c b d a
primeNumbers.ShowBetween('c', 'd');
// c b d
primeNumbers.IsExist('c');
// found: c
primeNumbers = primeNumbers;
list<char> copy = primeNumbers;
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment