dynamische container

algemene C code
Berichten: 49
Geregistreerd: 09 Mei 2015, 15:57

dynamische container

Berichtdoor doebi » 28 Jan 2017, 18:16

Hallo,

Heeft c++ voor arduino een container class waarmee ik
1) gegevens kan op plaatsen, onder een bepaalde naam of identificatie nr
2) gegevens kan terughalen dmv die naam of identificatie nr
3) gegevens kan wissen dmv die naam of identificatie nr
4) een functie waarmee ik de volledige container kan doorlopen , zonder de naam of identificatie nr te gebruiken ?

Visual basic heeft zoiets, waarmee je dan met een foreach de volledige container kan doorlopen , maar in c++ weet ik het niet zo hoe dit aan te pakken.

M'n bedoeling is voor m'n projectje ( zie http://www.arduinoforum.nl/viewtopic.php?f=9&t=2326 ) de gegevens voor de touchbuttons in dergelijke container op te slaan
en zo het programma overzichtelijk te houden. Al de gevens uit de class button zou ik in een struct zetten, deze structs moeten dan in die container.

Ik werk hiervoor liefst niet met array's daar ik het aantal elementen dynamisch wil maken.

Grt

Advertisement

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: dynamische container

Berichtdoor nicoverduin » 28 Jan 2017, 20:45

Je zou eens naar een linked list kunnen kijken https://github.com/ivanseidel/LinkedLis ... /README.md
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 49
Geregistreerd: 09 Mei 2015, 15:57

Re: dynamische container

Berichtdoor doebi » 29 Jan 2017, 00:34

Al een en ander getest,
al weet ik nog niet goed hoe het geheugen terug vrij te geven. :roll:

Dit werkt al :
Code: Alles selecteren
// First attempt to make a dynamic data container
//
//
//
// 28/01/2017



class strHeap {
  public :
  strHeap  *Parent = NULL;              // pointer to the previos data in the container
  strHeap  *Child = NULL;               // pointer to the next data in the container
  byte      Data;                       // the data
};

strHeap *HeapCurrent;                   // global var, used to point to the current used data in the container
strHeap *HeapStart;                     // global var, used to point to the first data in the container (for the moment no use)
strHeap *HeapEnd;                       // global var, used to point to the last data in the container

void PushHeap(byte aByte) {
  HeapCurrent = HeapEnd;                //  start with pointing the pointer to the current data to the last data in the container
  strHeap *NewElement = new strHeap();  //  make a new dynamic data
  NewElement->Data = aByte;             //  assign the data
  NewElement->Parent = HeapCurrent;     //  point the parent of the new data to the current data
  NewElement->Child = NULL;             //  no child yet
  HeapCurrent = NewElement;             //  make the current pointer to the new pointer
  HeapEnd = NewElement;                 //  make the end pointer the new pointer
}

void HeapPrint() {                     
  Serial.println("Begin");
  HeapCurrent = HeapEnd;
  while (HeapCurrent->Parent != NULL) {
    Serial.println(HeapCurrent->Data);
    HeapCurrent = HeapCurrent->Parent;
  }
 
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Setup");
  HeapCurrent = new strHeap();
  HeapStart = HeapCurrent;
  HeapEnd = HeapCurrent;
  PushHeap(1);
  PushHeap(2);
  PushHeap(50);

  HeapPrint();

  PushHeap(25);

  HeapPrint();

  PushHeap(17);
  PushHeap(11);

  HeapPrint();

}

void loop() {
  // put your main code here, to run repeatedly:

}

Berichten: 49
Geregistreerd: 09 Mei 2015, 15:57

Re: dynamische container

Berichtdoor doebi » 29 Jan 2017, 19:09

Met volgende kan ik intussen elementen in een container plaatsen, de lijst doorlopen dmv findfirst en findnext , elementen uit de container halen, en het geheugen vrijgeven.
Moet ik enkel nog de eigenlijke data (een struct) ook nog bijprogrammeren

cpp code
// First attempt to make a dynamic data container
//
//
//
// 29/01/2017



class clsElement {
public :
clsElement *Parent = NULL; // pointer to the previos data in the container
clsElement *Child = NULL; // pointer to the next data in the container
int ElementNr; // the element nr to index to (remark :: there is no check for duplicates (yet) )
ptr *Data = NULL;
int DataSize;
};

clsElement *ElementCurrent; // global var, used to point to the current used element in the container
clsElement *ElementFirst; // global var, used to point to the first element in the container
clsElement *ElementLast; // global var, used to point to the last element in the container

clsElement *HeapPush( int aElementNr) {
// push a new element on in the container
// sets global ElementCurrent to the new element
// returns ElementCurrent

if (ElementCurrent == NULL) { // nothing in the container yet
ElementCurrent = new clsElement(); // make a new dynamic element
ElementCurrent->Parent = NULL; // set its parent to null
ElementCurrent->Child = NULL; // set its child to null
ElementCurrent->ElementNr = aElementNr; // set ist data
ElementFirst = ElementCurrent; // refer this new element as first element
ElementLast = ElementCurrent; // refer this new element as last element
}
else
{ // there is already something in the container
ElementCurrent = ElementLast; // start with pointing the pointer to the current data to the last data in the container
clsElement *NewElement = new clsElement(); // make a new dynamic data
NewElement->ElementNr = aElementNr; // assign the data
NewElement->Parent = ElementCurrent; // point the parent of the new data to the current data
ElementCurrent->Child = NewElement;
NewElement->Child = NULL; // no child yet
ElementCurrent = NewElement; // make the current pointer to the new pointer
ElementLast = NewElement; // make the end pointer the new pointer
}
return ElementCurrent;
}

bool HeapDelete(int aElementNr) {
// deletes a element
// returns true if succesfull
// returns false if not

bool Res = false;
if (HeapFind(aElementNr) != NULL) {
// there is a element "aByte" in the container
// and ElementCurrent is pointing at it
Res = true;
// the element has been found
if (ElementCurrent->Child == NULL) {
if (ElementCurrent->Parent == NULL) {
// there is no child and no parent
delete ElementCurrent;
ElementCurrent = new clsElement();
ElementFirst = ElementCurrent;
ElementLast = ElementCurrent;
}
else
{ // there is no child and a parent
ElementCurrent->Parent->Child = NULL;
ElementLast = ElementCurrent->Parent;
delete ElementCurrent;
ElementCurrent = ElementLast;
}
}
else
{
if (ElementCurrent->Parent == NULL) {
// there is a child and no parent
ElementCurrent->Child->Parent = NULL;
delete ElementCurrent;
ElementCurrent = ElementLast;
}
else
{ // there is a child and a parent
ElementCurrent->Parent->Child = ElementCurrent->Child;
ElementCurrent->Child->Parent = ElementCurrent->Parent;
delete ElementCurrent;
ElementCurrent = ElementLast;
}
}
}
else
{
// the element has not been found
}
return Res;
}

clsElement *HeapFind(int aElementNr) {
// finds a element
// sets global ElementCurrent to that element
// returns NULL if not found
// return ElementCurrent if found

bool Res = false;
ElementCurrent = ElementLast;
while (ElementCurrent != NULL) {
if (ElementCurrent->ElementNr == aElementNr) {
// found
Res = true;
break;
}
else
{
if (ElementCurrent->Parent != NULL) {
// not found , go to parent
ElementCurrent = ElementCurrent->Parent;
}
else
{
// No Parent, not found
break;
}
}
}

if (! Res) {
ElementCurrent = ElementFirst;
return NULL;
}
else
{
return ElementCurrent;
}
}

clsElement *ElementFindFirst() {
// sets global ElementCurrent to the first element in the container
// returns NULL if the container is empty
// returns the first element if the container is not empty
// use in combination with ElementFindNext() to make a foreach loop
if (ElementFirst != NULL) {
ElementCurrent = ElementFirst;
return ElementCurrent;
}
else
{
return NULL;
}
}

clsElement *ElementFindNext() {
// sets global ElementCurrent to the next element in the container
// returns NULL if there is no next element
// returns ElementCurrent if succesfull

if (ElementCurrent->Child != NULL) {
ElementCurrent = ElementCurrent->Child;
}
else
{
// there is no child , so just to be safe, adjust the ElementLast
ElementLast = ElementCurrent;
return NULL;
}
return ElementCurrent;
}



void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Setup");
ElementCurrent = NULL;
ElementFirst = NULL;
ElementLast = NULL;





if (! HeapDelete(1) ) {
Serial.println("Not found");
}

HeapPush(1);
HeapPush(2);
HeapPush(3);
HeapPush(25);
HeapPush(4);
HeapDelete(25);

if (HeapFind(9)) {
Serial.println("found 9");
}



Serial.println(" For each ...");
ElementFindFirst();
Serial.println(ElementCurrent->ElementNr);
while (ElementFindNext() != NULL) {
Serial.println(ElementCurrent->ElementNr);
}


}

void loop() {
// put your main code here, to run repeatedly:

}

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: dynamische container

Berichtdoor nicoverduin » 29 Jan 2017, 20:02

Netjes
Alleen snap ik niet dat je niet die standaard list gebruikt die ook gewoon in c++ zit maar dan via de library. Was wel een goede exercitie
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 49
Geregistreerd: 09 Mei 2015, 15:57

Re: dynamische container

Berichtdoor doebi » 29 Jan 2017, 20:59

ja, kwas eigenlijk wat weg aan het mijmeren naar de begin jaren waar ik leerde programmeren (begin jaren 90),
waar ik via de programeer taal pascal een boomstructuur in elkaar had geknutseld , en wou dit nu ook eens in c++
t.b.v. mijn buttonclass

Terug naar C code

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 9 gasten