Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ostream>
- #include <fstream>
- #include <string>
- using namespace std;
- template <typename TElement> class Sequence {
- public:
- virtual void Append(TElement item) = 0;
- virtual void Prepend(TElement item) = 0;
- virtual void InsertAt(int index, TElement item) = 0;
- virtual int getLength() = 0;
- virtual int getIsEmpty() = 0;
- virtual TElement Get(int index) = 0;
- virtual TElement GetFirst() = 0;
- virtual TElement GetLast() = 0;
- virtual void Remove(TElement item) = 0;
- };
- template <typename TElement> struct Node //узел
- {
- TElement value;
- Node<TElement>* next;
- Node<TElement>* prev;//Указатели на адреса следующего и предыдущего элементов списка
- };
- template <typename TElement> class ListSequence: public Sequence<TElement> {
- private:
- int Length;
- Node<TElement>* head;
- Node<TElement>* tail;
- public:
- void Init() {
- head = NULL;
- tail = NULL;
- Length = 0;
- }
- void Append(TElement item) {
- struct Node<TElement>* temp;
- temp = (struct Node<TElement>*)malloc(sizeof(Node<TElement>));
- temp->value = item;
- if (head == NULL) { //если список пустой
- head = temp;
- tail = temp;
- head->prev = NULL;
- tail->next = NULL;
- Length = 1;
- }
- else { //список не пустой
- temp->prev = tail;
- tail->next = temp;
- tail = temp;
- tail->next = NULL;
- Length += 1;
- }
- }
- void Prepend(TElement item) {
- struct Node<TElement>* temp;
- temp = (struct Node<TElement>*)malloc(sizeof(Node<TElement>));
- temp->prev = NULL;
- temp->value = item;
- if (head == NULL) { //если список пустой
- head = temp;
- tail = temp;
- temp->next = NULL;
- Length = 1;
- }
- else { //список не пустой
- temp->next = head;
- head = temp;
- Length += 1;
- }
- }
- void InsertAt(int index, TElement item) {
- if (index == 1) return Prepend(item);
- if (index == Length + 1) return Append(item);
- if (index < 1 || index > Length + 1)
- cout << "This index out of length" << endl;
- else {
- struct Node<TElement>* temp;
- struct Node<TElement>* p;
- temp = (struct Node<TElement>*)malloc(sizeof(Node<TElement>));
- int i;
- p = head;
- cout << p->value << endl;
- for (i = 1; i < index; i++) {
- p = p->next;
- cout << p->value << endl;
- }
- temp->value = item;
- temp->prev = p->prev;
- temp->next = p;
- p=p->prev;
- p->next = temp;
- p = temp->next;
- p->prev = temp;
- Length += 1;
- }
- }
- 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;
- }
- }
- TElement Get(int index) {
- if ((Length <= index) || (index < 0)) {
- cout << "Element with index " << index << " didn't find" << endl;
- return -1;
- }
- else {
- int i;
- Node<TElement>* temp;
- temp = head;
- for (i = 0; i < index; i++) {
- temp = temp->next;
- }
- cout << "Element with index " << index << ": ";
- cout << temp->value << endl;
- return temp->value;
- }
- }
- TElement GetFirst() {
- cout << "First element. ";
- return Get(0);
- }
- TElement GetLast() {
- cout << "Last element. ";
- return Get(Length - 1);
- }
- void Remove(TElement item) {
- int i;
- Node<TElement>* p = head;
- Node<TElement>* p2 = head;
- if (head->value == item) {
- RemoveFirst();
- Length -= 1;
- }
- else {
- for (i = 1; i < Length; i++) {
- if ((p->next->value == item) && (p->next->next != NULL)) {
- Length -= 1;
- p->next = p->next->next;
- p = p->next;
- p->prev = p2;
- break;
- }
- if ((p->next->value == item) && (p->next->next == NULL)) {
- RemoveLast();
- break;
- }
- p = p->next;
- p2 = p2->next;
- }
- }
- }
- void RemoveFirst() {
- if (head == tail) {
- head = NULL;
- tail = NULL;
- }
- else {
- Node<TElement>* q = head;
- head = q->next;
- q->next->prev = head;
- }
- }
- void RemoveLast() {
- Node<TElement>* l = tail;
- tail = l->prev;
- l->prev->next = tail;
- tail->next = NULL;
- }
- ListSequence GetSubsequence(int startIndex, int endIndex) {
- ListSequence B;
- B.Init();
- Node<TElement>* p = head;
- if ((startIndex < 0) || (endIndex < 0) || (endIndex < startIndex) || (endIndex >= Length)) {
- cout << "Error with index" << endl;
- }
- else {
- for (int i = 0; i < startIndex; i++) {
- p = p->next;
- }
- for (int j = startIndex; j <= endIndex; j++) {
- B.Append(p->value);
- p = p->next;
- }
- }
- return B;
- }
- void Show() {
- Node<TElement>* temp = head;
- while (temp != NULL) {
- cout << temp->value << " ";
- temp = temp->next;
- }
- cout << endl;
- }
- };
- template <typename TElement> class ArraySequence: public Sequence<TElement> {
- private:
- TElement* point; // указатель на массив
- int Length; // размер массива
- public:
- void Init() {
- Length = 0; // по умолчанию размер массива = 10 элементов
- point = new TElement[Length]; // выделить место в памяти для массива
- }
- void Append(TElement item) {
- int i;
- TElement* tmp;
- tmp = new TElement[Length]; // выделяем память
- for (i = 0; i < Length; i++) {
- tmp[i] = point[i];
- }
- Length = Length + 1; // увеличиваем размер массива на 1
- point = new TElement[Length];
- for (i = 0; i < Length - 1; i++) {
- point[i] = tmp[i];
- }
- point[Length - 1] = item;
- delete tmp;
- }
- void Prepend(TElement item) {
- int i;
- TElement* tmp;
- tmp = new TElement[Length]; // выделяем память
- for (i = 0; i < Length; i++) {
- tmp[i] = point[i];
- }
- Length = Length + 1; // увеличиваем размер массива на 1
- point = new TElement[Length];
- point[0] = item;
- for (i = 1; i < Length; i++) {
- point[i] = tmp[i - 1];
- }
- delete tmp;
- }
- void InsertAt(int index, TElement item) {
- if ((Length + 1 <= index) || (index < 0)) {
- cout << "This index out of length" << endl;
- }
- else {
- int i;
- TElement* tmp;
- tmp = new TElement[Length]; // выделяем память
- for (i = 0; i < Length; i++) {
- tmp[i] = point[i];
- }
- Length = Length + 1; // увеличиваем размер массива на 1
- point = new TElement[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;
- }
- }
- 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;
- }
- }
- TElement 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];
- }
- }
- TElement GetFirst() {
- if (Length != 0) {
- cout << "First element: " << point[0] << endl;
- return point[0];
- }
- else {
- cout << "Sequance is empty" << endl;
- return -1;
- }
- }
- TElement GetLast() {
- if (Length != 0) {
- cout << "Last element: " << point[Length - 1] << endl;
- return point[Length - 1];
- }
- else {
- cout << "Sequance is empty" << endl;
- return -1;
- }
- }
- void Remove(TElement item) {
- int i = 0, index, key = 0;
- while (i < Length) {
- if (point[i] == item) {
- index = i;
- i = Length;
- key = 1;
- }
- i++;
- }
- if (key) { //удаляем
- TElement* tmp;
- tmp = new TElement[Length];
- for (i = 0; i < Length; i++) {
- tmp[i] = point[i];
- }
- Length = Length - 1;
- point = new TElement[Length];
- for (i = 0; i < index; i++) {
- point[i] = tmp[i];
- }
- for (i = index; i < Length; i++) {
- point[i] = tmp[i + 1];
- }
- }
- }
- ArraySequence GetSubsequence(int startIndex, int endIndex) {
- ArraySequence B;
- B.Init();
- 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 Show() {
- int i;
- for (i = 0; i < Length; i++) {
- cout << point[i] << " ";
- }
- cout << endl;
- }
- };
- void test1(ArraySequence<int> A, ArraySequence<int> A2){
- A.Init();
- A.getIsEmpty();
- 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.Show();
- A.getLength();
- A.GetFirst();
- A.GetLast();
- 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;
- A2 = A.GetSubsequence(1, 1);
- A2.getLength();
- A2.GetFirst();
- A2.GetLast();
- cout << endl;
- }
- void test2(ListSequence<int> A, ListSequence<int> A2) {
- A.Init();
- A.getIsEmpty();
- 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.Show();
- A.getLength();
- A.GetFirst();
- A.GetLast();
- 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;
- A2 = A.GetSubsequence(1, 1);
- A2.getLength();
- A2.GetFirst();
- A2.GetLast();
- cout << endl;
- }
- int main() {
- int i = 1;
- while (i) {
- cout << "Which Sequence do you want?" << endl;
- cout << "1 - Array Sequance" << endl;
- cout << "2 - List Sequance" << endl;
- cout << "0 - EXIT" << endl;
- int n;
- cin >> n;
- cout << endl;
- ArraySequence<int> A1;
- ArraySequence<int> A2;
- ListSequence<int> B1;
- ListSequence<int> B2;
- switch (n) {
- case 1:
- test1(A1, A2);
- break;
- case 2:
- test2(B1, B2);
- break;
- case 0:
- i = 0;
- break;
- default:
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement