Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- ifstream in("input.txt");
- ofstream out("output.txt");
- template <class Item> class DoublList {
- struct Node {
- Item data;
- Node* prev;
- Node* next;
- Node(Item x) : data(x), next(0), prev(0) {}
- };
- Node* head;
- Node* tail;
- int size;
- Node* Find(int index) {
- Node* cur = head;
- for (int i = 0; i < index; i++) {
- cur = cur->next;
- }
- return cur;
- }
- public:
- DoublList() {
- head = NULL;
- tail = NULL;
- }
- bool empty() {
- return head == 0;
- }
- int GetLength() {
- return size;
- }
- Node Get(int index) {
- Node* r = Find(index);
- Item i = r->data;
- return i;
- }
- void insertAtHead(string value) {
- Node* newNode = new Node();
- newNode->data = value;
- newNode->prev = NULL;
- newNode->next = head;
- if (head != NULL) {
- head->prev = newNode;
- }
- else {
- tail = newNode;
- }
- head = newNode;
- size++;
- }
- void insertAtTail(string value) {
- Node* newNode = new Node(value);
- if (tail != NULL) {
- tail->next = newNode;
- }
- else {
- head = newNode;
- }
- tail = newNode;
- size++;
- }
- void deleteAtHead() {
- if (head != NULL) {
- Node* temp = head;
- if (head == tail) {
- head = NULL;
- tail = NULL;
- }
- else {
- head = head->next;
- head->prev = NULL;
- }
- delete temp;
- }
- }
- void deleteAtTail() {
- if (tail != NULL) {
- Node* temp = tail;
- if (head == tail) {
- head = NULL;
- tail = NULL;
- }
- else {
- tail = tail->prev;
- tail->next = NULL;
- }
- delete temp;
- }
- }
- void printList() {
- Node* current = head;
- while (current != NULL) {
- out << current->data << " ";
- current = current->next;
- }
- cout << endl;
- }
- };
- int main()
- {
- DoublList <string> k, m;
- string value;
- while (in >> value) {
- k.insertAtTail(value);
- if (value.length() == 1) {
- k.insertAtTail(value);
- }
- }
- in.close();
- k.printList();
- return 0;
- }
- aarrarar
- r
- t
- base
- baza
- h
- pp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement