Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "exception.cpp"
- #include <iostream>
- #include <fstream>
- using namespace std;
- template <class X>
- class DoubleLinkedList
- {
- struct Element
- {
- X inf;
- Element *next;
- Element *prev;
- Element (X x): inf(x), next(0), prev(0)
- {
- }
- };
- Element *head;
- Element *tail;
- int size;
- Element *Find( int index)
- {
- if(index < 1||index > size)
- return NULL;
- else
- {
- Element *cur = head;
- for(int i = 1; i < index; i++)
- cur = cur->next;
- return cur;
- }
- }
- public:
- DoubleLinkedList():head(0), tail(0), size(0)
- {
- }
- ~DoubleLinkedList()
- {
- while(!Empty())
- Remove(1);
- }
- bool Empty()
- {
- return head == 0;
- }
- int GetLength()
- {
- return size;
- }
- X Get(int index)
- {
- if (index < 1 || index > size)
- {
- throw DoubleListException("ListException: get - double-linked list error");
- }
- else
- {
- Element *r = Find(index);
- X i = r->inf;
- return i;
- }
- }
- void InsertLeft(int index, X data )
- {
- if (index < 1 || index > size+1)
- {
- throw DoubleListException("ListException: insert - double-linked list error");
- }
- else
- {
- Element *newPtr = new Element(data);
- size = GetLength() + 1;
- Element *cur = Find(index)
- if (cur == NULL)
- {
- head = newPtr;
- tail = newPtr;
- }
- else
- {
- newPtr->next = cur;
- newPtr->prev = cur->prev;
- cur->prev = newPtr;
- if(cur == head)
- head = newPtr;
- else newPtr->prev->next = newPtr;
- }
- }
- }
- void InsertRight(int index, X data )
- {
- if ((index < 1 && head != NULL) || (index > size+1))
- {
- throw DoubleListException("ListException: insert - double-linked list error");
- }
- else
- {
- Element *newPtr = new Element(data);
- size = GetLength() + 1;
- Element *cur = Find(index);
- if (cur == NULL)
- {
- head = newPtr;
- tail = newPtr;
- }
- else
- {
- newPtr->next = cur->next;
- newPtr->prev = cur;
- cur->prev = newPtr;
- if(cur == tail)
- tail = newPtr;
- else newPtr->next->prev = newPtr;
- }
- }
- }
- void Remove (int index)
- {
- if (index < 1 || index > size)
- {
- throw DoubleListException("ListException: remove - double-linked list error");
- }
- else
- {
- Element *cur = Find(index);
- --size;
- if (size == 0)
- {
- head = NULL;
- tail = NULL;
- }
- else if (cur == head)
- {
- head = head->next;
- head->prev = NULL;
- }
- else if (cur == tail)
- {
- tail = tail->prev;
- tail->next = NULL;
- }
- else
- {
- cur->prev->next = cur->next;
- cur->next->prev = cur->prev;
- }
- cur->next = NULL;
- cur->prev = NULL;
- delete cur;
- }
- }
- void PrintLeftToRight(ofstream &out)
- {
- for(Element * cur = head; cur != NULL; cur = cur->next)
- out << cur->inf << " ";
- out << endl;
- }
- };
- int main()
- {
- DoubleLinkedList <int> t;
- int i;
- ifstream in ("input.txt");
- ofstream out ("output.txt");
- while (in >> i)
- t.InsertRight(t.GetLength(), i);
- in.close();
- for( int j = 1; j <= t.GetLength(); )
- {
- if(t.Get(j) % 2 == 0)
- t.InsertRight(j, t.Get(j)*2);
- }
- t.PrintLeftToRight(out);
- t.~DoubleLinkedList();
- out.close();
- return 0;
- }
- //срр
- #include <exception>
- #include <string>
- using namespace std;
- class DoubleListException: public exception
- {
- public:
- DoubleListException(const string & message=""): exception(message.c_str())
- {
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement