Giter Club home page Giter Club logo

Comments (2)

SMFSW avatar SMFSW commented on August 16, 2024

Hello,

you're trying here to print return value from q.pop() method, which returns 0 or 1 (1 if correctly pulled from queue).

You should decompose this:
Serial.println( q.pop(&Str1));
into:

char Str1;
q.pop(&Str1);
Serial.println(*Str1);

Also, in your code, you're pushing Str2 address to queue (not what it points to);
moreover, even if pushing Str2/Str1 pointer correctly, they will be freed and not exist anymore when leaving setup.

This should work for basic types, but not if using String class, even more with a pointer to String.
String is a class, and you would only be able to store each String pointer in the queue to use them later, as long as they are still allocated.
Queue can store any basic types, such as integers, chars (and arrays), structures, pointers...
Storing pointers contents is trickier (for classes such as String which are size variable and using dynamic allocation) and shall be handled by your own code.

Regards,
SMFSW

from queue.

MarcoZheng1991 avatar MarcoZheng1991 commented on August 16, 2024

Hello, thank you for your reply !
my ultimate goal is find the shortest path in a 5*5 grid. I used the breadth search method and the queue you built.
I created an array of int[3], [1] X coordinate [2] Y coordinate [3] Number of steps. Before this, I created a string array, but because I didn't understand the usage of string pointers, I changed it to an int array.
In Setup, input the coordinates of the starting point and the first step into the array and push it into the queue, and then continue to set the flag in the loop in the way of first right and then down until the end point is reached.

This program has now been completed, thank you very much for your development.
Below is my code:

//#include <Arduino_BuiltIn.h>

#include<cppQueue.h>
#include<String.h>

#define QueueLength 10

cppQueue q(sizeof(int[3]),QueueLength,FIFO,true);

//Crate 5*5 array
int Location[5][5]
{
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
};

int x;
int y;
int myArray[3]={0,0,0};// 1. X 2.Y 3 Step

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);

x=myArray[0];
y=myArray[1];
Location[x][y]=1; //write 1 to strat position
myArray[2]=myArray[2]+1; //Step+1
q.push(&myArray);
}

void loop() {

int myArray1[3]={0,0,0};// 1. X 2.Y 3 Step

// Strat loop if currentStep greater 1 and enter location be Occupied
if ( Location[4][4]==0 )
{
// get data from queue
q.pop(&myArray1);
Serial.println(myArray1[0]);
Serial.println(myArray1[1]);
Serial.println(myArray1[2]);
delay(500);

// move right
   x=myArray1[0]+1;           // X Coordinate+1 to int x 
   y=myArray1[1];             // y Coordinate int y 
   if(Location[x][y]==0)     // execute if value qeaul 0 in location
   {
   Location[x][y]=1;         // Ooccupy current location
   }
   myArray1[0]=myArray1[0]+1;           // X Coordinate+1 to int x 
   myArray1[2]=myArray1[2]+1;  // Step number +1  
   q.push(&myArray1);
   x=myArray1[0]-1;             // restore X coordinate
   delay(50);
   

 
 //move down
   x=myArray[0];           // X Coordinate+1 to int x 
   y=myArray[1]+1;             // y Coordinate int y 
   if(Location[x][y]==0)     // execute if value qeaul 0 in location
   {
   Location[x][y]=1;         // Ooccupy current location 
   }
    myArray[1]=myArray[1]+1;             // y Coordinate int y 
    myArray[2]=myArray[2]+1;  // Step number +1 
    q.push(&myArray);
    y=myArray[1]-1;             // restore X coordinate
    
   delay(50);

if (Location[4][4]==1)
{
 Serial.println("Congratulations !!! The shortest path is: "); 
 Serial.println(myArray1[2]);
 Serial.println(" Step");
  }

}

}

from queue.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.