2013-10-21 2 views
-2

처음으로 3의 크기에 도달하면 크기가 3만큼 동적으로 증가하는 큐를 만들려고합니다. 나는 malloc을 사용하거나 많은 realloc 함수,하지만 코드에서 정확해야 AFAIKC 동적으로 증가하는 큐

**Header:** 
#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
#include <string.h> 

typedef char Titem; 

/*The interface of queue */ 
#define MAXN 3 
typedef enum {NOT_OK, OK } Tboolean; 
typedef struct { 
     Titem array[MAXN]; 
     int number_of_items; 
} Tqueue; 
void initialize_queue (Tqueue *Pqueue); 
Tboolean enqueue(Tqueue *p, Titem item); 
Tboolean dequeue(Tqueue *p, Titem *Pitem); 
void print_queue(const Tqueue *Pqueue); 

**Queue functions:** 
#include "jono.h" 

void initialize_queue (Tqueue *Pqueue) { 

    int size = 0; 

    Pqueue->number_of_items = 0; 
    *Pqueue->array = (Titem) calloc (MAXN, sizeof(MAXN)); 
    size = sizeof (Pqueue->array); 
    printf ("%d\n", size); 

} 
Tboolean enqueue(Tqueue *Pqueue, Titem item) { 

    int size = 0; 

    if (Pqueue->number_of_items >= MAXN) { 
     *Pqueue->array = (Titem) realloc (Pqueue->array, sizeof(Pqueue->array) + MAXN); 
     size = sizeof (Pqueue->array); 
     printf ("\%d", size); 

     Pqueue->array[Pqueue->number_of_items++] = item; 
     return(OK); 
    } 
    else { 
     Pqueue->array[Pqueue->number_of_items++] = item; 
     return (OK); 
    } 
} 
Tboolean dequeue(Tqueue *Pqueue, Titem *Pitem) { 
    int i; 
    if (Pqueue->number_of_items == 0) 
     return(NOT_OK); 
    else { 
     *Pitem = Pqueue->array[0]; 
     for (i = 0 ; i < Pqueue->number_of_items-1 ; i++) 
      Pqueue->array[i] = Pqueue->array[i+1]; 
     Pqueue->number_of_items--; 
     return (OK); 
    } 
} 
void print_queue (const Tqueue *Pqueue) { 
    int i; 
    printf("\nQueue now: \n\n"); 
    for (i = 0 ; i < Pqueue->number_of_items ; i++) { 
     printf(" %c ", Pqueue->array[i]); 
    } 
    printf("\n\n"); 
} 

**Main:** 
#include "jono.h" 

int main(void) { 
    Tqueue queue; 
    Tboolean succeed; 
    char chr; 

    initialize_queue(&queue); 
    printf("\nEnter a letter to be queued "); 
    printf("\nor digit 1 to dequeue a letter"); 
    printf("\nor Return to quit a program\n"); 

    chr = _getche(); 
    while (chr != 10 && chr != 13) { 
     if (isalpha(chr)) { 
     succeed=enqueue(&queue, chr); 
     print_queue(&queue); 
     if (!succeed) 
      printf("\n Enqueue operation failed\n"); 
     } 
     if (chr == '1') { 
     succeed = dequeue(&queue, &chr); 
     if (succeed) { 
      printf("\na letter dequeued %c ", chr); 
      print_queue(&queue); 
      } 
     else printf("\nDequeue operation failed\n "); 
     } 
     chr = _getche(); 
    } 
} 
+0

그리고 (포인터 만이 (m/c/re)alloc을 사용할 수 있습니다) 수하지 않은 당신의 질문은 무엇입니까? – Sinkingpoint

+0

realloc이 일어날 때 힙 오류가 발생합니다 – user2735934

+0

오류 메시지를 게시하십시오. – user2339071

답변

1
typedef struct { 
     Titem array[MAXN]; /* char array[3] */ 
     int number_of_items; 
} Tqueue; 

*Pqueue->array = (Titem) calloc (MAXN, sizeof(MAXN)); 

당신은 배열하지 예약 공간

+0

Doh, 쉬운 해결책. 고맙습니다! – user2735934

+0

앞으로 더 많은 도움을 얻으려면 코드를 단일 파일에 저장하고 비표준 함수 (예 :'_getche' 대신에'getchar' 사용)를 사용하지 마십시오. –