ÇÁ·Î±×·¡¹Ö

 3206, 10/161 ȸ¿ø°¡ÀÔ  ·Î±×ÀΠ 
   ypkhj1
   C°øºÎÇÏ´Ù ±Ã±ÝÇÑ°Ô À־¿ä~´äº¯Á»ÇØÁÖ¼¼¿ä^^

http://www.hackerschool.org/HS_Boards/zboard.php?id=QNA_programming&no=196 [º¹»ç]


#include <stdio.h>
#include <stdlib.h>

int *heap, count;

int creat_heap( void );
int put_heap( int data );
int get_heap( void );
void clean_heap( void );



int main( void )
{
int c, i;

creat_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", &c );

if( c == 3 )
break;
if( c == 1 )
{
int a = 0;

printf( "Enter a number: " );
scanf( "%d", &i );
put_heap( i );
continue;
}
if( c == 2 )
{
i = get_heap();

if( i != -1 )
printf( "Number is %d.\n", i );
else
printf( "No data.\n" );
continue;
}
}
clean_heap();

return 1;
}

int creat_heap( void )
{
count = 0;
heap = (int *) malloc( 1 );

if( heap == NULL )
return 0;

return 1;
}

int put_heap( int data )
{
int *temp, a;

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 ) );
/* ¿©±â memcpy( heap + 1, temp, (count - 1) * sizeof(int) );
¼³¸íÁ» ÇØÁÖ¼¼¿ä. °è¼ÓºÁµµ À߸ô¸£°Ú³×¿ä */
free( temp );
return 1;
}

int get_heap( void )
{
int *temp, value, a;

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;
}

void clean_heap( void )
{
free( heap );
}

/* Èü¿¡ °üÇؼ­ º¸´Âµ¥ ±Ã±ÝÇÑ°Å ÀÖ¾î±×·¯´Âµ¥¿ä..int put_heap( int data)
¿©±âÇÔ¼ö¿¡¼­ óÀ½¿¡´Â countÃʱⰪÀÌ 0ÀÌÀݽÀ´Ï±î..¼öÇн𣿡 º¸¸é
0 °öÇϱ⠾ ¼ýÀÚÇÏ¸é ´äÀº 0À» °öÇßÀ¸¸é 0À̵Ǵµ¥
(int *) malloc( count * sizeof( int ) ); ¿©±â¼­
count * intÇü¸¸Å­ ¸Þ¸ð¸®ÇÒ´çÇϴµ¥ count°¡ 0ÀÌ¸é ¸Þ¸ð¸®ÇÒ´çÀÌ
¾î¶»°Ô µÇ´Â°Ì´Ï±î?? ±×¸®°í º¸¸é mallocÇÔ¼ö¸¦ ¾µ¶§ (int *)malloc(4)
À̰Ŷû (int *) malloc( sizeof( int ) )À̰Ŷû °°Àº°É·ç ºÁ¾ßÇմϱî?
¸¸ÀÏ °°Àº°Å¶ó¸é ±»ÀÌ sizeof(int)ÀÌ·±°Å ¾È¾²°í ¹Ù·Î malloc( 12 )
ÀÌ·±½ÄÀ¸·Î ½áµÎ µÇ´Â°Å ¾Æ´ÑÁö?±»ÀÌ sizeof( int )ÀÌ·±½Ä¾È½áµÎµÇ´Â°Å
¾Æ´Õ´Ï±î??? */

  Hit : 3947     Date : 2003/11/12 01:45



    
ckw80 count°¡ 0ÀÌ¸é ¸Þ¸ð¸®ÇÒ´çµÎ 0ÀÌÁ® Áï null ÀÔ´Ï´Ù. 2003/11/20  
  C°øºÎÇÏ´Ù ±Ã±ÝÇÑ°Ô À־¿ä~´äº¯Á»ÇØÁÖ¼¼¿ä^^[1]     ypkhj1
11/12 3946
3025       [re] ´Ù½Ã!! Ãß°¡ Áú¹®ÀÔ´Ï´Ù~[16]     yyjg
11/12 4026
3024     [re] C°øºÎÇÏ´Ù ±Ã±ÝÇÑ°Ô À־¿ä~´äº¯Á»ÇØÁÖ¼¼¿ä^^     hkpco
11/13 3604
3023   ¼Ò½º ºÎºÐÁß¿¡¿ä..[6]     shtjdanr
11/13 4014
3022   ÁøÂ¥ ±ÞÇÑ ·¹º§5 Áú¹®[1]     shtjdanr
11/13 4072
3021   Ȥ½Ã ÀÌ·±¾ð¾î ÀÚ·á ÀÖ³ª¿ä?[1]     pskkmj
11/14 3710
3020   ±³Âø»óÅ°ü·Ã[6]     pskkmj
11/14 3853
3019   c Áú¹®ÀÌ¿©~ Á¦¹ß ´äº¯ Á» ÇØÁÖ¼¼¿©~[6]     pmac2
11/16 3469
3018   ¾Æ ¹ÌÄ¡°Ú½¿µÂ -_-; ¿©±â¼­ ob1°´Ã¼°¡ ob2·Î ġȯµÇ¸é ¾î¶²¹®Á¦Á¡ÀÌ ÀϾ³ª¿ä?[16]     pmj0914
11/16 4213
3017   gcc »ç¿ë¹ýÁ¡¿©[8]     cosmos9541
11/18 4202
3016   msys ¿¡¼­ ÄÄÆÄÀϽà ¹®Á¦[3]     kiyuchi
11/19 3558
3015   À©µµ¿ì¿ë gcc³ª À©µµ¿ì¿ëcÄÄÆÄÀÏ·¯°¡ ÀÕ³ª¿ä???Åͺ¸¾¾¸»°í¿©[6]     Ä«ÀÌÀú9
11/19 5626
3014     [re] msys ¿¡¼­ ÄÄÆÄÀϽà ¹®Á¦     kiyuchi
11/20 3595
3013   ÄÄÆÄÀÏ ÇÏ´Â ¹æ¹ýÀÌ¿ä     sepironyel
11/21 3196
3012     [re] C°øºÎÇÏ´Ù ±Ã±ÝÇÑ°Ô À־¿ä~´äº¯Á»ÇØÁÖ¼¼¿ä^^     xozu500
11/22 3600
3011   ÇØÄð¾¾¾ð¾îÃ¥À¸·Î °øºÎÇÏ´Ù°¡....[4]     È­ÀÌÆ®ÆØ
11/22 3672
3010   quick_sortÀ©...ÁÖ¼®Á» ´Þ¾ÆÁÖ½É ¾ÈµÇ³ª¿ä??     haekiller
11/22 3710
3009   µµ¿ÍÁÖ¼¼¿ä....ÇÁ·Î±×·¡¹Ö ¹®Á¦...[2]     Keres
11/23 3578
3008   ÇØÄð C¾ð¾î ±³Àç 65ÂÊ¿¡¼­¿ä..½Ç¼öÇü¿¡ ´ëÇÑ°Å ¸»Àä..[6]     jjualegi
11/23 3731
3007   execlÁ» ¾Ë·ÁÁÖ¼¼¿ä[1]     This_Time
11/23 3577
[1][2][3][4][5][6][7][8][9] 10 ..[161]

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