C 动态增加队列

标签 c memory dynamic queue realloc

我正在尝试创建一个队列,当它第一次达到 3 的大小时,它的大小会动态增加 3。我没有太多使用 malloc 或 realloc,但据我所知,它们在代码中应该是正确的

**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();
     }
}

最佳答案

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

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

不能为数组保留空间(只有指针可以使用(m/c/re)alloc)

关于C 动态增加队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19490651/

相关文章:

java - 将值添加到具有固定长度的 Java 数组

c - 如何在不包含 stdio.h 的情况下使用 scanf()

c - 使用结构填充

c++ - 需要帮助理解从 B.Stroustrup 的新书中摘录的这段文字

forms - JSF 中的动态表单生成

python - 为什么在将属性更改为引用外部函数后不传递 self?

c - 相同翻转三角形的可能解决方案

c - 找到满足模数的最小值

node.js - 什么是 node.js 内存故障?

xml - valgrind 日志文件中缺少泄漏摘要