title: Initializing a doubly linked list from array parameters
body: <p>C++ noob reporting in. I'm trying to write a function that will create and initialize a doubly linked list using values that are stored in two different arrays. Here's how my linked list class is set up:</p>
  
  <pre><code>class node {
     public:
     node *prev;
     node *next;
     int key;
     char type;
  };
  </code></pre>
  
  <p>and here's how my dList class (containing various functions to alter my linked list) is set up:</p>
  
  <pre><code>class dList {
  private:
     node *head; // dummy head
     node *tail; // dummy tail
  
  public:
     dList() { // default constructor, creates empty list
       head = tail = NULL;
     }
  
     ~dList() { // deconstructor
       node *ptr = head;
       while (head != NULL) {
         head = head-&gt;next;
         delete ptr;
       }
       tail = NULL;
     }
  
     dList(int arrayNums[], char arrayChars[], int size); // parametrized constructor, initialize list w/ contents of arrays
  
     void addFront(int k, char t); // creates new node at front of list
  
     void addBack(int k, char t); // creates new node at back of list
  
     node *search(int k); // searches list for occurence of int parameter and returns pointer to node containing key
  
     void find(char t); // outputs all keys that have type equal to character parameter, front to back
  
     void moveFront(node* ptr); // moves node pointed to by parameter to front of list
  
     void moveBack(node* ptr); // moves node pointed to by parameter to back of list
  
     void out(int num, char = 'f'); // outputs first int elements of list, starting at front or back depending on char parameter
  
     void sort(); // peforms a quick or mergesort on items; list should be in increasing order based on integer key
  
  };
  </code></pre>
  
  <p>I need help implementing my parametrized constructor. Could someone tell me if the function I have now is written correctly? I think it is, but when I run my program, it runs forever--a clear problem. Here's my function as-is:</p>
  
  <pre><code>dList::dList(int arrayNums[], char arrayChars[], int size) { 
    node *newNode = NULL;
  
    for (int i = 0; i &lt; size; i++) {
  
      if (head == NULL) {
        newNode = new node;
        newNode-&gt;key = arrayNums[i];
        newNode-&gt;type = arrayChars[i];
        newNode-&gt;prev = NULL;
        newNode-&gt;next = NULL;
        head = newNode;
        tail = newNode;
      }
  
      else { // needs work!
        newNode = new node;
        newNode-&gt;key = arrayNums[i];
        newNode-&gt;type = arrayChars[i];
        newNode-&gt;prev = tail;
        tail-&gt;next = newNode;
        tail = newNode;
      }
  
      if (i == (size - 1)) {
        tail-&gt;next = NULL;
      }
    }
  }
  </code></pre>
  
  <p>Thank you very much!</p>
  
  <p>EDIT: here is my main.cpp (code I'm using to test my dList.cpp file)</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  #include "dList.cpp"
  #define SMALL 200
  #define MAX 100000
  #define ROUNDS 100
  int main(){
      int i, x[MAX];
      char ch[MAX];
  
      for(i=0;i&lt;SMALL;i++) {
        x[i] = 2 * (SMALL - i);
        ch[i] = 'a' + (i % 26);
      }
      dList A(x,ch,SMALL), B;
      A.out(10);
      node *tmp = A.search(2*SMALL-8);
      A.moveFront(tmp);
      A.out(10);
      A.moveBack(tmp);
      A.out(10);
      A.find('b');
      A.sort();
      A.out(10);
      A.out(10,'b');
      A.addBack(500,'d');
      A.addFront(501,'z');
      A.out(10);
      A.out(10,'b');
      B.addFront(1,'a');
      B.addBack(2,'b');
      B.out(2);
  
      for(int j=0; j&lt;ROUNDS; j++){
        cout &lt;&lt; endl &lt;&lt; "round " &lt;&lt; j &lt;&lt; endl;
        for(i=0;i&lt;MAX;i++) {x[i] = 2*MAX-i; ch[i] = 'a'+ (i%26);}
         dList A(x,ch,MAX);
         node *tmp = A.search(2*MAX-8);
         A.moveFront(tmp);
         A.moveBack(tmp);
         A.sort();
         A.out(10);
         A.out(10,'b');
      }
  }
  </code></pre>