Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sqstack.c 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "sqstack.h"
  4. sqstack_t pkt_creat_sqstack(int len)
  5. {
  6. sqstack_t s;
  7. if((s = (sqstack_t)malloc(sizeof(struct sqstack))) == NULL)
  8. {
  9. printf("malloc failed!\n");
  10. return NULL;
  11. }
  12. if((s->frame = (stack_frame_t)malloc(sizeof(struct stack_frame) * len)) == NULL)
  13. {
  14. printf("malloc failed!\n");
  15. return NULL;
  16. }
  17. s->max_len = len;
  18. s->top = -1;
  19. return s;
  20. }
  21. int pkt_sqstack_empty(sqstack_t s)
  22. {
  23. return s->top == -1 ? 1 : 0;
  24. }
  25. int pkt_sqstack_full(sqstack_t s)
  26. {
  27. return s->top == (s->max_len - 1) ? 1 : 0;
  28. }
  29. void pkt_clear_sqstack(sqstack_t s)
  30. {
  31. s->top = -1;
  32. }
  33. stack_frame_t pkt_sqstack_pop(sqstack_t s)
  34. {
  35. if(pkt_sqstack_empty(s))
  36. {
  37. printf("sqstack is empty!\n");
  38. return NULL;
  39. }
  40. s->top--;
  41. return &s->frame[s->top + 1];
  42. }
  43. int pkt_sqstack_push(sqstack_t s, struct stack_frame frame)
  44. {
  45. if(pkt_sqstack_full(s))
  46. {
  47. printf("sqstack is full!\n");
  48. return -1;
  49. }
  50. s->frame[s->top + 1] = frame;
  51. s->top++;
  52. return 0;
  53. }
  54. stack_frame_t pkt_get_sqstack_top(sqstack_t s)
  55. {
  56. return &s->frame[s->top];
  57. }
  58. void pkt_sqstack_free(sqstack_t s)
  59. {
  60. free(s->frame);
  61. free(s);
  62. }
  63. int main(void)
  64. {
  65. sqstack_t s = pkt_creat_sqstack(10);
  66. struct stack_frame frame;
  67. for(int i = 1; i <= 10; i++)
  68. {
  69. frame.data = i;
  70. pkt_sqstack_push(s, frame);
  71. }
  72. while(!pkt_sqstack_empty(s))
  73. {
  74. printf("%d\n", pkt_sqstack_pop(s)->data);
  75. }
  76. return 0;
  77. }