c 언어 구현 두 갈래 더미

6995 단어
두 갈래 더미 생성:
priorityqueue create(int max)
{
    priorityqueue h;
    h = (priorityqueue)malloc(sizeof(struct heap));
    h->data = (*int)malloc(sizeof(int)*(max+1));
    h->data[0] = INT_MIN;// 
    h->size = 0;
    h->capacity = max;
    return h;

 } 

삽입 작업:
void insert(priorityqueue h,int data)
{
    // 
    if(h->size==h->capacity) 
    {
        printf("full!
"
); return ; } h->size++; int i; for(int i = h->size;h->data[i/2]>data;i/=2) { h->data[i] = h->data[i/2]; } h->data[i] = data; }

삭제 작업:
int  deleted(priorityqueue h,int data)
{
    if(h->size==0) 
    {
        printf("failed
"
); return 0 ; } int lastdata = h->data[h->size--]; int mindata = h->data[1]; int i,child; for(int i =1 ; i*2<=h->size ; i =child) { // i child = 2*i; // if(child!=h->size&&h->data[child]>h->data[2*i+1]){ child++; } if(lastdata > h->data[child]) { h->data[i] =h->data[child]; } else break; } h->data[i] = lastdata; return mindata; }

전체 코드:
#include
#include
#include 

typedef struct heap* priorityqueue;

struct heap{
    int *data;
    int size;//  
    int capacity;//  
};



priorityqueue create(int max)
{
    priorityqueue h;
    h = (priorityqueue) malloc (sizeof(struct heap));
    h->data = (int*)malloc(sizeof(int)*(max+1));
    h->data[0] = INT_MIN;// 
    h->size = 0;
    h->capacity = max;
    return h; 
}
void insert(priorityqueue h,int data)
{
    // 
    if(h->size==h->capacity) 
    {
        printf("full!
"
); return ; } h->size++; int i; for(int i = h->size;h->data[i/2]>data;i/=2) { h->data[i] = h->data[i/2]; } h->data[i] = data; } int deleted(priorityqueue h,int data) { if(h->size==0) { printf("failed
"
); return 0 ; } int lastdata = h->data[h->size--]; int mindata = h->data[1]; int i,child; for(int i =1 ; i*2<=h->size ; i =child) { // i child = 2*i; // if(child!=h->size&&h->data[child]>h->data[2*i+1]){ child++; } if(lastdata > h->data[child]) { h->data[i] =h->data[child]; } else break; } h->data[i] = lastdata; return mindata; } int main() { }

좋은 웹페이지 즐겨찾기