CS140 Midterm Exam - October 5, 2004 - Page 4


Question 4

Behold p4.c:

#include < string.h >
#include < stdio.h >
#include "fields.h"
#include "dllist.h"

main()
{
  Stack s;
  Queue q;
  IS is;
  Jval j;
  int i;

  i = 0;
  s = new_stack();
  q = new_queue();
   
  is = new_inputstruct(NULL);

  while (get_line(is) >= 1) {
    if (i == 0) {
      stack_push(s, new_jval_s(strdup(is->fields[0])));
    } else {
      queue_enqueue(q, new_jval_s(strdup(is->fields[0])));
    }
    i = 1-i;
  }
  while (!stack_empty(s)) {
    j = stack_pop(s);
    printf("%s\n", j.s);
  }

  printf("\n");

  while (!queue_empty(q)) {
    j = queue_dequeue(q);
    printf("%s\n", j.s);
  }
}

What is the output of: p4 < inputfile