Beispiel #1
0
/* --- Function: void sort_list(Slist list) --- */
void sort_list(Slist list)
{
  my_clearscrn();
  printf("--- SORT LIST ---\n");
  SLISTsort(list, my_cmp);
  printf("\nCurrent list status(%d nodes, ascending) : ", SLISTsize(list));
  SLISTtraverse(list, print, SLIST_FWD);

  printf("\nCurrent list status(%d nodes, descending): ", SLISTsize(list));
  SLISTtraverse(list, print, SLIST_BWD);
  prompt_and_pause("\n\n");
}
Beispiel #2
0
/* --- Function: void print_queue_stack(Queue que, Stack stk) --- */
void print_queue_stack(Queue que, Stack stk)
{
  my_clearscrn();
  printf("--- PRINT QUEUE AND STACK ---\n");
  printf("\nCurrent contents: ");
  printf("\nQueue: ");
  SLISTtraverse(que, print, SLIST_FWD);
  printf(" (%d nodes)", QUEUEsize(que));
  printf("\nStack: ");
  SLISTtraverse(stk, print, SLIST_FWD);
  printf(" (%d nodes)", STACKsize(stk));
  prompt_and_pause("\n\n");
}
Beispiel #3
0
/* --- Function: void final_status(Queue que, Stack stk) --- */
void final_status(Queue que, Stack stk)
{
  /* Final list status... */
  my_clearscrn();
  printf("--- FINAL STATUS ---\n");
  printf("\nFinal list contents: ");
  printf("\nQueue: ");
  SLISTtraverse(que, print, SLIST_FWD);
  printf(" (%d nodes)", QUEUEsize(que));
  printf("\nStack: ");
  SLISTtraverse(stk, print, SLIST_FWD);
  printf(" (%d nodes)", STACKsize(stk));
}
Beispiel #4
0
/* --- Function: void push_node(Stack stk) --- */
void push_node(Stack stk)
{
  int tmp, *pi;
  char mess[BUFSIZ];

  do
    {
      my_clearscrn();
      printf("--- PUSH NODE ON STACK ---\n");
      printf("\nCurrent stack status(%d nodes): ", STACKsize(stk));
      SLISTtraverse(stk, print, SLIST_FWD);

      tmp = read_int("\nEnter integer data of node to be pushed (-1=Quit): ", 0, 0);

      if (tmp == -1)
        break;

      pi = (int *)malloc(sizeof(int));
      MALCHK(pi);

      *pi = tmp;

      if ((STACKpush(stk, pi)) != OK)
        {
          printf("\nFatal error pushing data - exiting...!");
          STACKdestroy(stk);
          exit(-1);
        }
      else
        {
          sprintf(mess, "Node %d will be pushed on stack!", *pi);
          prompt_and_pause(mess);
        }
    } while (TRUE);
}
Beispiel #5
0
/* --- Function: void create_random_nodes(Slist list, int nr_of_nodes) --- */
void create_random_nodes(Slist list, int nr_of_nodes)
{
  int i=0, *pi, retval;

  my_clearscrn();
  /* Initialize the list.. */
  printf("--- CREATED A SINGLY-LINKED LIST(%d NODES)- RANDOM INTEGER DATA ---", NR_OF_ITEMS);

  do
    {
      pi = (int *)malloc(sizeof(int));
      MALCHK(pi);

      *pi = rand_int(1,50);

      retval=SLISTinsnext(list, NULL, pi);
      assert(retval == OK);

    } while (++i < nr_of_nodes);

  /* Display the list... */
  printf("\n\nCurrent list status(%d nodes): ", SLISTsize(list));
  SLISTtraverse(list, print, SLIST_FWD);
  prompt_and_pause("\n\n");
}
Beispiel #6
0
/* --- Function: void enqueue_node(Queue que) --- */
void enqueue_node(Queue que)
{
  int tmp, *pi;
  char mess[BUFSIZ];

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

      tmp = read_int("\nEnter integer data of node to be enqueued (-1=Quit): ", 0, 0);

      if (tmp == -1)
        break;

      pi = (int *)malloc(sizeof(int));
      MALCHK(pi);

      *pi = tmp;

      if ((QUEUEenqueue(que, pi)) != OK)
        {
          printf("\nFatal error enqueing data - exiting...!");
          QUEUEdestroy(que);
          exit(-1);
        }
      else
        {
          sprintf(mess, "Node %d will be enqueued!", *pi);
          prompt_and_pause(mess);
        }
    } while (TRUE);
}
Beispiel #7
0
/* --- Function: void search_node(Slist lst) --- */
void search_node(Slist lst)
{
  int tmp;
  char mess[BUFSIZ];

  do
    {
      my_clearscrn();
      printf("--- SEARCH NODE ---\n");
      printf("\nCurrent list status(%d nodes): ", SLISTsize(lst));
      SLISTtraverse(lst, print, SLIST_FWD);

      tmp = read_int("\nEnter keydata for node to be found (-1=Quit): ", 0, 0);

      if (tmp == -1)
        break;

      if (SLISTfindnode(lst, &tmp) == NULL) /* Node not found.. */
        {
          sprintf(mess, "Node %d NOT found..!", tmp);
          prompt_and_pause(mess);
        }
      else
        {
          /* Search succesful - notify user.. */
          sprintf(mess, "Node %d FOUND!", tmp);
          prompt_and_pause(mess);
        }
    } while (TRUE);
}
Beispiel #8
0
/* --- Function: void final_status(Slist list) --- */
void final_status(Slist list)
{
  my_clearscrn();
  printf("--- FINAL STATUS ---\n");
  /* Final list status... */
  printf("\nFinal list contents(%d nodes): ", SLISTsize(list));
  SLISTtraverse(list, print, SLIST_FWD);
}
Beispiel #9
0
/* --- Function: void print_list(Slist lst) --- */
void print_list(Slist lst)
{
  my_clearscrn();
  printf("--- PRINT LIST ---\n");
  /* List status... */
  printf("\nCurrent list status(%d nodes): ", SLISTsize(lst));
  SLISTtraverse(lst, print, SLIST_FWD);
  prompt_and_pause("\n\n");
}
Beispiel #10
0
void CHTBLprint(CHtbl htbl, void (*callback)(const void *data))
{
  int i;

  for (i = 0; i < htbl->buckets; ++i)
    {
      printf("\nBucket #%03d: ", i);
      SLISTtraverse(htbl->table[i], callback, SLIST_FWD);
    }
}
Beispiel #11
0
/* --- Function: void pop_node(Stack stk) --- */
void pop_node(Stack stk)
{
  int tmp, *pi, *ptmp;
  char mess[BUFSIZ], ans;

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

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

      my_clearscrn();
      printf("--- POP NODE FROM STACK ---\n");
      printf("\nCurrent stack status(%d nodes): ", STACKsize(stk));
      SLISTtraverse(stk, print, SLIST_FWD);

      ptmp = (int *)STACKpeek(stk);

      if (ptmp  == NULL)
        {
          prompt_and_pause("\n\nStack is EMPTY - nothing to do..!");
          tmp = -1;
        }
      else
        {
          sprintf(mess, "\nAbout to pop node %d.. - Continue? (y/n): ", *ptmp);
          ans = read_char(mess, 0, 0, my_chkch);

          if (ans == 'y' || ans == 'Y')
            {
              if ((STACKpop(stk, (void **)&pi)) != OK)
                {
                  printf("\nFatal error popping data - exiting...!");
                  STACKdestroy(stk);
                  exit(-1);
                }
              else
                {
                  sprintf(mess, "Node %d will be popped!", *pi);
                  prompt_and_pause(mess);
                  my_destroy(pi);
                }
            }
          else
            tmp = -1;
        }
    } while (TRUE);
}
Beispiel #12
0
/* --- 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);
}
Beispiel #13
0
/* --- Function: void queue_nodess(Queue que, Stack stk, int nr_of_ele) --- */
void enqueue_push_nodes(Queue que, Stack stk, int nr_of_ele)
{
  int i=0, *pi, retval;

  my_clearscrn();
  printf("--- CREATING QUEUE AND STACK (%d nodes each), RANDOM INTEGER DATA ---\n", NR_OF_ITEMS);

  do
    {
      /* Create dyn. memory, store random nr - and enqueue... */
      pi = (int *)malloc(sizeof(int));
      MALCHK(pi);

      *pi = rand_int(1,50);

      retval = QUEUEenqueue(que, pi);
      assert(retval == OK);

      /* Create dyn. memory, store random nr - and push... */
      pi = (int *)malloc(sizeof(int));
      MALCHK(pi);

      *pi = rand_int(1,50);

      retval = STACKpush(stk, pi);
      assert(retval == OK);

    } while (++i < nr_of_ele);

  printf("\nCurrent queue and stack status: ");
  printf("\nQueue(%d nodes): ", QUEUEsize(que));
  SLISTtraverse(que, print, SLIST_FWD);
  printf("\nStack(%d nodes): ", STACKsize(stk));
  SLISTtraverse(stk, print, SLIST_FWD);
  prompt_and_pause("\n\n");
}
Beispiel #14
0
/* --- Function: void rem_nodes(Slist list) --- */
void rem_nodes(Slist list)
{
  int tmp, *pi, retval;
  char mess[BUFSIZ];

  do
    {
      my_clearscrn();
      printf("--- REMOVE NODE FROM LIST ---\n");
      printf("\nCurrent list status(%d nodes): ", SLISTsize(list));
      SLISTtraverse(list, print, SLIST_FWD);

      tmp = read_int("\nEnter keydata for node to be removed (-1=Quit): ", 0, 0);

      if (tmp == -1)
        break;

      /* Remove node - and free memory */
      pi = &tmp;

      if ((retval = SLISTfind_remove(list, (void **)&pi)) != OK)
        {
          if (retval == 1)
            {
              sprintf(mess, "Error: Node %d not found..!", tmp);
              prompt_and_pause(mess);
            }
          else 
            {
              if (retval == -2)
                printf("\nError: Match-callback is missing... - bailing out!");
              else
                printf("\nFatal error... - bailing out!");
              SLISTdestroy(list);
              exit(retval);
            }
        }
      else
        {
          /* Removal succesful - notify user.. */
          sprintf(mess, "Node %d will be removed..!", *(int *)pi);
          prompt_and_pause(mess);
          /* Free node - after being removed from list.. */
          my_destroy(pi);
        }
    } while (TRUE);
}
Beispiel #15
0
/* --- void queue_elements(Queue que, int nr_of_ele) --- */
void queue_elements(Queue que, int nr_of_ele)
{
  int i=0, *pi, retval;

  do
    {
      pi = (int *)malloc(sizeof(int));
      *pi = my_random(1,50);
      retval = QUEUEenqueue(que, pi);
      assert(retval == OK);
    } while (++i < nr_of_ele);

  printf("\nCurrent queue content(%d elements): ", QUEUEsize(que));
  /* This call to SLISTtraverse is possible - although this function is NOT a member
     of the queue (public) interface - but because a queue is also a list ! */
  SLISTtraverse(que, print, SLIST_FWD);
}
Beispiel #16
0
/* --- Function: void ins_nodes(Slist list) --- */
void ins_nodes(Slist list)
{
  int tmp, *pi;
  SlistNode node;
  char mess[BUFSIZ];

  do
    {
      my_clearscrn();
      printf("--- ADD NODE - WITH DATA=99 - AFTER USER-SPECIFIED NODE ---\n");
      printf("\nCurrent list status(%d nodes): ", SLISTsize(list));
      SLISTtraverse(list, print, SLIST_FWD);

      tmp = read_int("\nEnter (key)data, after which new node(key=99) will be inserted (-1=Quit): ", 0, 0);

      if (tmp == -1)
        break;

      if ((node = SLISTfindnode(list, &tmp)) != NULL) /* Node found */
        {
          /* Insert node after first occurance of user-specified node */
          pi = (int *)malloc(sizeof(int));
          MALCHK(pi);

          *pi = 99;

          if ((SLISTinsnext(list, node, pi)) != OK)
            {
              printf("\nFatal error - exiting...!");
              SLISTdestroy(list);
              exit(-1);
            }
          else
            {
              sprintf(mess, "Node 99 will be inserted after node %d", *(int *)SLISTdata(node));
              prompt_and_pause(mess);
            }
        }
      else
        {
          sprintf(mess, "Error: Node %d not found...!", tmp);
          prompt_and_pause(mess);
        }
    } while (TRUE);
}
Beispiel #17
0
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;
}