ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

链表实现栈

2020-05-23 10:52:17  阅读:12  来源: 互联网

标签:obj pMyStack temp 实现 top 链表 int return


题目来源:LeetCode739

  1 // 链表节点
  2 typedef struct Node {
  3     int value;    // 气温
  4     int index;    // 下标
  5     struct Node *next;
  6 } pNode;
  7 
  8 // 栈
  9 typedef struct MyStack {
 10     pNode *top;
 11 } pMyStack;
 12 
 13 /**
 14 * @brief 创建栈
 15 * @return 指向栈的指针
 16 */
 17 pMyStack* CreateStack()
 18 {
 19     pMyStack *temp = (pMyStack*)malloc(sizeof(pMyStack));
 20     if (temp == NULL) {
 21         printf("Failed to request memory");
 22         exit(1);
 23     }
 24 
 25     temp->top = NULL;
 26     return temp;
 27 }
 28 
 29 /**
 30 * @brief 判断栈是否为空
 31 * @param 指向栈的指针
 32 * @return 1 栈为空,0 栈不为空
 33 */
 34 int IsEmpty(pMyStack *obj)
 35 {
 36     return obj->top == NULL;
 37 }
 38 
 39 
 40 /**
 41 * @brief 入栈
 42 * @param obj 指向栈的指针
 43 * @param value 气温
 44 * @param index 下标
 45 */
 46 void Push(pMyStack *obj, int value, int index)
 47 {
 48     pNode *temp = (pNode*)malloc(sizeof(pNode));
 49     if (temp == NULL) {
 50         printf("Failed to request memory");
 51         exit(1);
 52     }
 53 
 54     temp->value = value;
 55     temp->index = index;
 56     temp->next = obj->top;
 57     obj->top = temp;
 58 }
 59 
 60 /**
 61 * @brief 出栈
 62 * @param obj 指向栈的指针
 63 */
 64 void Pop(pMyStack *obj)
 65 {
 66     if (obj->top != NULL) {
 67         pNode *temp = obj->top;
 68         obj->top = obj->top->next;
 69         free(temp);
 70     }
 71 }
 72 
 73 /**
 74 * @brief 获取栈顶的气温
 75 * @param obj 指向栈的指针
 76 * @return 栈顶的气温
 77 */
 78 int GetTopValue(pMyStack *obj)
 79 {
 80     if (obj->top != NULL) {
 81         return obj->top->value;
 82     } else {
 83         exit(1);
 84     }
 85 }
 86 
 87 /**
 88 * @brief 获取栈顶的下标
 89 * @param obj 指向栈的指针
 90 * @return 栈顶的下标
 91 */
 92 int GetTopIndex(pMyStack *obj)
 93 {
 94     if (obj->top != NULL) {
 95         return obj->top->index;
 96     } else {
 97         return -1;
 98     }
 99 }
100 
101 /**
102  * Note: The returned array must be malloced, assume caller calls free().
103  */
104 int* dailyTemperatures(int* T, int TSize, int* returnSize)
105 {
106     *returnSize = TSize;
107     int *result = (int*)malloc(sizeof(int) * TSize);
108     pMyStack *myStack = CreateStack();
109 
110     for (int i = TSize - 1; i >= 0; i--) {
111         if (i == TSize - 1) {
112             result[i] = 0;
113             Push(myStack, T[i], i);
114             continue;
115         }
116 
117         //找出后边第一个比今天气温高的日子
118         while (!IsEmpty(myStack)) {
119             if (GetTopValue(myStack) <= T[i]) {
120                 Pop(myStack);
121             } else {
122                 result[i] = GetTopIndex(myStack) - i;
123                 break;
124             }
125         }
126 
127         //栈为空说明后边没有比今天气温高的日子
128         if (IsEmpty(myStack)) {
129             result[i] = 0;
130         }
131 
132         Push(myStack, T[i], i);
133     }
134 
135     return result;
136 }

 

标签:obj,pMyStack,temp,实现,top,链表,int,return
来源: https://www.cnblogs.com/hemeiwolong/p/12941424.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有