当前位置:   article > 正文

重生之“我打数据结构,真的假的?”--3.栈和队列

重生之“我打数据结构,真的假的?”--3.栈和队列

1.栈和队列的基本概念

1.1 栈

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

1.2 初始化及功能实现

  1. #include"stack.h"
  2. typedef int datatype;
  3. typedef struct stack
  4. {
  5. datatype* a;
  6. int top;
  7. int capacity;
  8. }ST;
  9. void InitST(ST* sl)
  10. {
  11. assert(sl); //sl不为NULL
  12. sl->a = NULL;
  13. sl->top = 0;
  14. sl->capacity = 0;
  15. }//初始化
  16. void STpush(ST* sl, int data)
  17. {
  18. assert(sl);
  19. if (sl->top == sl->capacity)
  20. {
  21. int newcapacity = sl->capacity == 0 ? 2 : sl->capacity * 2;
  22. datatype* tmp = (datatype*)realloc(sl->a, newcapacity * sizeof(datatype));
  23. sl->a = tmp;
  24. sl->capacity = newcapacity;
  25. }
  26. sl->a[sl->top] = data;
  27. sl->top++;
  28. }//插入
  29. void STpop(ST* sl)
  30. {
  31. assert(sl); //!!!!!!判断指针是否为空,《《《《判断的是栈是否存在》》》》》》
  32. assert(!STempty(sl)); //!!!!!!!!!与上文不同,《《《《判断的是栈是否为空》》》》》》
  33. sl->top--;
  34. }//删除
  35. datatype STtop(ST* sl)
  36. {
  37. assert(sl);
  38. assert(!STempty(sl));
  39. return sl->a[sl->top-1];
  40. }//取栈顶
  41. void STdestroy(ST* sl)
  42. {
  43. assert(sl);
  44. free(sl->a);
  45. sl->a = NULL;
  46. sl->top = sl->capacity = 0;
  47. }
  48. bool STempty(ST* sl)
  49. {
  50. assert(sl);
  51. return sl->top == 0;
  52. }

1.3 括号匹配问题

. - 力扣(LeetCode)

思路:

1.设立一个栈,依次从s中取出元素,如果为左括号类型 则入栈

2.否则取栈顶元素(如果栈为空则直接返回false)如果匹配

则删除栈顶元素,如果不匹配,返回false

3.s访问完后,如果栈为空,则返回true,否则返回false

  1. bool isValid(char* s) {
  2. typedef struct stack
  3. {
  4. char * a;
  5. int top;
  6. int capacity;
  7. }ST;
  8. bool STempty(ST* sl)
  9. {
  10. assert(sl);
  11. return sl->top == 0;
  12. }
  13. void InitST(ST* sl)
  14. {
  15. assert(sl); //sl不为NULL
  16. sl->a = NULL;
  17. sl->top = 0;
  18. sl->capacity = 0;
  19. }
  20. void STpush(ST* sl, char data)
  21. {
  22. assert(sl);
  23. if (sl->top == sl->capacity)
  24. {
  25. int newcapacity = sl->capacity == 0 ? 2 : sl->capacity * 2;
  26. char* tmp = (char*)realloc(sl->a, newcapacity * sizeof(char));
  27. sl->a = tmp;
  28. sl->capacity = newcapacity;
  29. }
  30. sl->a[sl->top] = data;
  31. sl->top++;
  32. }
  33. void STpop(ST* sl)
  34. {
  35. assert(sl); //!!!!!!判断指针是否为空,《《《《判断的是栈是否存在》》》》》》
  36. assert(!STempty(sl)); //!!!!!!!!!与上文不同,《《《《判断的是栈是否为空》》》》》》
  37. sl->top--;
  38. }
  39. char STtop(ST* sl)
  40. {
  41. assert(sl);
  42. assert(!STempty(sl));
  43. return sl->a[sl->top-1];
  44. }
  45. void STdestroy(ST* sl)
  46. {
  47. assert(sl);
  48. free(sl->a);
  49. sl->a = NULL;
  50. sl->top = sl->capacity = 0;
  51. }
  52. int i=0;
  53. char j;
  54. char k;
  55. ST L;
  56. ST R;
  57. InitST(&L);
  58. InitST(&R);
  59. while(s[i]!='\0')
  60. {
  61. if(s[i]=='('||s[i]=='['||s[i]=='{')
  62. {
  63. STpush(&L, s[i]);
  64. i++;
  65. }
  66. else
  67. {
  68. if(STempty(&L))
  69. return false;
  70. if((STtop(&L)=='('&&s[i]==')')||(STtop(&L)=='{'&&s[i]=='}')||(STtop(&L)=='['&&s[i]==']'))
  71. {
  72. STpop(&L);
  73. i++;
  74. }
  75. else
  76. return false;
  77. }
  78. }
  79. if(STempty(&L))
  80. return true;
  81. else
  82. return false;
  83. }

2.队列 

2.1 概念

队列的顺序实现是指分配一块连续的存储单元存放队列中的元素,并附设两个指针:队头指针 front 指向队头元素,队尾指针 rear 指向队尾元素

2.2 初始化及功能实现

  1. typedef int datatype;
  2. typedef struct Queuenode
  3. {
  4. datatype data;
  5. struct Queuenode * next;
  6. }Qnode;
  7. typedef struct QT
  8. {
  9. Qnode* head;
  10. Qnode* tail;
  11. int size;
  12. }QT;
  13. void QueueInit(QT* sl)
  14. {
  15. assert(sl);
  16. sl->head = NULL;
  17. sl->tail = NULL;
  18. sl->size = 0;
  19. }
  20. bool Queueempty(QT* sl)
  21. {
  22. assert(sl);
  23. return sl->head ==NULL;
  24. }
  25. void Queuepush(QT* sl, int x)
  26. {
  27. assert(sl);
  28. Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
  29. newnode->next = NULL;
  30. newnode->data = x;
  31. if (Queueempty(sl))
  32. {
  33. sl->head = newnode;
  34. sl->tail = newnode;
  35. }
  36. else
  37. {
  38. sl->tail->next = newnode;
  39. sl->tail = newnode;
  40. }
  41. sl->size++;
  42. }
  43. void Queuepop(QT* sl)
  44. {
  45. assert(sl);
  46. assert(!Queueempty(sl));
  47. Qnode* next = sl->head->next;
  48. free(sl->head);
  49. sl->head = next;
  50. sl->size--;
  51. }
  52. int Queuetop(QT* sl)
  53. {
  54. assert(sl);
  55. assert(!Queueempty(sl));
  56. return sl->head->data;
  57. }
  58. void Queuedestroy(QT* sl)
  59. {
  60. assert(sl);
  61. while (!Queueempty(sl))
  62. Queuepop(sl);
  63. sl->head = sl->tail = NULL;
  64. sl->size = 0;
  65. }

2.3 循环队列 

. - 力扣(LeetCode)

  1. typedef struct Queuenode
  2. {
  3. int data;
  4. struct Queuenode * next;
  5. }Qnode;
  6. typedef struct {
  7. Qnode* head;
  8. Qnode* tail;
  9. int size;
  10. int flag;
  11. } MyCircularQueue;
  12. MyCircularQueue* myCircularQueueCreate(int k) {
  13. MyCircularQueue* sl=NULL;
  14. sl=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
  15. sl->head = NULL;
  16. sl->tail = NULL;
  17. sl->size = 0;
  18. sl->flag = k;
  19. return sl;
  20. }
  21. bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
  22. return obj->head ==NULL;
  23. }
  24. bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
  25. if(obj->size>=obj->flag)
  26. return false;
  27. Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
  28. newnode->next = NULL;
  29. newnode->data = value;
  30. if(myCircularQueueIsEmpty(obj))
  31. {
  32. obj->head = newnode;
  33. obj->tail = newnode;
  34. obj->tail->next=obj->head;
  35. obj->head->next=obj->tail;
  36. }
  37. else
  38. {
  39. obj->tail->next = newnode;
  40. obj->tail = newnode;
  41. if(obj->head->next==obj->head)
  42. obj->head->next=obj->tail;
  43. obj->tail->next=obj->head;
  44. }
  45. obj->size++;
  46. return true;
  47. }
  48. bool myCircularQueueDeQueue(MyCircularQueue* obj) {
  49. if(myCircularQueueIsEmpty(obj))
  50. return false;
  51. else{
  52. if(obj->head!=obj->tail)
  53. {
  54. Qnode* next = obj->head->next;
  55. obj->tail->next=next;
  56. free(obj->head);
  57. obj->head = next;
  58. obj->size--;
  59. }
  60. else{
  61. free(obj->head);
  62. obj->head = obj->tail = NULL;
  63. obj->size = 0;
  64. }
  65. }
  66. return true;
  67. }
  68. int myCircularQueueFront(MyCircularQueue* obj) {
  69. int i=0;
  70. if(myCircularQueueIsEmpty(obj))
  71. return -1;
  72. else
  73. return obj->head->data;
  74. }
  75. int myCircularQueueRear(MyCircularQueue* obj) {
  76. if(myCircularQueueIsEmpty(obj))
  77. return -1;
  78. else
  79. return obj->tail->data;
  80. }
  81. bool myCircularQueueIsFull(MyCircularQueue* obj) {
  82. return obj->size==obj->flag;
  83. }
  84. void myCircularQueueFree(MyCircularQueue* obj) {
  85. while (!myCircularQueueIsEmpty(obj))
  86. {
  87. Qnode* next = obj->head->next;
  88. obj->tail->next=next;
  89. if(obj->head!=obj->tail)
  90. { free(obj->head);
  91. obj->head = next;
  92. obj->size--;
  93. }
  94. else{
  95. obj->head = obj->tail = NULL;
  96. obj->size = 0;
  97. break;
  98. }
  99. }
  100. }

3.用栈实现队列 

 . - 力扣(LeetCode)

 思路:

1.可以设立两个栈 p,q

(1)入队:将p中元素依次入栈q,再将函数传递的数据入栈q

(2)出队:将q中元素依次入栈p,再取q栈顶元素

  1. typedef struct stack
  2. {
  3. int * a;
  4. int top;
  5. int capacity;
  6. }ST;
  7. typedef struct {
  8. ST* p;
  9. ST* q;
  10. } MyQueue;
  11. MyQueue* myQueueCreate()
  12. {
  13. MyQueue*sl=NULL;
  14. sl=(MyQueue*)malloc(sizeof(MyQueue));
  15. sl->q=(ST*)malloc(sizeof(ST));
  16. sl->p=(ST*)malloc(sizeof(ST));
  17. sl->q->a=NULL;
  18. sl->q->top=0;
  19. sl->q->capacity=0;
  20. sl->p->a=NULL;
  21. sl->p->top=0;
  22. sl->p->capacity=0;
  23. return sl;
  24. }
  25. bool STempty(ST* sl)
  26. {
  27. assert(sl);
  28. return sl->top == 0;
  29. }
  30. int STtop(ST* sl)
  31. {
  32. assert(sl);
  33. assert(!STempty(sl));
  34. int i;
  35. i=sl->a[sl->top-1];
  36. return i;
  37. }
  38. void myQueuePush(MyQueue* obj, int x) {
  39. while(!STempty(obj->q))
  40. {
  41. if (obj->p->top == obj->q->capacity)
  42. {
  43. int newcapacity = obj->q->capacity == 0 ? 2 : obj->p->capacity * 2;
  44. int * tmp = (int*)realloc(obj->p->a, newcapacity * sizeof(int));
  45. obj->p->a = tmp;
  46. obj->p->capacity = newcapacity;
  47. }
  48. obj->p->a[obj->p->top] =STtop(obj->q) ;
  49. obj->p->top++;
  50. obj->q->top--;
  51. }
  52. if (obj->p->top == obj->p->capacity)
  53. {
  54. int newcapacity = obj->p->capacity == 0 ? 2 : obj->p->capacity * 2;
  55. int * tmp = (int*)realloc(obj->p->a, newcapacity * sizeof(int));
  56. obj->p->a = tmp;
  57. obj->p->capacity = newcapacity;
  58. }
  59. obj->p->a[obj->p->top] = x;
  60. obj->p->top++;
  61. }
  62. int myQueuePop(MyQueue* obj) {
  63. while(!STempty(obj->p))
  64. {
  65. if (obj->q->top == obj->q->capacity)
  66. {
  67. int newcapacity = obj->q->capacity == 0 ? 2 : obj->q->capacity * 2;
  68. int * tmp = (int*)realloc(obj->q->a, newcapacity * sizeof(int));
  69. obj->q->a = tmp;
  70. obj->q->capacity = newcapacity;
  71. }
  72. obj->q->a[obj->q->top] =STtop(obj->p) ;
  73. obj->q->top++;
  74. obj->p->top--;
  75. }
  76. int i=STtop(obj->q);
  77. obj->q->top--;
  78. return i;
  79. }
  80. int myQueuePeek(MyQueue* obj) {
  81. while(!STempty(obj->p))
  82. {
  83. if (obj->q->top == obj->q->capacity)
  84. {
  85. int newcapacity = obj->q->capacity == 0 ? 2 : obj->q->capacity * 2;
  86. int * tmp = (int*)realloc(obj->q->a, newcapacity * sizeof(int));
  87. obj->q->a = tmp;
  88. obj->q->capacity = newcapacity;
  89. }
  90. obj->q->a[obj->q->top] =STtop(obj->p) ;
  91. obj->q->top++;
  92. obj->p->top--;
  93. }
  94. int i=STtop(obj->q);
  95. return i;
  96. }
  97. bool myQueueEmpty(MyQueue* obj) {
  98. if(STempty(obj->p)&&STempty(obj->q))
  99. return true;
  100. else
  101. return false;
  102. }
  103. void myQueueFree(MyQueue* obj) {
  104. free(obj->p);
  105. free(obj->q);
  106. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/892018
推荐阅读
相关标签
  

闽ICP备14008679号