#pragma once
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;

// biglist maintains a list of suspected programming bugs found across a variety of files.
// The list is maintained in (pseudo)alphabetically-increasing sorted order (< on strings),
//     with each insert finding the correct position for the new bug
//     and embedding it at that spot in the list.
class buglist {
   protected:
      struct bug {
         string name; // one word (no spaces) identifier for bug
         string desc; // short text description of nature of bug
         string file; // name of file believed to contain source of bug

         // going from front to back,
         //   next points to the one closer to back, nullptr if none
         //   prev points to the one closer to the front, nullptr if none
         bug* next;
         bug* prev;
      };

      // front and back point to alphabetically first/last bugs in the list
      bug* front;
      bug* back;

      int numBugs; // used to track current number of bugs in the list

      // protected methods are used by some of the public methods
      bug* create(string name, string desc, string file); // allocate, initialize, return bug
      bug* getByName(string name); // find and return pointer to named bug, null if not found
      void prtBug(bug* bptr); // if not null, display using prtBug(n,d,f)
                              // otherwise does nothing
      void insertBack(string name, string desc, string file); // create/insert at back

   public:
      buglist(); // generates an empty buglist
      buglist(const buglist &b); // initializes this list as a duplicate of the provided buglist
     ~buglist(); // delete each bug in the list

      // both prt methods must use prtBug to display the individual bugs' contents
      void prtAll(); // display all bugs in the list, front to back
      void prtFile(string file); // display all bugs (front to back) related to the named file
      void prtBug(string name, string desc, string file);
              // display in format name(file): desc (all one line, no newline/endl at the end)

      // insert, lookup, and remove each return true if successful, false otherwise
      // (insert doesn't allow two bugs with the same name for the same file)
      bool insert(string name, string desc, string file); // create/insert bug (sorted position)
      bool lookup(string name, string &desc, string &file); // find bug, fill in the parameters
      bool remove(string name); // find bug, remove from list, and deallocate
};

