c - C中的随机队列

标签 c data-structures

我以我所拥有的所有正确理解编写了这段代码。请检查我的问题。

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

//为出队元素定义一个结构

这个结构很好,有数据,下一个,上一个指针。

typedef struct RanElmt_ {

    void *data;
    struct DeqElmt_ *prev;
    struct DeqElmt_ *next;

    void (*destroy)(void *data);



//Your Code here
} RanElmt;

这个也可以,根据我的看法是正确的。

typedef struct RandQ_{
    int size;
    struct RanElmt *head;
    struct RanElmt *tail; 



}RandQ;



RandQ * RandomizedQueue(void (*destroy)(void *data)){

    RandQ *relmt = (RandQ*)malloc(sizeof(RandQ));



}     // construct an empty randomized queue


int isREmpty(RandQ *rQ){
    if ( rQ->size == 0)
        return 1;
    return 0;
}                                  // is the queue empty?

int rsize(RandQ *rQ){

    return rQ->size;

}    
                             // return the number of items on the queue

实际上这只是一个函数,(入队)我将得到这个想法并编写其他函数(出队、样本等)

int enqueue(RandQ *rQ, const void *data){
    RanElmt *relmt = (RanElmt*)malloc(sizeof(RanElmt));
    relmt->data = (void*)data;
    if (rQ->head == NULL){
        relmt = rQ->head;
        relmt = rQ->tail;
        relmt->prev = NULL;
        relmt->next = NULL;
    }
    else{
        rQ->head = relmt;
    }

    (rQ->head)->prev = relmt;
    relmt->prev = rQ->head;
    rQ->head = relmt;

}                // add the item




main(){

    Deque(free);


    printf(" okk \n");
}

这个程序给出了这些错误:

Errors i'm getting

最佳答案

在 C 中,结构标签和类型名称存在于不同的 namespace 中。也就是说struct RanElmtRanElmt是两种不同的类型,另外struct RanElmt没有完全定义。

您的 RandQ 应该定义为类似

typedef struct RandQ_{
    int size;
    struct RanElmt_ *head; // or RanElmt* head;
    struct RanElmt_ *tail; // or RanElmt* tail;

}RandQ;

此外,您的 RanElmt 可能不是您想要的,也许您的意思是:

typedef struct RanElmt_ {

    void *data;
    struct RanElmt_ *prev; // pointer to a struct of the same type
    struct RanElmt_ *next; // pointer to a struct of the same type

    void (*destroy)(void *data);

   // You cannot put code here in C (or even a function definition AFAIK).
} RanElmt;

关于c - C中的随机队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33473853/

相关文章:

algorithm - 设计电话簿的数据结构

java - 字符串哈希表实现

c++ - 无法创建数组大小为 [300][300] 的二维数据结构-->堆栈溢出

java - 存储单词联想的数据结构

c - 使用指向另一个函数的指针传递数组

c - RGB888 到 RGB565/位移位

c - 如何在 C+ 中将数组中打包的 inorder(LDR) 二叉树转换回二叉树

algorithm - 需要一些关于旅行商问题表示的帮助

C tcp socket magic : clientip, 关闭端口,重新连接标准输入/标准输出,sigaction,速度

c - 意外的 pipe() 行为