/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/**********************************************
 *  list_cpp.cpp
 *  Definition of member functions of class List for linked list 
 *********************************************/
#include "list.h"
#include "string.h"
#include "stdio.h"

/* static member to maintain the number
   of the students in the list */
int List::totnum = 0;

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

List::~List() {

   clear();    // release all memory allocated
}

/* append to the list at end */
void List::append(struct Data data)
{
  struct Element *temp = head;
  struct Element *e = new Element;
  e->data = data;
  e->next = NULL;

  if(head == NULL) /* no element in linked list */
    head = e;
  else
  { 
    /* get the tail of the linked list */ 
    while(temp->next) 
      temp = temp->next;
    /* add the new one to the end of the list */
    temp->next = e;
  }

  totnum++; 
  printf("\nelement with id %d is appended\n", e->data.id);
  //cout << endl << "element with id " <<  e->data.id << " is appended" << endl;
} 

/* delete all nodes attached to the list */
void List::clear() {
  struct Element *e = NULL;

  while(head) {
    e = head;
    head = head->next;
    delete e;
    totnum--;
  }
 
  printf("\nAll elements are cleared.\n");
  //cout << endl << "All elements are cleared." << endl;
  return;
}

/* insert to the list at pos */
void List::insert(struct Data data, int pos)
{
  int i = 0;
  struct Element *temp = head;

  struct Element *e = new Element;
  e->data = data;
  e->next= NULL;   /* can be replaced later */

  /* insert to the first position */
  if(pos == 1) {
    head = e;
    head->next = temp;
    totnum++;
    printf("\nAn element with id %d is inserted at the beginning\n", e->data.id);
    return;
  }

  /* get position */
  while(temp) {
    if(++i >= pos-1) break;
    temp = temp->next;
  }
  if(i != pos-1) { /* can not get that position */ 
    printf("\nWrong position for insert\n");
    //cout << endl << "Wrong position for insert " << endl;
    delete e;
    return;
  } 

  e->next = temp->next;
  temp->next = e;
  totnum++;
  
  printf("\nAn element with id %d is inserted in the position %d\n", e->data.id, pos);

  return;
}     
   
/* prepend to the list at the beginning */
void List::prepend(struct Data data)
{
  struct Element *e = new Element;

  e->data = data;
  e->next = head;
  head = e;

  totnum++;
  printf("\nAn element with id %d is prepended\n", e->data.id);
  //cout << endl << "An element with id " <<  e->data.id << " is prepended" << endl;

  return;
}

/* print out the list */
void List::printList() {
  struct Element *temp = head;

  if(temp == NULL) {
    printf("\nNo element is in the list\n");
    //cout << endl << "No element is in the list" << endl;
    return;
  }
  else
    printf("\n %d element is in the list\n", totnum);
    //cout << endl << totnum << " element is in the list" << endl;

  while(temp) {
    printf("id  = %d, neame = %s\n", temp->data.id, temp->data.name);
    //cout << "id  = " << temp->data.id << " name = " << temp->data.name << endl;
    temp = temp->next;
  }
  
  return;
} 

/* print nodes with the same lname */
int List::printName(char * name) {
  struct Element *temp = head;
  int count = 0;

  if(temp == NULL) {
    printf("\nNo element with the name %s\n", name);
    //cout << endl << "No element with the name " << name << endl;
    return 0;
  }

  printf("\nFollowing is/are element(s) with name %s\n", name);
  //cout << endl << "Following is/are element(s) with name " << name << endl;
  while(temp) {
    if(!strcmp(temp->data.name, name))
    {
      printf("id  = %d, name = %s\n", temp->data.id, temp->data.name);
      //cout << "id  = " << temp->data.id << " name = " << temp->data.name << endl;
      count++;
    }
    temp = temp->next;
  }

  return count;
}

/* delete first node with id = id */
void List::removeElement(int id) {
  struct Element *tmp1 = head, *tmp2 = NULL;

  if (head == NULL)
    return;

  else if (head->data.id == id) 
  {
    head = head->next;
    printf("\nAn element with id %d is moved\n", tmp1->data.id);
    //cout << endl << "An element with id " << tmp1->data.id << " is moved. " << endl;
    delete tmp1;
    totnum--;
    return;
  }

  else
    while (tmp1->next)
    {
      if (tmp1->next->data.id == id)
      {
        tmp2 = tmp1->next;
        tmp1->next = tmp1->next->next;
        printf("\nAn element with id %d is moved\n", tmp2->data.id);
        //cout << endl << "An element with id " << tmp2->data.id << " is moved." << endl;
        delete tmp2;
        totnum--;
        return;
      }
      tmp1 = tmp1->next;
    }

  return;
}   

/* create a new item of data */
void createData(struct Data *newData, int id, char *name) {
  newData->id = id;
  newData->name = strdup(name);
  return;
}

/* release the memory */
void freeMem(struct Data *elem) {
  free(elem->name);
  return;
}

