/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* file name list.cpp */

#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include "list_cpp.h"


/**** static fields *****/
int List::totnum = 0;

List::List() {
  head = NULL; // initialize
}

List::~List() {
    Element *e, *temp;

    e = head;
    while(e) {
      temp  = e;
      e= e->next;
      delete temp;
      totnum--;
    }
    head = NULL;
}

void List::prepend (int ssn, char *fname, char *lname) {
    Element *e = new Element;

    e->ssn = ssn;
    e->fname = strdup(fname);
    e->lname= strdup(lname);
    e->next = head;
    head = e;
    totnum++;
}

void List::printElement(Element *e) {
      printf(" %d %s %s\n", e->ssn, e->fname, e->lname);
}
int List::remove(int ssn) {
    if(head == NULL)              // nothing
       return -1;
    else if(head->ssn == ssn) {   // if the 1st node
       Element *e = head->next;
       delete head;               // delete node
       head = e;
       totnum--;
       return 0;
    }
    else { 
       Element *preve = head;
       Element *e= head->next;
       while(e) {
         if(e->ssn == ssn) {    // ssn match
            preve->next = e->next;
            delete e;           // delete node
            totnum--;
            return 0;
         }
         preve = e;             // keep going
         e= e->next;
       }
       return 0;
    }
}

void List::print() {
    Element *e = head;

    printf("%d elements in the list:\n", totnum);
    //cout << totnum << " elements in the list:\n";
    while(e) {
      printElement(e); 
      e = e->next;
    }
}
