Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6.  
  7. int counter = 0;
  8. struct info_node
  9. {
  10.     int  weight ;
  11.  
  12. struct info_node* next ;
  13.  
  14. } ;
  15.  
  16. struct lstack
  17. {
  18.     struct info_node* top ;
  19. } ;  
  20.  
  21. struct lqueue
  22. {
  23.     struct info_node* front ;
  24.     struct info_node*  rear ;
  25. } ;
  26.  
  27. struct prq_que
  28. {
  29.     struct info_node* top ;
  30. } ;
  31.  
  32.  
  33. #define TRUE 1  
  34. #define FALSE 0  
  35.  
  36. void  create_stack( struct lstack *  ) ;  
  37. int   empty_stack( struct lstack*  ) ;
  38. void  push( struct lstack * , struct info_node * ) ;
  39. struct info_node*  pop( struct lstack * ) ;
  40.  
  41. void  create_queue( struct lqueue*  ) ;  
  42. int   empty_queue( struct lqueue*  ) ;
  43. void  queue_insert( struct lqueue * , struct info_node * ) ;
  44. struct info_node*  queue_remove( struct lqueue * ) ;
  45.  
  46.  
  47. void  create_prq( struct prq_que*  ) ;  
  48. int   empty_prq( struct prq_que*  ) ;
  49. void  prq_insert( struct prq_que* , struct info_node * ) ;
  50. struct info_node*  prq_remove( struct prq_que * ) ;
  51.  
  52.  
  53.  
  54. int main()
  55. {
  56.     FILE* infile ;
  57.  
  58.     infile = fopen( "heap_data.txt", "r" ) ;  
  59.  
  60.     struct lstack main_stack ;
  61.     struct lqueue main_queue ;
  62.     struct prq_que pmain_queue ;
  63.  
  64.     struct info_node * new_node  = NULL ;  
  65.     int rweight = 0 ;
  66.     struct info_node* rnode = NULL ;
  67.  
  68.  
  69.           create_stack( & main_stack ) ;  
  70.           create_queue( & main_queue ) ;
  71.           create_prq( & pmain_queue ) ;
  72.  
  73. // begin commentation
  74.     //    while (!feof (infile) )
  75.     //    //for( int a = 0 ; a < 20 ; a++ )
  76.     //    {
  77.  
  78.     //       
  79.  
  80.     //        new_node =  (struct info_node* ) malloc( sizeof( struct info_node ) ) ;  
  81.     //        fscanf(infile, "%d", &new_node->weight ) ;
  82.  
  83.     //        push( &main_stack , new_node ) ;
  84.  
  85.     //       
  86.     //    }  // end of for ( a < 30 )
  87.  
  88.  
  89.     //for( int a = 0 ; a < 20 ; a++ )
  90.     //    {
  91.  //
  92.     //        // printf("%d ", pop(&main_stack )->weight ) ;
  93.     //         rnode = pop( &main_stack ) ;
  94.  
  95.     //     if( rnode != NULL )
  96.     //     {
  97.     //         rweight = rnode->weight ;
  98.     //         printf("%d ", rweight ) ;
  99.     //     }
  100.  
  101.     //       
  102.     //    }  // end of for ( a < 30 )
  103.  
  104.     //  printf("\n\n") ;
  105.  
  106.         // end commentation  
  107.  
  108. //  test code for queue  
  109.  // begin commentation!
  110.           //while (!feof (infile) )
  111.           // reads in actual order
  112.     counter = 120;
  113.         //?
  114.  
  115.           for( int a = 0 ; a <counter; a++ )
  116.           {
  117.  
  118.              
  119.  
  120.               new_node =  (struct info_node* ) malloc( sizeof( struct info_node ) ) ;  
  121.               fscanf(infile, "%d", &new_node->weight ) ;
  122.  
  123.               queue_insert( &main_queue , new_node ) ;
  124.  
  125.               printf("[A]");
  126.               //counter++;
  127.               // MAKE THE a queue of 20 numbers!
  128.           }  // end of for ( a < 30 )
  129.  
  130.           printf("\n\n");
  131.            
  132.           // sorts
  133.          
  134.            for( int a = 0 ; a < counter ; a++ ) // a will end when counter from last is equal
  135.      {
  136.  
  137.               // printf("%d ", pop(&main_stack->weight) ) ; // should not pop?
  138.                rnode = queue_remove( &main_queue ) ; // remove from queue
  139.  
  140.            if( rnode != NULL )
  141.            {
  142.                rweight = rnode->weight ;
  143.                printf("%d ", rweight ) ;// write value removed??
  144.            }
  145.  
  146.               printf("[b]"); // printf debug
  147.      }  // end of for ( a < 30 )
  148.  
  149.         printf("\n\n");
  150.            
  151. // end commentation
  152.  
  153.  
  154.  for( int a = 0 ; a < counter ; a++ ) // all code same now!
  155.           {
  156.  
  157.              
  158.  
  159.               new_node =  (struct info_node* ) malloc( sizeof( struct info_node ) ) ;  
  160.               fscanf(infile, "%d", &new_node->weight ) ;
  161.  
  162.               prq_insert( &pmain_queue , new_node ) ;
  163.  
  164.               printf("[C]");
  165.               // this one is read in in the correct order! via a pqueue???
  166.           }  // end of for ( a < 30 )
  167.  
  168.  
  169.  
  170.  
  171.           for( int a = 0 ; a < counter ; a++ )
  172.           {
  173.  
  174.              //printf("%d ", pop(&main_stack )->weight ) ; // was commented out
  175.                rnode = prq_remove( &pmain_queue ) ; // why remove first?
  176.  
  177.            if( rnode != NULL )
  178.            {
  179.                rweight = rnode->weight ;
  180.                printf("%d ", rweight ) ;
  181.            }
  182.            // printed the priority queue?
  183.               printf("[D]");
  184.           }  // end of for ( a < 30 )
  185.  
  186.         printf("\n\n") ;
  187.  
  188.  
  189.  
  190.  
  191.  
  192.         fclose( infile ) ;
  193.         system("PAUSE");
  194.     return( 0 ) ;
  195.  
  196.  
  197. }  // end of function main()
  198.  
  199. void  create_stack( struct lstack * nstack )
  200. {
  201.     nstack->top = NULL ;
  202.     return ;
  203.  
  204. }  // end of function create_stack()
  205.  
  206. int   empty_stack( struct lstack* nstack )
  207. {
  208.     if( nstack->top == NULL )
  209.         return( TRUE ) ;
  210.     else
  211.         return( FALSE ) ;
  212.  
  213. }  // end of function empty_stack()
  214.  
  215. void  push( struct lstack * nstack , struct info_node * new_node )
  216. {
  217.     new_node->next = nstack->top ;
  218.     nstack->top = new_node ;
  219.     return ;
  220. }  // end of function push()
  221.  
  222.  
  223. struct info_node*  pop( struct lstack * nstack )
  224. {
  225.     struct info_node* preturn = NULL ;
  226.  
  227.     if( empty_stack( nstack ) )
  228.     {
  229.         printf("ERROR: Stack is Empty \n\n" ) ;
  230.         return( preturn ) ;
  231.     }
  232.     else
  233.     {
  234.         preturn = nstack->top ;
  235.         nstack->top = nstack->top->next ;
  236.         return( preturn ) ;
  237.     }
  238.  
  239. } // end of function pop()
  240.  
  241.  
  242.  
  243.  
  244. void  create_queue( struct lqueue * nqueue)
  245. {
  246.     nqueue->front = NULL ;
  247.     nqueue->rear  = NULL ;
  248.     return ;
  249.  
  250. }  // end of function create_queue()
  251.  
  252. int   empty_queue( struct lqueue* nqueue )
  253. {
  254.     if ( ( nqueue->front == NULL ) && ( nqueue->rear  == NULL ) )
  255.         return( TRUE ) ;
  256.     else
  257.         return( FALSE ) ;
  258.  
  259. }  // end of function empty_queue()
  260.  
  261. void  queue_insert( struct lqueue * nqueue , struct info_node * new_node )
  262. {
  263.    
  264.     if( empty_queue( nqueue ) )
  265.     {
  266.         new_node->next = NULL ;
  267.         nqueue->front = new_node ;
  268.         nqueue->rear  = new_node ;
  269.     }
  270.     else
  271.     {
  272.         nqueue->rear->next = new_node ;
  273.         new_node->next = NULL ;
  274.         nqueue->rear = new_node ;
  275.     }
  276.  
  277.     return ;
  278. }  // end of function queue_insert ()
  279.  
  280.  
  281. struct info_node*  queue_remove( struct lqueue * nqueue )
  282. {
  283.     struct info_node* preturn = NULL ;
  284.  
  285.     if( empty_queue( nqueue ) )
  286.     {
  287.         printf("ERROR:  Queue is Empty \n\n" ) ;
  288.         return( preturn ) ;
  289.     }
  290.     else if ( nqueue->front == nqueue->rear )
  291.     {
  292.         preturn = nqueue->front  ;
  293.         nqueue->front = NULL  ;
  294.         nqueue->rear  = NULL  ;
  295.         return( preturn ) ;
  296.     }
  297.     else
  298.     {
  299.         preturn = nqueue->front  ;
  300.         nqueue->front = nqueue->front->next  ;
  301.         return( preturn ) ;
  302.     }
  303.  
  304. } // end of function pop()
  305.  
  306. void  create_prq( struct prq_que*  ppque )  
  307. {
  308.     ppque->top = NULL ;
  309.     return ;
  310.  
  311. }   // end of function create_prq()
  312.  
  313.  
  314. int   empty_prq( struct prq_que* ppque )
  315. {
  316.     if( ppque->top == NULL )
  317.         return(TRUE ) ;
  318.     else
  319.         return(FALSE ) ;
  320.  
  321. }  // end of function empty_prq()
  322.  
  323.  
  324. void  prq_insert( struct prq_que* ppque , struct info_node * nnode)
  325. {
  326.  
  327.     struct info_node* move_pntr ;
  328.     struct info_node* pre_pntr ;
  329.  
  330.  
  331.  
  332.     if( empty_prq( ppque ) )
  333.     {
  334.         ppque->top = nnode ;
  335.         nnode->next = NULL ;
  336.     }
  337.     else
  338.     {
  339.         move_pntr = ppque->top ;
  340.         pre_pntr = move_pntr ;
  341.  
  342.         while( ( move_pntr != NULL )&& ( nnode->weight > move_pntr->weight ) )
  343.         {
  344.             pre_pntr = move_pntr ;
  345.             move_pntr = move_pntr->next ;
  346.         }
  347.  
  348.         if( move_pntr == NULL )
  349.         {
  350.             pre_pntr->next = nnode ;
  351.             nnode->next = NULL ;
  352.         }
  353.         else if( move_pntr == pre_pntr )
  354.         {
  355.             nnode->next = ppque->top ;
  356.             ppque->top = nnode ;
  357.         }
  358.         else
  359.         {
  360.             nnode->next = move_pntr ;
  361.             pre_pntr->next = nnode ;
  362.         }
  363.  
  364.  
  365.     }  // end of else ( not empty priority queue )
  366.  
  367.  
  368.     return ;
  369.  
  370. } // end of function prq_insert()
  371.  
  372.  
  373.  
  374. struct info_node*  prq_remove( struct prq_que * ppque )
  375. {
  376.  
  377.     struct info_node *  rnode = NULL ;
  378.  
  379.     if( empty_prq( ppque ) )
  380.         printf("ERROR: Priority Queue is Empty \n") ;
  381.     else
  382.     {
  383.         rnode = ppque->top ;
  384.         ppque->top = ppque->top->next ;
  385.     }
  386.     return( rnode ) ;
  387.  
  388. }  // end of function prq_remove()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement