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.