|
Since I just got started with legOs (thanks to 'Extreme Mindstorms'), I'm
creating a set of high level utility functions. This one here is intended
to display a DYNAMIC list of values, but there seems to be a problem with
the use of 'struct' and pointers.
Have I gone too far? Some of the kernel modules use pointers...
If you're interested, this is a circular list which is constantly being
displayed by a thread, while the main process keeps adding entries to the list.
#include<stdlib.h>
#include<unistd.h>
#include<conio.h>
#include<string.h>
#include<rom/lcd.h>
struct tNode {
char text[6];
int value;
tNode *next;
} list;
void initList () {
strcpy(list.text,"BEGIN");
list.value = 0;
list.next = &list;
}
void destroyList() {
tNode *this;
while (list.next != &list) {
this = list.next;
list.next = this->next;
free(this);
}
}
void addToList (char *pText, int pValue) {
tNode *newNode = NULL;
newNode = (tNode *)calloc(1, sizeof(tNode));
strcpy(newNode->text, pText);
newNode->value = pValue;
newNode->next = list.next;
list.next = newNode;
}
int displayList (int n, char **p) {
tNode *thisNode;
thisNode = &list;
while (1) {
cls()
cputs(thisNode->text);
sleep(1);
lcd_int(thisNode->value);
sleep(1);
thisNode = thisNode->next;
}
return 0;
}
int main (int n, char **p) {
pid_t displayThread;
initList();
displayThread = execi (&displayList,0,0,PRIO_LOW,DEFAULT_STACK_SIZE);
sleep(4);
addToList("ONE",1);
addToList("TWO",2);
sleep(10);
addToList("MORE",8);
sleep(5);
addToList("DONE",14789);
sleep(20);
kill(displayThread);
destroyList();
return 0;
}
|
|
Message has 1 Reply: | | Re: Dynamic lists allowed?
|
| (...) This should be "struct tNode *next;" (...) After this, any time you want to refer to a tNode, you need to say "struct tNode". Or you can put "typedef struct tNode tNode" and then use the "tNode" as a type like you're doing. (24 years ago, 12-Apr-01, to lugnet.robotics.rcx.legos)
|
3 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|