Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 31f8aef

Browse files
committed
add the content of Quack
1 parent c6289b1 commit 31f8aef

File tree

2 files changed

+197
-2
lines changed

2 files changed

+197
-2
lines changed

linkedlist/README.md

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,202 @@ public class Linkedlist {
3838

3939
如果想要删除单链表上的节点, 也非常简单,就是删除过程的逆过程。这里不再赘述。
4040

41+
### 拓展
42+
网上链表相关的代码有比较多,这里可以考虑使用链表来实现队列/栈。
43+
队列:一种先进先出的结构
44+
栈: 一种先进后出的结构
45+
46+
可以看到,栈和队列非常相似。
47+
好了,还是程序猿那句老话,**talk is cheap, show me the code.**
48+
我们来设计一个接口,又可以当栈用,也可以当队列用。栈叫Stack, 队列叫Queue, 那么我们称之为Quack。
49+
50+
---
51+
Quack.h的内容:
52+
```c
53+
#include<stdio.h>
54+
#include <stdlib.h>
55+
56+
typedef struct _node* Quack;
57+
58+
59+
Quack createQuack(void); // create and return Quack
60+
Quack destroyQuack(Quack); // remove the Quack
61+
void push(int, Quack); // put the given integer onto the top of the quack
62+
void qush(int, Quack); // put the given integer onto the bottom of the quack
63+
int pop(Quack); // pop and return the top element on the quack
64+
int isEmptyQuack(Quack); // return 1 is Quack is empty, else 0
65+
void makeEmptyQuack(Quack);// remove all the elements on Quack
66+
void showQuack(Quack); // print the contents of Quack, from the top down
67+
68+
```
69+
70+
在头文件里面写好了函数定义,我们来考虑实现功能。
71+
72+
Quack.c
73+
```c
74+
#include "Quack.h"
75+
#include <limits.h> // for INT_MAX macro.
76+
77+
#define MARKERDATA INT_MAX // dummy data
78+
79+
struct node {
80+
int data;
81+
struct node *next;
82+
};
83+
84+
Quack createQuack(void) {
85+
Quack marker;
86+
marker = malloc(sizeof(struct node));
87+
if (marker == NULL) {
88+
fprintf (stderr, "createQuack: no memory, aborting\n");
89+
exit(1);
90+
}
91+
marker->data = MARKERDATA; // should never be used
92+
marker->next = NULL;
93+
return marker;
94+
}
95+
96+
void push(int data, Quack qs) {
97+
Quack newnode;
98+
if (qs == NULL) {
99+
fprintf(stderr, "push: quack not initialised\n");
100+
}
101+
else {
102+
newnode = malloc(sizeof(struct node));
103+
if (newnode == NULL) {
104+
fprintf(stderr, "push: no memory, aborting\n");
105+
exit(1);
106+
}
107+
newnode->data = data;
108+
newnode->next = qs->next;
109+
qs->next = newnode;
110+
}
111+
return;
112+
}
113+
114+
void qush(int data, Quack qs) {
115+
116+
// code not shown
117+
118+
return;
119+
}
120+
121+
int pop(Quack qs) {
122+
int retval = 0;
123+
if (qs == NULL) {
124+
fprintf(stderr, "pop: quack not initialised\n");
125+
}
126+
else {
127+
if (isEmptyQuack(qs)) {
128+
fprintf(stderr, "pop: quack underflow\n");
129+
}
130+
else {
131+
Quack topnode = qs->next; // top dummy node is always there
132+
retval = topnode->data;
133+
qs->next = topnode->next;
134+
free(topnode);
135+
}
136+
}
137+
return retval;
138+
}
139+
140+
Quack destroyQuack(Quack qs) { // remove the Quack and returns NULL
141+
if (qs == NULL) {
142+
fprintf(stderr, "destroyQuack: quack not initialised\n");
143+
}
144+
else {
145+
Quack ptr = qs->next;
146+
while (ptr != NULL) {
147+
Quack tmp = ptr->next;
148+
free(ptr);
149+
ptr = tmp;
150+
}
151+
free(qs);
152+
}
153+
return NULL;
154+
}
155+
156+
void makeEmptyQuack(Quack qs) {
157+
if (qs == NULL)
158+
fprintf(stderr, "makeEmptyQuack: quack not initialised\n");
159+
else {
160+
while (!isEmptyQuack(qs)) {
161+
pop(qs);
162+
}
163+
}
164+
return;
165+
}
166+
167+
int isEmptyQuack(Quack qs) {
168+
int empty = 0;
169+
if (qs == NULL) {
170+
fprintf(stderr, "isEmptyQuack: quack not initialised\n");
171+
}
172+
else {
173+
empty = qs->next == NULL;
174+
}
175+
return empty;
176+
}
177+
178+
void showQuack(Quack qs) {
179+
if (qs == NULL) {
180+
fprintf(stderr, "showQuack: quack not initialised\n");
181+
}
182+
else {
183+
printf("Quack: ");
184+
if (qs->next == NULL) {
185+
printf("<< >>\n");
186+
}
187+
else {
188+
printf("<<"); // start with <<
189+
qs = qs->next; // step over the marker link
190+
while (qs->next != NULL) {
191+
printf("%d, ", qs->data); // print each element
192+
qs = qs->next;
193+
}
194+
printf("%d>>\n", qs->data); // last element ends with >>
195+
}
196+
}
197+
return;
198+
}
199+
```
200+
201+
那么我们来编写程序来使用,我们来反转字符串。
202+
revarg.c
203+
204+
```c
205+
#include <stdio.h>
206+
#include <stdlib.h>
207+
#include "quack.h"
208+
209+
int main(int argc, char *argv[]) {
210+
Quack s = NULL;
211+
212+
if (argc >= 2) {
213+
char *inputc = argv[1];
214+
s = createQuack();
215+
while (*inputc != '\0') {
216+
push(*inputc++, s);
217+
}
218+
while (!isEmptyQuack(s)) {
219+
printf("%c", pop(s));
220+
}
221+
putchar('\n');
222+
}
223+
return EXIT_SUCCESS;
224+
}
225+
```
226+
编译代码:
227+
```
228+
gcc Quack.c revarg.c
229+
```
230+
运行:
231+
```
232+
./a.out 0123456789
233+
>> 9876543210
234+
```
235+
236+
41237
[LeetCode876 链表中间节点题解](linkedlist/Leetcode876MiddleOftheLinkedList.md)
42238
43239
[Leetcode19 删除倒数n个节点题解](linkedlist/leetcode19.md)

myself/c_style_guide.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,4 @@ int sum(int a, int b) {
237237
- 避免使用 `do {} while(condition)`, 使用while循环来替代
238238
- 不要使用联合体。(union)
239239
- 避免指针的算数操作
240-
- 避免类型转换。
241-
240+
- 避免类型转换。

0 commit comments

Comments
 (0)