详情

美丽的神话 Lv.8

关注
一:有效括号数


学了栈之后这一题就比力简单了。
思绪:1、左括号进栈 2、右括号出栈匹配。
完备代码

由于使用C语言写的,以是内里包罗了栈的实现
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<assert.h>
  4. #include<stdbool.h>
  5. typedef int STDataType;
  6. typedef struct Stack
  7. {
  8.         STDataType* a;
  9.         int top;
  10.         int capacity;
  11. }ST;
  12. void StackInit(ST* st);
  13. void StackPush(ST* st, STDataType x);
  14. void StackPop(ST* st);
  15. STDataType StackTop(ST* st);
  16. bool StackEmpty(ST* st);
  17. void StackDestroy(ST* st);
  18. int StackSize(ST* st);
  19. void StackInit(ST* st)
  20. {
  21.         st->a = NULL;
  22.         //top 表示栈顶元素的下一个
  23.         st->capacity = st->top = 0;
  24. }
  25. void StackPush(ST* st, STDataType x)
  26. {
  27.         assert(st);
  28.         if (st->top == st->capacity)
  29.         {
  30.                 int newcapacity = st->capacity == 0 ? 4 : st->capacity * 2;
  31.                 STDataType* tmp = (STDataType*)realloc(st->a, newcapacity*sizeof(STDataType));
  32.                 if (tmp == NULL)
  33.                 {
  34.                         perror("malloc fail!!");
  35.                         exit(1);
  36.                 }
  37.                 st->a = tmp;
  38.                 st->capacity = newcapacity;
  39.         }
  40.         st->a[st->top++] = x;
  41. }
  42. void StackPop(ST* st)
  43. {
  44.         assert(st);
  45.         assert(!StackEmpty(st));
  46.         st->top--;
  47. }
  48. STDataType StackTop(ST* st)
  49. {
  50.         assert(st);
  51.         assert(!StackEmpty(st));
  52.         return st->a[st->top - 1];
  53. }
  54. bool StackEmpty(ST* st)
  55. {
  56.         assert(st);
  57.         return st->top == 0;
  58. }
  59. void StackDestroy(ST* st)
  60. {
  61.         assert(st);
  62.         free(st->a);
  63.         st->a == NULL;
  64.         st->capacity = st->top = 0;
  65. }
  66. int StackSize(ST* st)
  67. {
  68.         assert(st);
  69.         return st->top;
  70. }
  71. bool isValid(char* s) {
  72.     ST st;
  73.     StackInit(&st);
  74.     while(*s)
  75.     {
  76.         if(*s=='{'
  77.         || *s=='('
  78.         || *s=='[')
  79.         {
  80.             StackPush(&st,*s);
  81.         }
  82.         else
  83.         {
  84.             if(StackEmpty(&st))
  85.             {
  86.                 StackDestroy(&st);
  87.                 return false;
  88.             }
  89.             char c = StackTop(&st);
  90.             StackPop(&st);
  91.             if((c == '{' && *s != '}')
  92.             || (c == '(' && *s != ')')
  93.             || (c == '[' && *s != ']'))
  94.             {
  95.                 StackDestroy(&st);
  96.                 return false;
  97.             }
  98.         }
  99.         s++;
  100.     }
  101.     bool ret = StackEmpty(&st);
  102.     StackDestroy(&st);
  103.     return ret;
  104. }
二:用队列实现栈


起首阐明一点,这只是对栈和队列熟练度的观察,并不是栈的更好地实现方式。
思绪:两个队列,包管有一个队列为空,然后别的操纵基于空队列实现。
画图分析:


完备代码

  1. typedef int QDatatype;
  2. typedef struct QueueNode {
  3.         QDatatype data;
  4.         struct QueueNode* next;
  5. }QueueNode;
  6. typedef struct Queue {
  7.         struct QueueNode* head;
  8.         struct QueueNode* tail;
  9. }Queue;
  10. void QueueInit(Queue* pq);
  11. void QueueDestroy(Queue* pq);
  12. void QueuePush(Queue* pq, QDatatype x);
  13. void QueuePop(Queue* pq);
  14. QDatatype QueueFront(Queue* pq);
  15. QDatatype Queueback(Queue* pq);
  16. int QueueSize(Queue* pq);
  17. bool QueueEmpty(Queue* pq);
  18. void QueueInit(Queue* pq)
  19. {
  20.         assert(pq);
  21.         pq->head = NULL;
  22.         pq->tail = NULL;
  23. }
  24. void QueueDestroy(Queue* pq)
  25. {
  26.         assert(pq);
  27.         QueueNode* cur = pq->head;
  28.         while (cur)
  29.         {
  30.                 QueueNode* next = cur->next;
  31.                 free(cur);
  32.                 cur = next;
  33.         }
  34.         pq->head = pq->tail = NULL;
  35. }
  36. void QueuePush(Queue* pq, QDatatype x)
  37. {
  38.         assert(pq);
  39.         QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
  40.         if (newnode == NULL)
  41.         {
  42.                 perror("malloc fail!!");
  43.                 exit(1);
  44.         }
  45.         else
  46.         {
  47.                 newnode->next = NULL;
  48.                 newnode->data = x;
  49.         }
  50.         if (pq->head == NULL)
  51.         {
  52.                 pq->head = pq->tail = newnode;
  53.         }
  54.         else
  55.         {
  56.                 pq->tail->next = newnode;
  57.                 pq->tail = newnode;
  58.         }
  59. }
  60. void QueuePop(Queue* pq)
  61. {
  62.         assert(pq);
  63.         assert(pq->head != NULL);
  64.         QueueNode* del = pq->head;
  65.         pq->head = del->next;
  66.         free(del);
  67.         del = NULL;
  68.         if (pq->head == NULL)
  69.         {
  70.                 pq->tail = NULL;
  71.         }
  72. }
  73. QDatatype QueueFront(Queue* pq)
  74. {
  75.         assert(pq);
  76.         assert(pq->head != NULL);
  77.         return pq->head->data;
  78. }
  79. QDatatype Queueback(Queue* pq)
  80. {
  81.         assert(pq);
  82.         assert(pq->head != NULL);
  83.         return pq->tail->data;
  84. }
  85. int QueueSize(Queue* pq)
  86. {
  87.         assert(pq);
  88.         int cnt = 0;
  89.         QueueNode* cur = pq->head;
  90.         while (cur)
  91.         {
  92.                 cnt++;
  93.                 cur = cur->next;
  94.         }
  95.         return cnt;
  96. }
  97. bool QueueEmpty(Queue* pq)
  98. {
  99.         assert(pq);
  100.         return pq->head == NULL;
  101. }
  102. typedef struct {
  103.     Queue q1;
  104.     Queue q2;
  105. } MyStack;
  106. MyStack* myStackCreate()
  107. {
  108.     //不能直接创建结构体,出函数栈帧销毁
  109.     MyStack* st = (MyStack*)malloc(sizeof(MyStack));
  110.     QueueInit(&st->q1);
  111.     QueueInit(&st->q2);
  112.     return st;
  113. }
  114. void myStackPush(MyStack* obj, int x) {
  115.     if(!QueueEmpty(&obj->q1))
  116.     {
  117.         QueuePush(&obj->q1,x);
  118.     }
  119.     else
  120.     {
  121.         QueuePush(&obj->q2,x);
  122.     }
  123. }
  124. int myStackPop(MyStack* obj) {
  125.         //判空
  126.         Queue *Empty = &obj->q1;
  127.         Queue *NonoEmpty = &obj->q2;
  128.         if (!QueueEmpty(Empty))
  129.         {
  130.                 Empty = &obj->q2;
  131.                 NonoEmpty = &obj->q1;
  132.         }
  133.         //出队列,入另外一个队列
  134.         while (QueueSize(NonoEmpty) > 1)
  135.         {
  136.                 QDatatype data = QueueFront(NonoEmpty);
  137.                 QueuePush(Empty, data);
  138.                 QueuePop(NonoEmpty);
  139.         }
  140.         //剩余一个元素
  141.         QDatatype data = QueueFront(NonoEmpty);
  142.         QueuePop(NonoEmpty);
  143.         return data;
  144. }
  145. int myStackTop(MyStack* obj) {
  146.     if(!QueueEmpty(&obj->q1))
  147.     {
  148.         return Queueback(&obj->q1);
  149.     }
  150.     else
  151.     {
  152.         return Queueback(&obj->q2);
  153.     }
  154. }
  155. bool myStackEmpty(MyStack* obj) {
  156.     return (QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2));
  157. }
  158. void myStackFree(MyStack* obj) {
  159.     QueueDestroy(&obj->q1);
  160.     QueueDestroy(&obj->q2);
  161.     free(obj);
  162. }
  163. /**
  164. * Your MyStack struct will be instantiated and called as such:
  165. * MyStack* obj = myStackCreate();
  166. * myStackPush(obj, x);
  167. * int param_2 = myStackPop(obj);
  168. * int param_3 = myStackTop(obj);
  169. * bool param_4 = myStackEmpty(obj);
  170. * myStackFree(obj);
  171. */
三:用栈实现队列


这一题就是反过来用栈实现队列,要实现后进先出。
思绪:两个栈pushst popst 然后往pushst内里push ,每当要出队列大概要去队头数据时,就返回popst的栈顶数据,如果popst为空,就一口气把pushst内里的数据全塞进去。
就不画图了

完备代码

栈的实如今上面有,不重复拷贝了。
  1. typedef struct {
  2.     ST pushst;
  3.     ST popst;
  4. } MyQueue;
  5. MyQueue* myQueueCreate() {
  6.     MyQueue* pq = (MyQueue*)malloc(sizeof(MyQueue));
  7.     StackInit(&pq->pushst);
  8.     StackInit(&pq->popst);
  9.     return pq;
  10. }
  11. void myQueuePush(MyQueue* obj, int x) {
  12.     StackPush(&obj->pushst,x);
  13. }
  14. int myQueuePop(MyQueue* obj) {
  15.     if(StackEmpty(&obj->popst))
  16.     {
  17.         while(!StackEmpty(&obj->pushst))
  18.         {
  19.             StackPush(&obj->popst,StackTop(&obj->pushst));
  20.             StackPop(&obj->pushst);
  21.         }
  22.     }
  23.     int frontstack = StackTop(&obj->popst);
  24.     StackPop(&obj->popst);
  25.     return frontstack;
  26. }
  27. int myQueuePeek(MyQueue* obj) {
  28.     if(StackEmpty(&obj->popst))
  29.     {
  30.         while(!StackEmpty(&obj->pushst))
  31.         {
  32.             StackPush(&obj->popst,StackTop(&obj->pushst));
  33.             StackPop(&obj->pushst);
  34.         }
  35.     }
  36.     return StackTop(&obj->popst);
  37. }
  38. bool myQueueEmpty(MyQueue* obj) {
  39.     return StackEmpty(&obj->popst) && StackEmpty(&obj->pushst);
  40. }
  41. void myQueueFree(MyQueue* obj) {
  42.     StackDestroy(&obj->popst);
  43.     StackDestroy(&obj->pushst);
  44.     free(obj);
  45. }
四:环形队列(环形缓冲器)


这里给各人贴一张图:

(数组实现)思绪:创建K(存有效数据的巨细)+1的长度,两个指针(严格来讲应该是下标)指向头和尾。然后用%操纵实现循环。
详细请看下面画图分析
画图分析:


完备代码:

  1. typedef struct {
  2.     int* a;
  3.     int tail;
  4.     int head;
  5.     int k;
  6. } MyCircularQueue;
  7. bool myCircularQueueIsEmpty(MyCircularQueue* obj);
  8. bool myCircularQueueIsFull(MyCircularQueue* obj);
  9. MyCircularQueue* myCircularQueueCreate(int k) {
  10.     MyCircularQueue* cp =(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
  11.     cp->a =(int*)malloc(sizeof(int)*(k+1));
  12.     cp->tail = 0;
  13.     cp->head = 0;
  14.     cp->k = k;
  15.     return cp;
  16. }
  17. bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
  18.     if(myCircularQueueIsFull(obj))
  19.     return false;
  20.     else
  21.     {
  22.         obj->a[obj->tail] = value;
  23.         ++obj->tail;
  24.         obj->tail %= (obj->k+1);
  25.         return true;
  26.     }
  27. }
  28. bool myCircularQueueDeQueue(MyCircularQueue* obj) {
  29.     if(myCircularQueueIsEmpty(obj))
  30.     return false;
  31.     else
  32.     {
  33.         obj->head++;
  34.         obj->head %= (obj->k+1);
  35.         return true;
  36.     }
  37. }
  38. int myCircularQueueFront(MyCircularQueue* obj) {
  39.     if(myCircularQueueIsEmpty(obj))
  40.     return -1;
  41.     return obj->a[obj->head];
  42. }
  43. int myCircularQueueRear(MyCircularQueue* obj) {
  44.     if(myCircularQueueIsEmpty(obj))
  45.     return -1;
  46.     return obj->a[(obj->tail+obj->k)%(obj->k+1)];
  47. }
  48. bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
  49.     return obj->head == obj->tail;
  50. }
  51. bool myCircularQueueIsFull(MyCircularQueue* obj) {
  52.     return (obj->tail+1)%(obj->k+1) == obj->head;
  53. }
  54. void myCircularQueueFree(MyCircularQueue* obj) {
  55.     free(obj->a);
  56.     free(obj);
  57. }
  完

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
附件: 您需要登录才可以下载或查看附件。没有账号?立即注册
55阅读
0回复

暂无评论,点我抢沙发吧