I carried out the changes you suggested and although they do remove the first two errors I still get the other errors about missing ;, ) etc. They are still said to be missing in the middle of lines of code though.
Here's the new code:
Code:
#include <stdlib.h>
#include "bool.h"
// Struct Definitions
//=========================
typedef struct node node
{
// node *contents
void *item;
node *next, *prev;
// utility methods
void (*insertBetween) (node *THIS, node *prev, node *next);
void (*remove) (node *THIS);
};
typedef struct linkedList linkedList
{
// list contents
node *head, *tail;
int size;
// add to list methods
void (*addToTail) (linkedList *THIS, void *item);
void (*addToHead) (linkedList *THIS, void *item);
void (*addAtIndex) (linkedList *THIS, void *item, int index);
// remove from list methods
void (*removeByIndex) (linkedList *THIS, int index);
void (*removeByItem) (linkedList *THIS, void *item);
// utility methods
int (*indexOf) (linkedList *THIS, void *item);
node* (*nodeOf) (linkedList *THIS, void *item)
node* (*nodeAtIndex) (linkedList *THIS, int index);
boolean (*contains) (linkedList *THIS, void *item);
boolean (*isEmpty) (linkedList *THIS, void *item);
};
// Method Definitions
//=========================
// node *constructor
node* newNode(void *item);
// linked list constructor
linkedList* newLinkedList();
// Method Implementations
//=========================
// INTERNAL NODE METHODS
// insertion
void insertBetween (node *THIS, node *prev, node *next)
{
THIS->prev = prev;
THIS->next = next;
prev->next = THIS;
next->prev = THIS;
}
void remove (node *THIS)
{
THIS->prev->next = THIS->next;
THIS->next->prev = THIS->prev;
THIS->next = NULL;
THIS->prev = NULL;
}
// INTERNAL LINKED LIST METHODS
// add methods
void addToTail (linkedList *THIS, void *item)
{
node *add = newNode(item);
add->insertBetween(add, THIS->tail->prev, THIS->tail);
THIS->size = THIS->size + 1;
}
void addToHead (linkedList *THIS, void *item)
{
node *add = newNode(item);
add->insertBetween(add, THIS->head, THIS->head->next);
THIS->size = THIS->size + 1;
}
void addAtIndex (linkedList *THIS, void *item, int index)
{
if (index == 0)
addToHead(THIS,item);
else
{
node *cur = nodeAtIndex(index-1);
node *add = newNode(item);
add->insertBetween(add,cur,cur->next);
}
}
// remove methods
void removeByIndex (linkedList *THIS, int index)
{
node *cur = nodeAtIndex(index);
cur->remove(cur);
free(cur);
}
void removeByItem (linkedList *THIS, void *item)
{
node *cur = nodeOf(item);
cur->remove(cur);
free(cur);
}
// utility methods
int indexOf(linkedList *THIS, void *item)
{
int i=0;
node *cur=THIS->head->next;
while (i < index && cur->item != item)
{
cur = cur->next;
i++;
}
if (cur->item == item)
return i;
return -1;
}
node* nodeOf(linkedList *THIS, void *item)
{
int i=0;
node *cur=THIS->head->next;
while (i < index && cur->item != item)
{
cur = cur->next;
i++;
}
if (cur->item == item)
return cur;
return NULL;
}
node* nodeAtIndex(linkedList *THIS, int index)
{
if (index >= size)
return NULL;
int i=0;
node *cur=THIS->head->next;
while (i < index)
{
cur = cur->next;
i++;
}
return cur;
}
boolean contains(linkedList *THIS, void *item)
{
return ( indexOf(THIS, item) != -1 );
}
boolean isEmpty(linkedList THIS)
{
return (head->next == tail);
}
// CONSTRUCTORS
// node *constructor
node* newNode(void *item)
{
// memory allocation
node *THIS = (node) (malloc( sizeof(node_t) ));
// fields
THIS->next = THIS;
THIS->prev = THIS;
THIS->item = THIS;
// methods
THIS->insertBetween = insertBetween;
THIS->remove = remove;
//return
return THIS;
}
// list constructor
linkedList* newLinkedList()
{
// memory allocation
linkedList *THIS = (linkedList) (malloc( sizeof(linkedList_t) ));
// fields
THIS->head = newNode(NULL);
THIS->tail = newNode(NULL);
head->next = THIS->tail;
tail->prev = THIS->head;
// methods
// add methods
THIS->addToTail = addToTail;
THIS->addToHead = addToHead;
THIS->addAtIndex = addAtIndex;
// remove methods
THIS->removeByIndex = removeByIndex;
THIS->removeByItem = removeByItem;
// utility methods
THIS->indexOf = indexOf;
THIS->nodeOf = nodeOf;
THIS->nodeAtIndex = nodeAtIndex;
THIS->contains = contains;
THIS->isEmpty = isEmpty;
//return
return THIS;
}
And here's the new errors:
Code:
lib/linkedlist.h:8: error: expected =, ,, ;, asm or __attribute__ before { token
lib/linkedlist.h:20: error: expected =, ,, ;, asm or __attribute__ before { token
lib/linkedlist.h:47: error: expected =, ,, ;, asm or __attribute__ before * token
lib/linkedlist.h:50: error: expected =, ,, ;, asm or __attribute__ before * token
lib/linkedlist.h:58: error: expected ) before * token
lib/linkedlist.h:65: error: expected ) before * token
lib/linkedlist.h:76: error: expected ) before * token
lib/linkedlist.h:82: error: expected ) before * token
lib/linkedlist.h:88: error: expected ) before * token
lib/linkedlist.h:101: error: expected ) before * token
lib/linkedlist.h:107: error: expected ) before * token
lib/linkedlist.h:115: error: expected ) before * token
lib/linkedlist.h:128: error: expected =, ,, ;, asm or __attribute__ before * token
lib/linkedlist.h:141: error: expected =, ,, ;, asm or __attribute__ before * token
lib/linkedlist.h:154: error: expected ) before * token
lib/linkedlist.h:158: error: expected ) before THIS
lib/linkedlist.h:166: error: expected =, ,, ;, asm or __attribute__ before * token
lib/linkedlist.h:186: error: expected =, ,, ;, asm or __attribute__ before * token
Originally Posted by
laserlight
Those are function pointer declarations. That said, pbtrn10k might be intending to simulate OO later, otherwise it seems to me that those declarations would not be useful.
The object of the lectures in question was to show how OO can be simulated in a non-OO language like C. That said, I'm doing this to learn and because our lecturer has a reputation for putting code in his handouts which doesn't compile, so I want to see if he was actually telling the truth!!