예제 #1
0
파일: demo03.c 프로젝트: Sukumi/levawc
/* --- Function: void dequeue_node(Queue que) --- */
void dequeue_node(Queue que)
{
  int tmp, *pi, *ptmp;
  char mess[BUFSIZ], ans;

  /* Initialize 'tmp'... */
  tmp = 0;

  do
    {
      if (tmp == -1)
        break;

      my_clearscrn();
      printf("--- DEQUEUE NODE FROM QUEUE ---\n");
      printf("\nCurrent queue status(%d nodes): ", QUEUEsize(que));
      SLISTtraverse(que, print, SLIST_FWD);

      ptmp = (int *)QUEUEpeek(que);

      if (ptmp  == NULL)
        {
          prompt_and_pause("\n\nQueue is EMPTY - nothing to do..!");
          tmp = -1;
        }
      else
        {
          sprintf(mess, "\nAbout to dequeue node %d.. - Continue? (y/n): ", *ptmp);
          ans = read_char(mess, 0, 0, my_chkch);
          
          if (ans == 'y' || ans == 'Y')
            {
              if ((QUEUEdequeue(que, (void **)&pi)) != OK)
                {
                  printf("\nFatal error dequeing data - exiting...!");
                  QUEUEdestroy(que);
                  exit(-1);
                }
              else
                {
                  sprintf(mess, "Node %d will be dequeued!", *pi);
                  prompt_and_pause(mess);
                  my_destroy(pi);
                }
            }
          else
            tmp = -1;
        }
    } while (TRUE);
}
예제 #2
0
파일: demo3.c 프로젝트: AsamQi/levawc
int main(void)
{
  /* Declare YOUR variables here ! */
  Stack mystk;
  Queue myqueue;
  char mess[BUFSIZ];
  int i, nr;

  srand((unsigned int)time(NULL));
  my_clearscrn();

  printf("--- INITIALIZING A QUEUE, %d ELEMENTS, RANDOM INTEGER DATA ---", NR_OF_ITEMS);
  if ((myqueue = QUEUEinit(my_destroy)) == NULL) /* Initialize the queue... */
    {
      printf("\nFatal error - bailing out...!");
      exit(-1);
    }

  queue_elements(myqueue, NR_OF_ITEMS); /* Populate the queue... */

  nr = QUEUEsize(myqueue)/2;  /* Save half the size of the queue... */
  sprintf(mess, "\nNext - let's DEQUEUE %d elements from our queue...", nr);
  prompt_and_pause(mess);
  prompt_and_pause("...and now PUSH them - on a brand, new STACK...!!");

  if ((mystk = STACKinit(my_destroy)) == NULL) /* Set up a new stack... */
    {
      printf("\nFatal error - bailing out...!");
      exit(-1);
    }

  for (i = 0; i < nr; ++i)
    {
      void *piq, *pis;
      int retval;

      retval = QUEUEdequeue(myqueue, &piq);
      assert(retval == OK);

      sprintf(mess, "QUEUE: Dequeued: %02d (new frontvalue: %02d)", *(int *)piq, *(int *)QUEUEpeek(myqueue));
      prompt_and_pause(mess);

      /* Check current stack top... */
      pis = STACKpeek(mystk);
      /* Push the value just dequeued - from our queue... */
      retval = STACKpush(mystk, piq);
      assert(retval == OK);

      if (pis == NULL) /* If this is the FIRST stack push... */
	sprintf(mess, "STACK: Pushed  : %02d (old stacktop  : none)", *(int *)STACKpeek(mystk));
      else
	sprintf(mess, "STACK: Pushed  : %02d (old stacktop  : %02d)", *(int *)STACKpeek(mystk), *(int *)pis);

      /* Print the message assembled above... */
      prompt_and_pause(mess);
    }

  printf("\n--- CURRENT STATUS OF STACK AND QUEUE ---");
  printf("\nStack: ");
  SLISTtraverse(mystk, print, SLIST_FWD);
  printf("\nQueue: ");
  SLISTtraverse(myqueue, print, SLIST_FWD);
  
  prompt_and_pause("\n\nLet's tidy up (destroy queue/stack) - Bye!");

  SLISTdestroy(mystk);
  SLISTdestroy(myqueue);

  return 0;
}