To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.legosOpen lugnet.robotics.rcx.legos in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / legOS / 1835
1834  |  1836
Subject: 
Dynamic lists allowed?
Newsgroups: 
lugnet.robotics.rcx.legos
Date: 
Thu, 12 Apr 2001 22:22:27 GMT
Viewed: 
1257 times
  
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. (23 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
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR