我以我所拥有的所有正确理解编写了这段代码。请检查我的问题。
#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");
}
这个程序给出了这些错误:
最佳答案
在 C 中,结构标签和类型名称存在于不同的 namespace 中。也就是说struct RanElmt
和RanElmt
是两种不同的类型,另外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/