Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitce7a93e

Browse files
committed
Create 0155-min-stack.c.
1 parent59f24eb commitce7a93e

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

‎c/0155-min-stack.c‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include<stdlib.h>
2+
#include<assert.h>
3+
4+
// A structure to represent an element of the stack.
5+
typedefstructNode {
6+
intvalue;
7+
structNode*next;
8+
}Node;
9+
10+
Node*nodeCreate(intval) {
11+
Node*node= (Node*)malloc(sizeof(Node));
12+
node->value=val;
13+
node->next=NULL;
14+
returnnode;
15+
}
16+
17+
voidnodePush(Node**obj,intval) {
18+
assert(obj);
19+
Node*node= (Node*)malloc(sizeof(Node));
20+
node->value=val;
21+
node->next=*obj;
22+
*obj=node;
23+
}
24+
25+
voidnodePop(Node**obj) {
26+
assert(obj);
27+
if (*obj) {
28+
Node*tmp=*obj;
29+
*obj=tmp->next;
30+
free(tmp);
31+
}
32+
}
33+
34+
// A structure to represent a stack.
35+
typedefstruct {
36+
intsize;
37+
Node*top;
38+
}MyStack;
39+
40+
MyStack*stackCreate()
41+
{
42+
MyStack*stack= (MyStack*)malloc(sizeof(MyStack));
43+
stack->size=0;
44+
stack->top=NULL;
45+
returnstack;
46+
}
47+
48+
voidstackPush(MyStack*stack,intitem)
49+
{
50+
if (!stack->top) {
51+
stack->top=nodeCreate(item);
52+
}else {
53+
nodePush(&stack->top,item);
54+
}
55+
stack->size++;
56+
}
57+
58+
voidstackPop(MyStack*stack)
59+
{
60+
nodePop(&stack->top);
61+
stack->size--;
62+
}
63+
64+
intstackTop(MyStack*stack)
65+
{
66+
assert(stack);
67+
assert(stack->top);
68+
returnstack->top->value;
69+
}
70+
71+
typedefstruct {
72+
MyStack*stk;
73+
MyStack*minStk;
74+
}MinStack;
75+
76+
MinStack*minStackCreate() {
77+
MinStack*min= (MinStack*)malloc(sizeof(MinStack));
78+
min->stk=NULL;
79+
min->minStk=NULL;
80+
returnmin;
81+
}
82+
83+
voidminStackPush(MinStack*obj,intval) {
84+
assert(obj);
85+
if(!obj->stk) {
86+
obj->stk=stackCreate();
87+
obj->minStk=stackCreate();
88+
}
89+
stackPush(obj->stk,val);
90+
if (obj->minStk->top) {
91+
intminTop=stackTop(obj->minStk);
92+
if (val<minTop) {
93+
stackPush(obj->minStk,val);
94+
}else {
95+
stackPush(obj->minStk,minTop);
96+
}
97+
}else {
98+
stackPush(obj->minStk,val);
99+
}
100+
}
101+
102+
voidminStackPop(MinStack*obj) {
103+
assert(obj);
104+
stackPop(obj->stk);
105+
stackPop(obj->minStk);
106+
}
107+
108+
intminStackTop(MinStack*obj) {
109+
assert(obj);
110+
returnstackTop(obj->stk);
111+
}
112+
113+
intminStackGetMin(MinStack*obj) {
114+
assert(obj);
115+
returnstackTop(obj->minStk);
116+
}
117+
118+
voidminStackFree(MinStack*obj) {
119+
assert(obj);
120+
if (obj->stk) {
121+
while(obj->stk->top) {
122+
stackPop(obj->stk);
123+
}
124+
while(obj->minStk->top) {
125+
stackPop(obj->minStk);
126+
}
127+
free(obj->stk);
128+
free(obj->minStk);
129+
}
130+
free(obj);
131+
}
132+
133+
/**
134+
* Your MinStack struct will be instantiated and called as such:
135+
* MinStack* obj = minStackCreate();
136+
* minStackPush(obj, val);
137+
* minStackPop(obj);
138+
* int param_3 = minStackTop(obj);
139+
* int param_4 = minStackGetMin(obj);
140+
* minStackFree(obj);
141+
*/
142+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp