#include <iostream>
#include <strings.h>

using namespace std;

struct node {
  node* next;
  node* prev;
  string line;
};

class linkedlist {
  protected:
    node* first;
    node* last;
    node* curr;

  public:
    linkedlist();
    
    void addLine();
    string shift();
    void unshift(string value);
    string pop();
    void push(string value);

    int offEnd();
    void goFirst();
    void goLast();
    void linkedlist::goNext() { if(!offEnd()) curr = curr->next; }
    void linkedlist::goPrev() { if(!offEnd()) curr = curr->prev; }
    string getCurr();
};

linkedlist::linkedlist () {
  first = NULL;
  last = NULL;
  curr = NULL;
}  

void linkedlist::addLine() {
  if(last != NULL) {
    last->next = new node;
    (last->next)->prev = last;
    last = last->next;
    last->next = NULL;
  }
  else {
    first = new node;
    last = first;
    first->next = NULL;
    first->prev = NULL;
  }
}

string linkedlist::shift() {
  string value;
  node* tempnode = first;
  if(first == NULL) return 0;
  value = first->line;
  first = first->next;
  if(first != NULL) {
    first->prev = NULL;
  }
  else {
    last = NULL;
  }
  if(curr == tempnode) curr = NULL;
  free(tempnode);
  return value;
}

void linkedlist::unshift(string value) {
  node* tempNode = new node;
  tempNode->line = value;
  tempNode->next = first;
  tempNode->prev = NULL;
  if(first != NULL) {
    first->prev = tempNode;
  }
  else {
    last = tempNode;
  }
  first = tempNode;
}

string linkedlist::pop() {
  string value;
  node* tempnode = last;
  if(last == NULL) return 0;
  value = last->line;
  last = last->prev;
  if(last != NULL) {
    last->next = NULL;
  }
  else {
    first = NULL;
  }
  if(curr == tempnode) curr = NULL;
  free(tempnode);
  return value;
}

void linkedlist::push(string value) {
  node* tempNode = new node;
  tempNode->line = value;
  tempNode->prev = last;
  tempNode->next = NULL;
  if(last != NULL) {
    last->next = tempNode;
  }
  else {
    first = NULL;
  }
  last = tempNode;
}

int linkedlist::offEnd() { return curr == NULL; }

void linkedlist::goFirst() { curr = first; }

void linkedlist::goLast() { curr = last; }

string linkedlist::getCurr() {
  if(!offEnd()) return curr->line;
  return 0;
}

int main() {
  linkedlist list;
  string str = "monkeys";
  list.push("hi");
}
