ÇÁ·Î±×·¡¹Ö

 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 : 3940     Date : 2003/11/12 01:45



    
ckw80 count°¡ 0ÀÌ¸é ¸Þ¸ð¸®ÇÒ´çµÎ 0ÀÌÁ® Áï null ÀÔ´Ï´Ù. 2003/11/20  
3026   ¸®´ª½º c¾ð¾î¿Í ¸¶ÀÌÅ©·Î c++¿¡¼­ÀÇ c¾ð¾î Áú¹® ÀÖ½À´Ï´Ù...[10]     superio1999
11/08 4423
3025     [re] ¸®´ª½º c¾ð¾î¿Í ¸¶ÀÌÅ©·Î c++¿¡¼­ÀÇ c¾ð¾î Áú¹® ÀÖ½À´Ï´Ù...     superio1999
11/08 3826
3024   ÇØÄðC¾ð¾î Ã¥Áß..[7]     eiichiro
11/08 3910
3023     [re] ÇØÄðC¾ð¾î Ã¥Áß..     lottie
11/10 3581
3022     [re] ÇØÄðC¾ð¾î Ã¥Áß.. ¼öÁ¤ÇÒ °÷.     ¹ß¼Û´ã´çÀÚ
11/10 4052
3021   ÆÄÀÏÀÔÃâ·Â¿¡°üÇÑ Áú¹®ÀÌ¿¹¿ä~[2]     yyjg
11/08 3792
3020   c¾ð¾î ÷ ¼Õ´ëº½...´Ï´Ù...¤Ñ¤Ñ;;;[3]     limsangseop
11/09 3903
3019   Ãʺ¸ÀÔ´Ï´Ù....ÇØÅ·¿¡ÇÊ¿äÇÑ ¾ð¾î¿¡ ´ëÇÏ¿©...[3]     rytas
11/10 4215
3018   ÇØÄð c¾ð¾î 186ÂÊ ¼Ò½º Áú¹®ÀÔ´Ï´Ù.[1]     na9876
11/10 4236
3017     [re] ÇØÄð c¾ð¾î 186ÂÊ ¼Ò½º Áú¹®ÀÔ´Ï´Ù.     indra
11/11 4833
3016   ¹®ÀÚ¿­ ó¸®ÇÏ´Â °Í¿¡ ´ëÇØ Áú¹®ÀÌ ÀÖ¾î¿ä     yyjg
11/10 4055
3015     [re] ¹®ÀÚ¿­ ó¸®ÇÏ´Â °Í¿¡ ´ëÇØ Áú¹®ÀÌ ÀÖ¾î¿ä[1]     indra
11/11 4091
3014       [re] ´Ù½Ã!! Ãß°¡ Áú¹®ÀÔ´Ï´Ù~[16]     yyjg
11/12 4019
3013     [re] C°øºÎÇÏ´Ù ±Ã±ÝÇÑ°Ô À־¿ä~´äº¯Á»ÇØÁÖ¼¼¿ä^^     hkpco
11/13 3599
  C°øºÎÇÏ´Ù ±Ã±ÝÇÑ°Ô À־¿ä~´äº¯Á»ÇØÁÖ¼¼¿ä^^[1]     ypkhj1
11/12 3939
3011     [re] C°øºÎÇÏ´Ù ±Ã±ÝÇÑ°Ô À־¿ä~´äº¯Á»ÇØÁÖ¼¼¿ä^^     xozu500
11/22 3592
3010   ¼Ò½º ºÎºÐÁß¿¡¿ä..[6]     shtjdanr
11/13 4004
3009   ÁøÂ¥ ±ÞÇÑ ·¹º§5 Áú¹®[1]     shtjdanr
11/13 4064
3008   Ȥ½Ã ÀÌ·±¾ð¾î ÀÚ·á ÀÖ³ª¿ä?[1]     pskkmj
11/14 3703
3007   ±³Âø»óÅ°ü·Ã[6]     pskkmj
11/14 3845
[1][2][3][4][5][6][7][8][9] 10 ..[161]

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