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

Commitc7a3655

Browse files
authored
Merge pull requestneetcode-gh#1930 from seinlin/225
Create 0225-implement-stack-using-queue.c
2 parentseee4d89 +8652b8c commitc7a3655

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

‎c/0225-implement-stack-using-queue.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// A structure to represent an element of the queue.
2+
typedefstructNode {
3+
intvalue;
4+
structNode*next;
5+
}Node;
6+
7+
typedefstructMyQueue {
8+
intsize;
9+
Node*front;
10+
Node*rear;
11+
}MyQueue;
12+
13+
MyQueue*MyQueueCreate() {
14+
MyQueue*queue= (MyQueue*)malloc(sizeof(MyQueue));
15+
queue->size=0;
16+
queue->front=NULL;
17+
queue->rear=NULL;
18+
returnqueue;
19+
}
20+
21+
voidMyQueuePush(MyQueue**obj,intval) {
22+
assert(obj);
23+
assert(*obj);
24+
MyQueue*queue=*obj;
25+
Node*node= (Node*)malloc(sizeof(Node));
26+
node->value=val;
27+
node->next=NULL;
28+
if (queue->rear) {
29+
queue->rear->next=node;
30+
queue->rear=node;
31+
}else {
32+
queue->front=node;
33+
queue->rear=node;
34+
}
35+
queue->size++;
36+
}
37+
38+
voidMyQueuePop(MyQueue**obj) {
39+
assert(obj);
40+
assert(*obj);
41+
MyQueue*queue=*obj;
42+
Node*tmp=queue->front;
43+
if (queue->size>0&&tmp) {
44+
queue->front=queue->front->next;
45+
queue->size--;
46+
free(tmp);
47+
}
48+
if (queue->rear==tmp) {
49+
queue->rear=NULL;
50+
}
51+
}
52+
53+
intMyQueueFront(MyQueue**obj) {
54+
assert(obj);
55+
MyQueue*queue=*obj;
56+
assert(queue->front);
57+
returnqueue->front->value;
58+
}
59+
60+
intMyQueueRear(MyQueue**obj) {
61+
assert(obj);
62+
MyQueue*queue=*obj;
63+
assert(queue->rear);
64+
returnqueue->rear->value;
65+
}
66+
67+
intMyQueueSize(MyQueue**obj) {
68+
assert(obj);
69+
assert(*obj);
70+
return (*obj)->size;
71+
}
72+
73+
typedefstruct {
74+
MyQueue*queue;
75+
}MyStack;
76+
77+
78+
MyStack*myStackCreate() {
79+
MyStack*stack= (MyStack*)malloc(sizeof(MyStack));
80+
stack->queue=NULL;
81+
returnstack;
82+
}
83+
84+
voidmyStackPush(MyStack*obj,intx) {
85+
assert(obj);
86+
intsize;
87+
if (!obj->queue) {
88+
obj->queue=MyQueueCreate();
89+
}
90+
MyQueuePush(&obj->queue,x);
91+
size=obj->queue->size;
92+
while(--size>0) {
93+
MyQueuePush(&obj->queue,MyQueueFront(&obj->queue));
94+
MyQueuePop(&obj->queue);
95+
}
96+
}
97+
98+
intmyStackPop(MyStack*obj) {
99+
assert(obj);
100+
intval=MyQueueFront(&obj->queue);
101+
MyQueuePop(&obj->queue);
102+
returnval;
103+
}
104+
105+
intmyStackTop(MyStack*obj) {
106+
assert(obj);
107+
assert(obj->queue);
108+
returnMyQueueFront(&obj->queue);
109+
}
110+
111+
boolmyStackEmpty(MyStack*obj) {
112+
assert(obj);
113+
if (obj->queue) {
114+
returnobj->queue->size==0;
115+
}
116+
return true;
117+
}
118+
119+
voidmyStackFree(MyStack*obj) {
120+
if(obj) {
121+
while(obj->queue&&obj->queue->front) {
122+
MyQueuePop(&obj->queue);
123+
}
124+
free(obj);
125+
}
126+
}
127+
128+
/**
129+
* Your MyStack struct will be instantiated and called as such:
130+
* MyStack* obj = myStackCreate();
131+
* myStackPush(obj, x);
132+
* int param_2 = myStackPop(obj);
133+
* int param_3 = myStackTop(obj);
134+
* bool param_4 = myStackEmpty(obj);
135+
* myStackFree(obj);
136+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp