Exemplo n.º 1
0
int main(void)
{
  /* Declare YOUR variables here ! */
  Slist mylist;
  int menu_choice;

  /* Seed to random generator and clear the screen.. */
  srand((unsigned int)time(NULL));

  if ((mylist = SLISTinit(my_destroy)) == NULL)
    {
      printf("\nFatal error... - bailing out!");
      SLISTdestroy(mylist);
      exit(-1);
    }

  /* Set match-callback into list structure.. */
  SLISTsetmatch(mylist, my_match);

  /* Populate the (empty) list.. */
  create_random_nodes(mylist, NR_OF_ITEMS);

  /* Enter menu loop... */
  do
    {
      menu_choice = menu(MAIN_MENU_ROW, 0, 5);

      switch (menu_choice)
        {
        case 1:
          ins_nodes(mylist);
          break;
        case 2:
          rem_nodes(mylist);
          break;
        case 3:
          search_node(mylist);
          break;
        case 4:
          sort_list(mylist);
          break;
        case 5:
          print_list(mylist);
          break;
        default:
          final_status(mylist);
          break;
        }
    }
  while (menu_choice); 

  /* ..and finally destroy the list. */
  prompt_and_pause("\n\nLet's tidy up and destroy the list..- Bye!");
  SLISTdestroy(mylist);

  return 0;
}
Exemplo n.º 2
0
int bfs(void)
{
  Graph gr;
  Slist network;
  struct BfsVertexdata_ bfstmp;
  BfsVertexdata netdata;
  int tmpid, retval;
  SlistNode listnode;

  my_clearscrn();
  printf("--- NETWORK HOPS/BFS DEMO ---");

  gr = GRAPHinit(bfs_match, bfs_destroy);

  /* Read net node(=vertex) data into graph.. */
  if ((read_netnodes(gr, net_nodes, NR_OF_NETNODES)) != OK)
    {
      fprintf(stderr, "Fatal error when reading netnode data - bailing out..");
      GRAPHdestroy(gr);
      exit(-1);
    }
  /* Read net connection(=edge) data into graph.. */
  if ((read_netconnections(gr, net_connections, NR_OF_NETNODES)) != OK)
    {
      fprintf(stderr, "Fatal error when reading netconnections data - bailing out..");
      GRAPHdestroy(gr);
      exit(-1);
    }

  prompt_and_pause("\n\nGraph initialized and graph data read..");
  printf("\nGRAPH:");

  /* Display graph.. */
  GRAPHprint(gr, prt_bfs_vtx, prt_bfs_edge);
  printf("\nNr of vertices/edges: %d/%d", GRAPHvcount(gr), GRAPHecount(gr));

  tmpid = read_int("\nEnter startnode id ", 1, 6);
  bfstmp.data = &tmpid;

  if ((retval = ALGObfs(gr, &bfstmp, &network, bfs_match)) != OK)
    {
      fprintf(stderr, "\nFatal error when calling 'ALGObfs()'(Return value: %d) - Bailing out..", retval);
      GRAPHdestroy(gr);
      exit(-1);
    }

  printf("\nNetwork Hops(BFS Analysis)\n--------------------------");
  for (listnode = SLISThead(network); listnode != NULL; listnode = SLISTnext(listnode))
    {
      netdata = (BfsVertexdata)SLISTdata(listnode);
      printf("\nNode%02d, color=%d, hops=%d", *(int *)netdata->data, netdata->color, netdata->hops);
    }
    
  prompt_and_pause("\n\nTime to tidy up..");

  SLISTdestroy(network);
  GRAPHdestroy(gr);
 
  return OK;
}
Exemplo n.º 3
0
int dfs(void)
{
  Graph gr;
  Slist tasks;
  DfsVertexdata taskdata;
  int retval;
  SlistNode listnode;

  my_clearscrn();
  printf("--- TOPOLOGICAL SORTING/DFS DEMO ---");

  gr = GRAPHinit(dfs_match, dfs_destroy);

  /* Read task node(=vertex) data into graph.. */
  if ((read_tasknodes(gr, task_nodes, NR_OF_TASKNODES)) != OK)
    {
      fprintf(stderr, "Fatal error when reading task node data - bailing out..");
      GRAPHdestroy(gr);
      exit(-1);
    }
  
  /* Read task connection(=edge) data into graph.. */
  if ((read_taskconnections(gr, task_connections, NR_OF_TASKNODES)) != OK)
    {
      fprintf(stderr, "Fatal error when reading taskconnections data - bailing out..");
      GRAPHdestroy(gr);
      exit(-1);
    }

  prompt_and_pause("\n\nGraph created and graph data read..");
  printf("\nGRAPH:");

  /* Display graph.. */
  GRAPHprint(gr, prt_dfs_vtx, prt_dfs_edge);
  printf("\nNr of vertices/edges: %d/%d", GRAPHvcount(gr), GRAPHecount(gr));

  if ((retval = ALGOdfs(gr, &tasks)) != OK)
    {
      fprintf(stderr, "\nFatal error when calling 'ALGOdfs()'(Return value: %d) - Bailing out..", retval);
      GRAPHdestroy(gr);
      exit(-1);
    }

  printf("\n\nTopological Sort(DFS Analysis):\n-------------------------------");
  for (listnode = SLISThead(tasks); listnode != NULL; listnode = SLISTnext(listnode))
    {
      taskdata = (DfsVertexdata)SLISTdata(listnode);
      printf("\nNode %c, color=%d", *(char *)taskdata->data, taskdata->color);
    }

  prompt_and_pause("\n\nTime to tidy up..");

  SLISTdestroy(tasks);
  GRAPHdestroy(gr);

  return OK;
}
Exemplo n.º 4
0
void CHTBLdestroy(CHtbl htbl)
{
  int i;

  for (i = 0; i < htbl->buckets; ++i)
    {
      SLISTdestroy(htbl->table[i]);
    }

  free(htbl->table);
  free(htbl);
}
Exemplo n.º 5
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);
}
Exemplo n.º 6
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);
}
Exemplo n.º 7
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;
}