Systemnahe Programmierung in C: Dynamische Datenstrukturen: Zeiger |
1typedef struct {
2 int a[20];
3 unsigned int topPtr;
4} * Stack;
5
6void push(Stack s, int v) {
7 s->a[s->topPtr++] = v;
8}
9
10int top(Stack s) {
11 return s->a[s->topPtr -1];
12}
13
14void pop(Stack s) {
15 --s->topPtr;
16}
17
18int isEmpty(Stack s) {
19 return s->topPtr == 0;
20}
21
22#include "stdlib.h"
23
24Stack mkStack(void) {
25 Stack res = malloc(sizeof (*res));
26 if (! res) exit(1);
27 res->topPtr = 0;
28 return res;
29}
30
31
|
1#include "Stack3.c"
2
3int main(void)
4{
5 Stack * s1 = mkStack();
6 Stack * s2 = mkStack();
7
8 push(s1,...);
9
10 ... top(s1) ...;
11
12 pop(s2);
13
14 ... ( isEmpty(s2) )
15
16 push(s2,...);
17
18 pop(s1);
19
20}
|
struct { | typedef struct {
int a[20]; int a[20];
unsigned int topPtr; unsigned int topPtr;
} stack= {{0}, 0}; | } * Stack;
void push(int v) { | void push(Stack s, int v) {
stack.a[stack.topPtr++] = v; | s->a[s->topPtr++] = v;
} }
int top(void) { | int top(Stack s) {
return stack.a[stack.topPtr -1 | return s->a[s->topPtr -1];
} }
void pop(void) { | void pop(Stack s) {
--stack.topPtr; | --s->topPtr;
} }
int isEmpty(void) { | int isEmpty(Stack s) {
return stack.topPtr == 0; | return s->topPtr == 0;
} }
> #include "stdlib.h"
>
> Stack mkStack(void) {
> Stack res = malloc(sizeof (*res));
> if (! res) exit(1);
> res->topPtr = 0;
> return res;
> }
>
>
|
|
Letzte Änderung: 04.11.2005 | © Prof. Dr. Uwe Schmidt |