ÇÁ·Î±×·¡¹Ö

 3206, 1/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 : 3949     Date : 2003/11/12 01:45



    
ckw80 count°¡ 0ÀÌ¸é ¸Þ¸ð¸®ÇÒ´çµÎ 0ÀÌÁ® Áï null ÀÔ´Ï´Ù. 2003/11/20  
3206   SNSÇØÅ·ÀÌ µÇ³ª¿ë? µµ¿ÍÁÖ¼¼¿ä ¤Ð¤Ð¤Ð [2]     ÇÑä¹Î
02/28 333
3205   Ä¡Æ®¿£Áø °í¼öºÐ °è½Ç±î¿ä[1]     rjsdudals123
02/15 186
3204   ¿À·ù Á» ã¾ÆÁÖ¼¼¿ä [1]     marunim
05/30 918
3203 ºñ¹Ð±ÛÀÔ´Ï´Ù  124     minsub87
08/17 1
3202   c¾ð¾î segmentation fault:11 ¿À·ù Áú¹®µå¸³´Ï´Ù![2]     leebk1124
05/21 2007
3201   C++ÇÔ¼ö°ü·Ã Áú¹®ÀÌ¿¡¿ë!![3]     1999dylee
05/11 1841
3200   ÆÄÀ̽ã Áö¹® µå¸³´Ï´Ù.[1]     kksh1107
04/24 1581
3199   ¸®¹ö½ÌÀÇ ¼¼¹ø¤Š ²É - ¿ª¶û-     nninni79
04/20 2286
3198   ´Þ°í³ª ¹®¼­ ½©ÄÚµå[1]     ghjk645
03/24 1604
3197 ºñ¹Ð±ÛÀÔ´Ï´Ù  c¾ð¾î ¼Ò¼ö °ª     adwefq
04/29 0
3196   C¾ð¾î ¼Ò½ºÁú¹®ÀÔ´Ï´Ù![5]     an0088
01/05 5117
3195   C++ /// ºôµå ¿¡·¯ ¤Ð¤Ð[1]     guichanta
08/23 2394
3194 ºñ¹Ð±ÛÀÔ´Ï´Ù  c¾ð¾î µµ¿ÍÁÖ¼¼¿ä¤Ð¤Ð     su6339
04/06 0
3193   ÇØÅ·À» ¹è¿ì·Á°íÇϴµ¥[3]     zoodem04
03/26 4179
3192   c¾ð¾î¸¦ ¹è¿ì°í½Í¾î¿ä ![7]     dwc07238
02/11 4061
3191   ½ºÅÃÀÌ ½×ÀÌ´Â ¹æÇâ¿¡ °üÇؼ­!![1]     hackxx123
12/10 3512
3190   ÇØÅ·Åø¿¡¼­ ip ¾øÀÌ Çϵ忡 ½É´Â°Å¿¡´Â ¾î¶²°Ô ÀÖÀ»±î¿ä?[2]     aowlrgmlals
11/27 4273
3189   C¾ð¾î Çϴµ¥ double ½Ç¼ö¸¦ ³ÖÀºµ¥ ÀÚ²Ù 0.0ÀÌ ³ª¿Í¿ä[2]     fatou10336
11/20 3725
3188   dumpcode.h ÀÌÇØÁ» µµ¿ÍÁÖ¼¼¿ä .[1]     cm6418
11/06 3703
3187   c¾ð¾î ¾Ë°í¸®Áò Áú¹®ÀÔ´Ï´Ù.[3]     alstn8150
10/12 3729
1 [2][3][4][5][6][7][8][9][10]..[161]

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