#include #include using namespace std; /* class Sequence { private: int* point; // pointer int Length; public: void newEmpty() { Length = 0; point = new int[Length]; // выделить место в памяти для массива } int getLength() { cout << "Lenght of sequence: "; cout << Length << endl; return Length; } int getIsEmpty() { if (Length == 0) { cout << "Sequence is empty" << endl; return 1; } else { cout << "Sequence isn't empty" << endl; return -1; } } void Append(int item) { int i; int* tmp; tmp = new int[Length]; // выделяем память for (i = 0; i < Length; i++) { tmp[i] = point[i]; } Length = Length + 1; // увеличиваем размер массива на 1 point = new int[Length]; for (i = 0; i < Length-1; i++) { point[i] = tmp[i]; } point[Length-1] = item; delete tmp; } void Prepend(int item) { int i; int* tmp; tmp = new int[Length]; // выделяем память for (i = 0; i < Length; i++) { tmp[i] = point[i]; } Length = Length + 1; // увеличиваем размер массива на 1 point = new int[Length]; point[0] = item; for (i = 1; i < Length; i++) { point[i] = tmp[i-1]; } delete tmp; } void InsertAt(int index, int item) { if ((Length+1 <= index) || (index < 0)) { cout << "This index out of length" << endl; } else { int i; int* tmp; tmp = new int[Length]; // выделяем память for (i = 0; i < Length; i++) { tmp[i] = point[i]; } Length = Length + 1; // увеличиваем размер массива на 1 point = new int[Length]; for (i = 0; i < index; i++) point[i] = tmp[i]; point[index] = item; for (i = index + 1; i < Length; i++) point[i] = tmp[i - 1]; delete tmp; } } void Remove(int item) { int i = 0, index, key = 0; while(i < Length) { if (point[i] == item) { index = i; i = Length; key = 1; } i++; } if (key){ //удаляем int* tmp; tmp = new int[Length]; for (i = 0; i < Length; i++) { tmp[i] = point[i]; } Length = Length - 1; point = new int[Length]; for (i = 0; i < index; i++) { point[i] = tmp[i]; } for (i = index; i < Length; i++) { point[i] = tmp[i + 1]; } } } int GetFirst() { if (Length != 0) { cout << "First element: " << point[0] << endl; return point[0]; } else { cout << "Sequance is empty" << endl; return -1; } } int GetLast() { if (Length != 0) { cout << "Last element: " << point[Length-1] << endl; return point[Length-1]; } else { cout << "Sequance is empty" << endl; return -1; } } int Get(int index) { if ((Length <= index) || (index < 0)) { cout << "Element with index " << index << " didn't find" << endl; return -1; } else { cout << "Element with index " << index << ": "; cout << point[index] << endl; return point[index]; } } Sequence GetSubsequence(int startIndex, int endIndex) { Sequence B; B.newEmpty(); if ((startIndex < 0) || (endIndex < 0) || (endIndex < startIndex) || (endIndex >= Length)) { cout << "Error with index" << endl; } else { for (int i = startIndex; i <= endIndex; i++) { B.Append(point[i]); } } return B; } void OutputSequance() { int i; for (i = 0; i < Length; i++) { cout << point[i] << " "; } cout << endl; } }; */ struct Node { int value; Node* next; Node *prev; //Указатели на адреса следующего и предыдущего элементов списка }; class ListSequence { private: int Length; Node* head; Node* tail; public: void Append(int item) { Node* tmp = new Node; //Выделение памяти под новый элемент структуры tmp->next = NULL; //Указываем, что изначально по следующему адресу пусто tmp->value = item; if (head == NULL) { //если список пустой head = tmp; tail = tmp; tmp->prev = NULL; } else { //список не пустой tmp->prev = tail; //Указываем адрес на предыдущий элемент в соотв. поле tail->next = tmp; //Указываем адрес следующего за хвостом элемента tail = tmp; } } void Show() { Node* tmp = head; while (tmp!=NULL) { //пока не дойдем до конца cout << tmp->value << " "; //Выводим каждое считанное значение на экран tmp = tmp->next; } cout << "\n"; } }; int main() { ListSequence A; A.Append(1); A.Append(2); A.Append(3); A.Show(); /*Sequence A; A.newEmpty(); A.getLength(); cout << endl; A.Append(23); A.getLength(); A.GetFirst(); A.GetLast(); A.Get(0); A.Get(-1); A.Get(1); cout << endl; A.Append(43); A.getLength(); A.Get(0); A.Get(1); A.Get(-1); A.Get(2); cout << endl; A.Prepend(53); A.getLength(); A.GetFirst(); A.GetLast(); A.Get(0); A.Get(1); A.Get(-1); A.Get(3); cout << endl; Sequence A2; A2 = A.GetSubsequence(1, 1); A2.getLength(); A2.GetFirst(); A2.GetLast(); */ return 0; }