8000 linkstack: 链栈的push&pop · home-coder/data-abstraction-001@dee09b3 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit dee09b3

Browse files
author
oneface
committed
linkstack: 链栈的push&pop
1 parent 7425b80 commit dee09b3

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

003-linkstack.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/***************************************************************************
2+
> Filename : 003-linkstack.c
3+
> Author : oneface - one_face@sina.com
4+
> Company : 一尊还酹江月
5+
> Time : 2018-05-05 11:41:22
6+
> Description:
7+
8+
- This program is free software; you can redistribute it and/or modify it
9+
- under the terms of the GNU General Public License as published by the
10+
- Free Software Foundation; either version 2 of the License, or (at your
11+
- option) any later version.
12+
**************************************************************************/
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
16+
typedef struct _lstack {
17+
char data;
18+
struct _lstack *next;
19+
}lstack;
20+
21+
/*
22+
* push & pop
23+
*/
24+
25+
static void lstack_push(lstack **lsk, char da)
26+
{
27+
lstack *ns = (lstack *)malloc(sizeof(lstack));
28+
29+
ns->data = da;
30+
ns->next = *lsk;
31+
*lsk = ns;
32+
}
33+
34+
static void lstack_pop(lstack **lsk)
35+
{
36+
if (!*lsk) {
37+
printf("栈已经空\n");
38+
return ;
39+
}
40+
41+
//free 操作
42+
printf("data: %c\n", (*lsk)->data);
43+
lstack *f = *lsk;
44+
*lsk = (*lsk)->next;
45+
free(f);
46+
}
47+
48+
int main()
49+
{
50+
lstack *lsk = NULL;
51+
52+
lstack_push(&lsk, 'a');
53+
lstack_push(&lsk, 'b');
54+
lstack_pop(&lsk);
55+
lstack_pop(&lsk);
56+
lstack_push(&lsk, 'c');
57+
lstack_push(&lsk, 'd');
58+
lstack_push(&lsk, 'c');
59+
lstack_push(&lsk, 'd');
60+
lstack_push(&lsk, 'c');
61+
lstack_push(&lsk, 'd');
62+
lstack_push(&lsk, 'e');
63+
64+
lstack_pop(&lsk);
65+
lstack_pop(&lsk);
66+
lstack_pop(&lsk);
67+
lstack_pop(&lsk);
68+
lstack_pop(&lsk);
69+
lstack_pop(&lsk);
70+
lstack_pop(&lsk);
71+
lstack_pop(&lsk);
72+
lstack_pop(&lsk);
73+
74+
return 0;
75+
}

006-huffman.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ static void huffman_coding_nodeorder(hftree *h, hfcode **hc, int s)
144144
memcpy((*hc)[i], &cd[start], s - start);
145145
}
146146

147+
free(cd);
147148
}
148149

149150
static void huffman_code_print(hfcode *hc, int s)
@@ -177,6 +178,9 @@ int main()
177178
//huffman_coding_rootorder(hft, 2 * s - 1);
178179

179180
huffman_code_print(hc, s);
181+
182+
free(hc);
180183
free(hft);
184+
181185
return 0;
182186
}

0 commit comments

Comments
 (0)
0