2014-06-27 61 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..)。這是一個很好的例子。

相關問題