Systemnahe Programmierung in C: Dynamisches Binden durch Funktionszeiger |
1#ifndef Stack__
2#define Stack__ 1
3
4typedef char *Element;
5
6typedef struct Stack *Stack;
7
8extern Stack push (Stack s, Element e);
9extern Stack pop (Stack s);
10extern Element top (Stack s);
11extern int isEmpty (Stack s);
12
13typedef struct StackMethods *StackMethods;
14
15struct Stack
16{
17 StackMethods methods;
18 void *data;
19};
20
21struct StackMethods
22{
23 Stack (*push) (Stack s, Element e);
24 Stack (*pop) (Stack s);
25 Element (*top) (Stack s);
26 int (*isEmpty) (Stack s);
27};
28
29#endif
|
1#include "Stack.h"
2
3extern Stack
4push (Stack s, Element e)
5{
6 return s->methods->push (s, e);
7}
8
9extern Stack
10pop (Stack s)
11{
12 return s->methods->pop (s);
13}
14
15extern Element
16top (Stack s)
17{
18 return s->methods->top (s);
19}
20
21extern int
22isEmpty (Stack s)
23{
24 return s->methods->isEmpty (s);
25}
|
1#ifndef StackAsList__
2#define StackAsList 1
3
4#include "Stack.h"
5
6extern Stack mkListStack (void);
7
8#endif
|
1#include "StackAsList.h"
2#include <stdlib.h>
3
4typedef struct Node *List;
5
6struct Node
7{
8 Element info;
9 List next;
10};
11
12List mkEmptyList (void);
13int isEmptyList (List l);
14Element head (List l);
15List tail (List l);
16List cons (Element e, List l);
17
18static Stack
19pushLL (Stack s, Element e)
20{
21 s->data = cons (e, (List) (s->data));
22 return s;
23}
24
25static Stack
26popLL (Stack s)
27{
28 s->data = tail ((List) (s->data));
29 return s;
30}
31
32static Element
33topLL (Stack s)
34{
35 return head ((List) (s->data));
36}
37
38static int
39isEmptyLL (Stack s)
40{
41 return isEmptyList ((List) (s->data));
42}
43
44static struct StackMethods methodsVector = {
45 pushLL,
46 popLL,
47 topLL,
48 isEmptyLL
49};
50
51extern Stack
52mkListStack (void)
53{
54 Stack res = malloc (sizeof *res);
55 if (!res)
56 {
57 exit (1);
58 }
59
60 res->methods = &methodsVector;
61 res->data = mkEmptyList ();
62
63 return res;
64}
|
1#ifndef StackAsArray__
2#define StackAsArray__ 1
3
4#include "Stack.h"
5
6#define STACK_CAPACITY 20
7
8extern Stack mkArrayStack (void);
9
10#endif
|
1#include "StackAsArray.h"
2
3#include <stdlib.h>
4
5typedef struct
6{
7 unsigned topptr;
8 Element elems[STACK_CAPACITY];
9}
10 *Array;
11
12
13static Stack
14pushArr (Stack s, Element e)
15{
16 Array data = s->data;
17 data->elems[data->topptr++] = e;
18 return s;
19}
20
21static Stack
22popArr (Stack s)
23{
24 Array data = s->data;
25 --(data->topptr);
26 return s;
27}
28
29static Element
30topArr (Stack s)
31{
32 Array data = s->data;
33
34 return data->elems[data->topptr - 1];
35}
36
37static int
38isEmptyArr (Stack s)
39{
40 Array data = s->data;
41 return data->topptr == 0;
42}
43
44static struct StackMethods methodsVector = {
45 pushArr,
46 popArr,
47 topArr,
48 isEmptyArr
49};
50
51Stack
52mkArrayStack (void)
53{
54 Stack res = malloc (sizeof *res);
55
56 if (res)
57 {
58 Array data = malloc (sizeof *data);
59 if (data)
60 {
61 data->topptr = 0;
62
63 res->methods = &methodsVector;
64 res->data = data;
65 return res;
66 }
67 }
68
69 exit (1);
70}
|
1#include "Stack.h"
2#include "StackAsList.h"
3#include "StackAsArray.h"
4
5int
6main (void)
7{
8
9 Stack s1 = mkListStack ();
10 Stack s2 = mkArrayStack ();
11
12 s1 = push (push (s1, "23"), "42");
13 s2 = push (push (s2, top (s1)), "emil");
14
15 return 0;
16}
|
Letzte Änderung: 11.01.2007 | © Prof. Dr. Uwe Schmidt |