因此,对于这个项目,我们需要通过向下看堆栈并转发队列来检查单词是否是回文。我将所有逻辑放在一起,以及我的palindrome.c文件中的#include。所有我遇到麻烦的是在我的回文检查方法中使用(下面看到的allocPQueue)时正确定义它。
以下是摘要.c for PriorityQueue
#include "PriorityQueue.h"
PQueue* allocPQueue(uint elementSize, PQMode mode){
PQueue* pq = (PQueue*)calloc(1, sizeof(PQueue));
pq->elements = allocDList(elementSize, NULL, NULL);
//the elementSampling is to speed up search time, but it is not yet ready
//in order for it to work I cannot make a copy of DNode*, rather I should
//place the pointer of a DNode directly into the array
//I will call this a DeepDArray
pq->elementSampling = (GenericArray)allocDArray(10, sizeof(DNode*));
pq->mode = mode;
if(mode == PQMODE_STACK){
pq->priorityExtractor = &stackPriority;
}
else if(mode == PQMODE_QUEUE){
pq->priorityExtractor = &queuePriority;
}
return pq;
}
void releasePQueue(PQueue* pq){
if(pq){
if(pq->elements){
releaseDList(pq->elements);
}
if(pq->elementSampling){
releaseDArray(pq->elementSampling);
}
free(pq);
}
}
Object peekMin(PQueue* pq){
if(isEmptyPQueue(pq)){
return NULL;
}
Object data = malloc(pq->elements->elementSize);
memcpy(data, pq->elements->head->data, pq->elements->elementSize);
return data;
}
以下是我的palindrome.c文件中的代码(不包含内容):
#include "PriorityQueue.h"
bool isPalindrome(char* str, uint length);
char getPalindromeChar(char c);
PQueue* stack;
PQueue* queue;
int main(int argc, char** argv){
if(isPalindrome(argv[1], strlen(argv[1]))){
printf("%s is a palindrome\n", argv[1]);
}
else{
printf("%s is not a palindrome\n", argv[1]);
}
return 0;
}
bool isPalindrome(char* str, uint length){
//TODO
//insert your check, you are only allowed to use a Stack, a Queue, and the char
//getPalindromeChar(char) helper function
char ch;
int wordLength;
int counter;
char stackChar;
char queueChar;
bool stillPalli;
stack = allocPQueue(sizeof(char), PQMODE_STACK); <--"undefined reference to 'allocPQueue'"
queue = allocPQueue(sizeof(char), PQMODE_QUEUE); <--"undefined reference to 'allocPQueue'"
wordLength = 0;
int i;
for(i = 0; i < length; i++){ // Goes through the str array, looking char-by-char for
ch = str[i];
ch = getPalindromeChar(ch);
wordLength++;
// places them in stack and queue
add(stack,ch);
add(queue,ch);
}
stillPalli = true;
while(stillPalli && (counter < wordLength)){
stackChar = top(stack);
pop(stack);
queueChar = front(queue);
dequeue(queue);
// test for equality
if(strcmp(stackChar, queueChar) != 0){
stillPalli = false;
}
counter++;
}
}
答案 0 :(得分:0)
在PriorityQueue.h中声明你的函数allocPQueue()
:
extern PQueue* allocPQueue(uint elementSize, PQMode mode);
编译你的palindrome.c时,编译器需要知道某个地方有一个在链接时可用的函数。
extern
关键字正是这样:它填充编译器符号表,但不要尝试定义它的实际内容(汇编代码,如果是函数)。编译器将输出目标代码(palindrome.o),注释该函数仍缺少定义。它将来自另一个目标文件。
然后进入链接器。你应该传递palindrome.o和PriorityQueue.o,这样就不会在最终的可执行文件中定义任何引用。