-3

So I'm writing a program which uses double linked lists and Nodes to traverse them while preforming simple mathematical operations and add/remove type stuff.

We've gone through the code and algorithms over and over again trying to find logical issues but there is nothing we can see. When we go to build, it shows about 43 errors mostly "dereferencing" issues around where I have the temp nodes used to traverse the list. We have a .c file and a .h file.

The typedef structs are defined in the .h and #include in the .c. We just cannot figure out where the issue is. I have the code linked in this pastebin: 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;
            }

}

This is a sample function which this program is made from. Where the dereferencing issue comes in is when the variable 'NewNode' points to next or previous. I can't really figure out what the actual problem is, since all the typedefs are listed in the .h.

Sample typedef from my code:

typedef struct _DNode{

Object data;

struct _DNode* prev;

struct _DNode* next;

} DNode;
  • 2
    Please post your code in the question, rather than linking to external sites. Make an effort to remove any code which is not relevant, i.e. post the smallest code you can post which still demonstrates the problem. – M.M Jun 27 '14 at 02:30
  • If you could include the pertinent definitions, and show us exactly what the errors are, we'd more easily be able to help you. – jwismar Jun 27 '14 at 02:30
  • You will avoid downvotes by posting the salient parts of the code. I'll go take a look, but posting your code here will save you grief as Matt McNabb advised. – David C. Rankin Jun 27 '14 at 02:31
  • Yeah sorry I wasnt expecting quick replies so I was still editing it a bit. – user3781510 Jun 27 '14 at 02:36
  • Can you please post the compiler error? I suspect in the line "typedef struct DoubleLinkedList mainList;". You try to define a variable, remove the typedef. – eyalm Jun 27 '14 at 03:15
  • The only error we get at any instance a pointer is used or operated with is "dereferencing pointer to incomplete type". For instance, every line in the code I included above within the while loop is an error line. Along with the brackets themselves. – user3781510 Jun 27 '14 at 03:30
  • `struct DNode* newNode = calloc(list, sizeOf(newElement));` : No piece of correctness. – BLUEPIXY Jun 27 '14 at 08:36

1 Answers1

1

The biggest problem you have is with your continued inclusion of struct before DoubleLinkedList typedef which causes problems. The same problem occurs with DNode. It permeates the code. You need to read typedef struct vs struct definitions As far as the code goes, you need to revisit all of your calloc calls. It is rather unclear what you are trying to do (yes I know you are allocating either DoubleLinkedList or DNode, but what you are attempting isn't correct.

That being so, I can compile your doublelinkedlist code. There is no short way to show the changes other than just providing a diff. (created with diff -uNrb) This will get you started:

--- 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;

        }

Also, unless you have a huge need to include both DoubleLinkedList and DNode (with void data types), you are much better served with a simple list. What you are doing is valid, but doing it this way makes debugging much more difficult. For a simple double linked list example, see: Doubly Linked List (with C..). It is a fair example.

Community
  • 1
  • 1
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85