
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"


LIST buildIntList(void) 
{
  LIST l = EMPTYLIST;
  int size;

  printf("How many integers will you input? ");
  scanf("%d", &size);

  while (size) {
    int *newData = (int*) malloc(sizeof(int));
    NODE *newNode;

    printf("Enter data item: ");
    scanf("%d", newData);

    newNode = createNode(newData);
    l = insertNode(newNode, l);

    --size;
  }

  return l;
}


LIST buildStrList(void) 
{
  const int MAXLINE = 60;
  LIST l = EMPTYLIST;
  int size;

  printf("How many strings will you input? ");
  scanf("%d", &size);

  printf("(Maximum line length: %d)\n", MAXLINE);

  while (size) {
    char* newData = (char*) malloc(sizeof(char) * (MAXLINE+1));
    NODE *newNode;

    printf("Enter line: ");
    scanf("%60s", newData);

    newNode = createNode(newData);
    l = insertNode(newNode, l);

    --size;
  }

  return l;
}


void printStrNode(NODE* np) {
  printf("%s\n", (char*)np->pdata);
}


int compInt(void* pi1, void* pi2) 
{
  int i1 = *((int*)pi1);
  int i2 = *((int*)pi2);
  return (i1-i2);
}


int compStr(void* pc1, void* pc2)
{
  return strcmp(pc1, pc2);
}


int main() 
{
  LIST l1 = buildIntList();
  LIST l2;
  l1 = bubbleSort(l1, compInt);
  printList(l1);

  l2 = buildStrList();
  l2 = bubbleSort(l2, compStr);
  applyToList(l2, printStrNode);

  return 0;
}

