2
0

Beginners_-_StructStackListFunction.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Stack and List and other data structures are not build into the c language.
  3. // If you need to convert code that has stacks or lists you need to write or re use code
  4. // for that.
  5. //
  6. // As far as I have gotten to know Clang and sources about it you need to use
  7. // arrays for these. A stack is a array with several functions to manipulate
  8. // it. Most known are Pop And Push. With making you own stack from a array
  9. // the Push adds the data to the highest location[0..highest] in the array. You actually
  10. // need to keep track of this by a counter that holds the size of your stack. (You need to
  11. // create arrays in Clang at program start and thus need to estimate how large they might
  12. // need to be.)
  13. // A pop removes the highest location from the array and decreases the counter with one.
  14. //
  15. // You can write more functions to remove items from the middle of the list or stack. You then
  16. // need to shift or "sort" the array.
  17. #define MAXSTACKSIZE 1024 // how large a stack do we need?
  18. #include "raylib.h"
  19. // Our stack
  20. static int mystacklen=0;
  21. typedef struct stack{
  22. int x;
  23. int y;
  24. }stack;
  25. static stack mystack[MAXSTACKSIZE]; // Here this stack is global to our program.
  26. static void push(struct stack in);
  27. static void pop(void);
  28. int main(void)
  29. {
  30. // Initialization
  31. //--------------------------------------------------------------------------------------
  32. const int screenWidth = 800;
  33. const int screenHeight = 450;
  34. InitWindow(screenWidth, screenHeight, "raylib example.");
  35. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  36. //--------------------------------------------------------------------------------------
  37. // Create a struct called banana and pass it to the push function.
  38. struct stack banana;
  39. banana.x = 10;
  40. banana.y = 20;
  41. push(banana);
  42. // Main game loop
  43. while (!WindowShouldClose()) // Detect window close button or ESC key
  44. {
  45. // Update
  46. //----------------------------------------------------------------------------------
  47. if(IsKeyReleased(KEY_SPACE)){
  48. // Another way to create and pass a struct through a function.
  49. struct stack banana={GetRandomValue(0,100),0};
  50. push(banana);
  51. }
  52. if(IsKeyReleased(KEY_ENTER)){
  53. pop();
  54. }
  55. //----------------------------------------------------------------------------------
  56. // Draw
  57. //----------------------------------------------------------------------------------
  58. BeginDrawing();
  59. ClearBackground(RAYWHITE);
  60. DrawText("Press space to add to stack, enter to remove",200,10,20,DARKGRAY);
  61. DrawText(FormatText("Stack size : %i",mystacklen),320,30,20,DARKGRAY);
  62. for(int i=0;i<mystacklen;i++){
  63. DrawText(FormatText("stack %i",mystack[i].x),0,20+i*20,20,BLACK);
  64. }
  65. EndDrawing();
  66. //----------------------------------------------------------------------------------
  67. }
  68. // De-Initialization
  69. //--------------------------------------------------------------------------------------
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }
  74. void pop(){
  75. if(mystacklen>0)mystacklen-=1;
  76. }
  77. void push(struct stack in){
  78. mystack[mystacklen] = in;
  79. mystacklen+=1;
  80. }