2014-06-27 79 views
-3

因此,我正在编写一个程序,它使用双链表和节点遍历它们,同时执行简单的数学运算和添加/删除类型的东西。解引用指向不完整类型的指针不编译

我们已经经历了一遍又一遍的代码和算法,试图找出逻辑问题,但我们没有看到任何东西。当我们开始构建时,它会显示大约43个错误,这些错误主要是在我使用临时节点遍历列表的地方“解除引用”问题。我们有一个.c文件和一个.h文件。

typedef结构体在.c和.c中的#include中定义。我们无法弄清楚问题出在哪里。我在这个引擎收录联的代码:http://pastebin.com/PLT3K8kX

void insertDListElementAt(DoubleLinkedList* list, Object newElement, uint position){ 

    struct DNode* newNode = calloc(list, sizeOf(newElement)); 
    newNode->data = newElement; 

    int counter = 0; 

    struct _DNode* current = list->head; 

    while(counter < list->length){ 

        if(counter == position){ 

          current->next = newNode; 
          newNode->prev = current; 
          newNode->next = current->next; 
          current->next->prev = newNode; 

        } 
        counter++; 
        current = current->next; 
      } 

} 

这是此程序从制备的样品的功能。解除引用问题的地方在于变量'NewNode'指向next或previous。我无法弄清楚实际问题是什么,因为所有的typedef都在.h中列出。从我的代码

样品的typedef:

typedef struct _DNode{ 

Object data; 

struct _DNode* prev; 

struct _DNode* next; 

} DNode; 
+2

请将您的代码发布在问题中,而不是链接到外部网站。努力删除任何不相关的代码,即发布可以发布的最小代码,这仍然表明问题。 –

+0

如果您可以包含相关定义,并准确告诉我们错误是什么,我们可以更轻松地为您提供帮助。 – jwismar

+0

您将通过发布代码的显着部分来避免downvotes。我会去看一看,但是在这里发布你的代码将会像Matt McNabb所建议的那样为你节省麻烦。 –

回答

1

你有最大的问题是与您继续纳入structDoubleLinkedList typedef,它引起的问题。与DNode发生同样的问题。它渗透到代码中。您需要阅读typedef struct vs struct definitions就代码而言,您需要重新访问所有calloc调用。这是相当不清楚你要做什么(是的,我知道你正在分配DoubleLinkedListDNode,但你试图是不正确的。 。短的方式来显示比只是提供了一个差异等的变化(与DIFF创建-uNrb)这将让你开始:

--- DoubleLinkedList.c 
+++ DoubleLinkedList.c 2014-06-26 22:59:35.768919428 -0500 
@@ -19,13 +19,13 @@ 
#include "DoubleLinkedList.h" 


-typedef struct DNode mainTemp; 
-typedef struct DoubleLinkedList mainList; 
+DNode mainTemp; 
+DoubleLinkedList mainList; 

//1 DONE 
-DoubleLinkedList* allocDList(uint elementSize){ 
+DoubleLinkedList* allocDList (uint elementSize) { 

- struct DoubleLinkedList* list = &mainList; 
+  DoubleLinkedList* list = &mainList; 

     list->head = NULL; 
     list->tail = NULL; 
@@ -35,18 +35,16 @@ 

     return list; 

- 
- 
} 
//2 DONE 
void releaseDList(DoubleLinkedList* list){ 

- struct DNode* node = list->head; 
- struct DNode* next = NULL; 
+ DNode* node = list->head; 
+ DNode* next = NULL; 

     while(node){ 

-  struct DNode* next = node->next; 
+  DNode* next = node->next; 
       free(node); 
       node = next; 

@@ -56,12 +54,12 @@ 
//3 DONE 
void insertDListElementAt(DoubleLinkedList* list, Object newElement, uint position){ 

- struct DNode* newNode = calloc(list, sizeOf(newElement)); 
+ DNode* newNode = calloc (1, sizeof(newNode)); // allocating newNode or list? 
     newNode->data = newElement; 

     int counter = 0; 

- struct _DNode* current = list->head; 
+ DNode* current = list->head; 

     while(counter < list->length){ 

@@ -81,7 +79,7 @@ 
//4 DONE 
void appendDList(DoubleLinkedList* list, Object newElement){ 

- struct DNode* newNode = calloc(list, sizeOf(newElement)); 
+ DNode* newNode = calloc(1, sizeof(newNode)); // allocating newNode or list? 
     newNode->data = newElement; 

     newNode = list->tail->next; // setting newNode as current tail's next 
@@ -95,7 +93,7 @@ 



- struct DNode* newNode = (DNode*)calloc(list, sizeOf(newElement)); 
+ DNode* newNode = calloc(1, sizeof(newElement)); 

     newNode->data = newElement; 

@@ -109,12 +107,12 @@ 
//6 DONE 
DoubleLinkedList* reverseDList(DoubleLinkedList* list){ 

- struct DoubleLinkedList* newList = NULL; 
- newList = (struct DoubleLinkedList*) malloc(sizeOf(DoubleLinkedList)); 
+ DoubleLinkedList* newList = NULL; 
+ newList = malloc(sizeof(DoubleLinkedList)); 

- struct DNode* temp = NULL; 
+ DNode* temp = NULL; 

- temp = (DNode*)malloc(sizeOf(DNode)); 
+ temp = malloc (sizeof (DNode)); 

     temp = list->tail; 

@@ -136,9 +134,9 @@ 
//7 DONE 
DoubleLinkedList* halfList(DoubleLinkedList* list){ 

- struct DNode* slow = list->head; 
- struct DNode* fast = list->head; 
- struct DoubleLinkedList* newList = malloc(uint); 
+ DNode* slow = list->head; 
+ DNode* fast = list->head; 
+ DoubleLinkedList* newList = malloc (sizeof (uint)); 

     if(list->head != NULL){ 

@@ -166,7 +164,7 @@ 
Object removeDList(DoubleLinkedList* list, int position){ 

     int counter = 0; 
- struct _DNode* temp = list->head; 
+ DNode* temp = list->head; 

     while(counter < list->length){ 

@@ -189,11 +187,11 @@ 
//9 DONE 
void printDList(DoubleLinkedList* list){ 

- struct _DNode* temp = list->head; 
+ DNode* temp = list->head; 

     while(temp->next != NULL){ 

-  printf("%d", temp->data); 
+  printf ("%d", temp->data); 
       temp = temp->next; 

     } 

同样,除非你有一个巨大的需求包括DoubleLinkedListDNode(含无效数据类型),用一个简单的列表就可以更好地服务你正在做的是有效的,但是这样做会使得调试更加困难。 ple双重链接列表示例,请参阅:Doubly Linked List (with C..)。这是一个很好的例子。

相关问题