C언어

[-구름-] 배열로 큐 구현하기

by [SST]구름 posted Jun 13, 2009
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄

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

int front, rear;
int queue[5];
enum {PUT = 1, GET, INIT, PRINT, EXIT};

void init_queue()
{
     front = rear = 0;
}

int put(int nPutNum)
{
     int state = 1;
    
     if(rear > 4){
     printf("OVERFLOW n");
     state = 0;
     }

     else{
     queue[rear++] = nPutNum;
    
     printf("PUTn");
     }
     return state;
}

int get()
{
    int nGetNum = 0;
   
    if(front > 4){
             printf("UNDERFLOW n");
             nGetNum = -1;
    }
   
    else{
    nGetNum = queue[front++];
   
    printf("GETn");
}
    return nGetNum;
}

void printQueue()
{
     int i;
    
     printf("front -> rear n");
    
     for( i = front; i<rear; i++)
          printf("%d  ", queue[i]);
    
     puts("");
}

void printMenu()
{
     printf("SELECT : 1)PUT 2)GET 3)INIT 4)PRINT 5)EXIT n");
}

int Selection()
{
    int nSelection;
    scanf("%d", &nSelection);
    return nSelection;
}

main()
{
      int nSelect = 0;
      int nPutNum = 0;
     
      while(1)
      {
              printMenu();
              nSelect = Selection();
             
              switch(nSelect)
              {
                             case PUT :
                                  printf("Input Num : ");
                                  nPutNum = Selection();
                                  puts("");
                                  put(nPutNum);
                                  break;
                            
                             case GET :
                                  get();
                                  break;
                                 
                             case INIT :
                                  init_queue();
                                  break;
                            
                             case PRINT :
                                  printQueue();
                                  break;
                                 
                             case EXIT :
                                  exit(-1);
                                 
                             default :
                                     printf("Wrong Number!! n");
                                     break;
              }
      }
}
             

 

흐음...이번엔 배열을 이용해 큐를 구현해 봤습니다...

이번에도 틀린것이 있다면 정중하게 태클~