[ create a new paste ] login | about

Link: http://codepad.org/ciczlWmB    [ raw code | output | fork ]

C, pasted on Aug 11:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
/* #define DEBUG */
#if defined(DEBUG)
#include "xmalloc.h"
#else
#define xmalloc(x, y) malloc(x)
#define xfree(x, y) free(x)
#define xrealloc(x, y, z) realloc(x, y)
#define xmallocdump()
#endif
#define ID_GETLINE 1001
#define ID_NODE    1002

#define BUFFSIZE 3 /* >= 2 */
char *mygetline(FILE *fp) {
  static char inbuff[BUFFSIZE];
  char *outbuff_malloc, *tmpbuff;
  char *p, *r;
  int fEOL;
 
  if ((outbuff_malloc = xmalloc(1, ID_GETLINE)) == NULL) {
    return NULL;
  }
  *outbuff_malloc = '\0';
  fEOL = 0;
  do {
    r = fgets(inbuff, BUFFSIZE, fp);
    if (r == NULL)
      break;
    for (p = inbuff; *p != '\0'; p++)
      ;
    if (*(p - 1) == '\n')
      fEOL = 1;
    if ((tmpbuff = xrealloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1, ID_GETLINE)) == NULL) {
      xfree(outbuff_malloc, ID_GETLINE);
      return NULL;
    }
    strcat(tmpbuff, inbuff);
    outbuff_malloc = tmpbuff;
  } while (!fEOL);
  if (strlen(outbuff_malloc) > 0) {
    for (p = outbuff_malloc; *p != '\0'; p++)
      ;
    if (*(p - 1) == '\n')
      *(p - 1) = '\0';
    return outbuff_malloc;
  }
  xfree(outbuff_malloc, ID_GETLINE);
  return NULL;
}
 
struct node {
  void *data;
  struct node *left, *right;
};
 
void *push(struct node **root, void *data, int (*cmp)(void *, void *)) {
  struct node *p;
  if (!*root) {
    if ((p = xmalloc(sizeof(struct node), ID_NODE)) == NULL) {
      fprintf(stderr, "cannot allocate enough memory(struct node), aborted\n");
      exit(-1);
    }
    p->data = data;
    p->left = p->right = NULL;
    *root = p;
    return data;
  } 
  if ((*cmp)(data, (*root)->data) < 0) {
    return push(&((*root)->left), data, cmp);
  } else {
    return push(&((*root)->right), data, cmp);
  }
}
 
void *pop(struct node **root) {
  struct node *p;
  void *data;
  if (*root == NULL) {
    return NULL;
  }
  if ((*root)->left) {
    data = pop(&((*root)->left));
    return data;
  }
  p = *root;
  data = p->data;
  *root = p->right;
  xfree(p, ID_NODE);
  return data;
}
 
int cmp_ascent(char *p, char *q) {
  return strcmp(p, q);
}

int cmp_descent(char *p, char *q) {
  return -strcmp(p, q);
}
 
int main(int argc, char *argv[]) {
  struct node *root;
  char *p;
  int (*cmp)(char *, char *);

  if (argc != 2) {
label_usage:
    fprintf(stderr, "usage: %s <option>\n", argv[0]);
    fprintf(stderr, "option: -a: ascent, -d: descent, input/output: stdio\n");
    return 1;
  }
  if (*argv[1] == '-' && *(argv[1] + 1) == 'a')
    cmp = cmp_ascent;
  else if (*argv[1] == '-' && *(argv[1] + 1) == 'd')
    cmp = cmp_descent;
  else
    goto label_usage;
  
  root = 0;
  while ((p = mygetline(stdin)) != 0)
    push(&root, p, (int (*)(void *, void *))cmp);
  while ((p = pop(&root)) != 0) {
    printf("%s\n", p);
    xfree(p, ID_GETLINE);
  }
  xmallocdump();
  return 0;
}
/* end */


Output:
1
2
usage: /t <option>
option: -a: ascent, -d: descent, input/output: stdio


Create a new paste based on this one


Comments: