2587, 1/130 ȸ¿ø°¡ÀÔ  ·Î±×ÀΠ 
   ¿­½ÉÈ÷
   Ã¥³»¿ëÁß¿¡¿ä....HEAPÀ» ±¸ÇöÇÒ¶§

http://www.hackerschool.org/HS_Boards/view.php?id=books&no=1582 [º¹»ç]


Ã¥¿¡ ³ª¿ÍÀÖ´Â ¼Ò½ºÀä...put_heap() ÇÔ¼ö¿¡¼­

temp = (int *)malloc( count * sizeof(int) );
        if( temp == NULL )
                return 0;
À§ ¼Ò½ººÎºÐ¿¡¼­ (int *)malloc()¿¡¼­ count = 0À϶§ (int *)malloc(0)ÀÌ µÇ´Âµ¥
Àç »ý°¢¿¡´Â À̶§ temp¿¡ NULL °ªÀÌ µé¾î°¡´Â °Í °°°Åµç¿ä ½ÇÁ¦·Î ÄÄÆÄÀÏÇؼ­
½ÇÇàÇغ¸¸é Ã¥¿¡ ÀÖ´Â ¼Ò½º ´ë·Î¶ó¸é °è¼Ó À§ºÎºÐ¿¡¼­ 0°ªÀÌ °è¼Ó ¸®ÅÏ µÇ¼­
½ÇÇàÀÌ µÇÁö ¾Ê½À´Ï´Ù..¸Û¸Û´Ô ÀÌ°Å ÇØ°áÁ» ÇØÁÖ¼¼¿ä ¾Æ·¡´Â Àüü ¼Ò½º±¸¿ä..

/* pre_processing command */
#include <stdio.h>
#include <stdlib.h>

/* variables announce */
int *heap, count;

/* function prototypes announce */
int create_heap( void );
int put_heap( int data );
int get_heap( void );
void clean_heap( void );

/* structure define */


/* main() function define */
int main( void )
{
        int n,d;

        create_heap();

        while( 1 ){
                printf( "\n--------------------\n" );
                printf( "1. Put a data\n" );
                printf( "2. Get a data\n" );
                printf( "3. Exit\n" );
                printf( ">" );
                scanf( "%d", &n );

                if( n == 3 )
                        break;
                
                if( n == 1 ){
                        printf( "Enter a number\n" );
                        printf( ">" );
                        scanf( "%d", &d );
                        put_heap( d );
                        continue;
                }
                if( n == 2 ){
                        d = get_heap();
                        if( d != -1 )
                                printf( "Number is %d.\n", d );
                        else
                                printf( "No data.\n" );
                        continue;
                }
        }
        clean_heap();

        return 1;
}


/* function define */

/* create_heap() define */
int create_heap( void )
{
        count = 0;
        heap = (int *)malloc( 1 );
        if( heap == NULL ){
                return 0;
        }
        return 1;
}

/* put_heap() define */
int put_heap( int data )
{
        int *temp;

        temp = (int *)malloc( count * sizeof(int) );
        if( temp == NULL )
                return 0;
        memcpy( temp, heap, count * sizeof(int) );
        free( heap );
        count += 1;
        heap = (int *)malloc( count * sizeof(int) );
        if( heap == NULL )
                return 0;
        heap[0] = data;
        memcpy( heap+1, temp, (count-1) * sizeof(int) );
        free( temp );
        return 1;
}

/* get_heap() define */
int get_heap( void )
{
        int *temp,value;

        if( count == 0 ){
                return -1;
        }

        temp = (int *)malloc( count * sizeof(int) );
        if( temp == NULL ){
                return -1;
        }
        memcpy( temp, heap, count * sizeof(int) );
        free( heap );
        count -= 1;
        heap = (int *)malloc( count * sizeof(int) );
        if( heap == NULL )
                return -1;
        memcpy( heap, temp, count * sizeof(int) );
        value = temp[count];
        free( temp );
        return value;
}

/* clean_heap() define */
void clean_heap( void )
{
        free( heap );
}

  Hit : 939     Date : 2003/05/18 06:07



    
¸Û¸Û ¾È³çÇϼ¼¿ä. ¸»¾¸ÇϽŠºÎºÐÀ» Å×½ºÆ® Çغ¸´Ï, linuxÀÏ ¶§¿Í dosÀ϶§ °á°ú Â÷ÀÌ°¡ ³ª³×¿ä. 2003/05/19  
¸Û¸Û linuxÀÇ °æ¿ì malloc(0)À¸·Î ÇÒ´çµÈ °ø°£ÀÌ heap ¿µ¿ªÀÇ ÇÑ ÁÖ¼Ò°¡ µÇ´Â ¹Ý¸é¿¡ 2003/05/19  
¸Û¸Û dos¿¡¼± ´ÔÀÌ ¸»¾¸ÇϽŴë·Î 0ÀÌ µÇ¾î¹ö¸®´Â±º¿ä. ÇØ´ç ºÎºÐÀ» ¼öÁ¤ÇÏ¿©µµ ´Ù¸¥ ºÎºÐ¿¡¼­ °è¼Ó ¿¡·¯°¡ ³ª´Â °ÍÀ» º¸´Ï 2003/05/19  
¸Û¸Û linux¿Í dosÀÇ ÁÖ¼Ò Ã¼°è°¡ ¸¹ÀÌ ´Ù¸¥ °Í °°½À´Ï´Ù. 2003/05/19  
¸Û¸Û ¿©·¯ ¹æ¹ýÀ¸·Î Å×½ºÆ®¸¦ ÇغÁ¾ßÇÒ °Í °°±¸¿ä. ÀÏ´Ü ÇØ´ç ÄÚµå´Â linux ȯ°æÀÎ ftz.hackerschool.org¿¡¼­ ÇнÀÇØ Áֽñ⠹ٶø´Ï´Ù. 2003/05/19  
¸Û¸Û cat > heap.c ÀÔ·ÂÇϽŠÈÄ, À§ ³»¿ëÀ» ºÙ¿©³Ö±â, ±× ´ÙÀ½ ctrl+d¸¦ ´©¸£½Ã¸é ÄÚµå°ú ¿Ï¼ºµÇ±¸¿ä. 2003/05/19  
¸Û¸Û gcc -o heap heap.c ±× ´ÙÀ½ ./heapÀ» ÀÔ·ÂÇÏ½Ã¸é µË´Ï´Ù. 2003/05/19  

   Ã¥³»¿ëÁß¿¡¿ä....HEAPÀ» ±¸ÇöÇÒ¶§ [7]

¿­½ÉÈ÷
2003/05/18 938
 
     [re] Ã¥³»¿ëÁß¿¡¿ä....HEAPÀ» ±¸ÇöÇÒ¶§

¿­½ÉÈ÷
2003/05/20 867

Copyright 1999-2024 Zeroboard / skin by Hackerschool.org / Secure Patch by Hackerschool.org