Adicionado Stack usando as Foundation.h
[blacktomato.git] / Stack.m
blob6dfe5ef89da8ff2393e4cfa20b0b0ecb13770373
1 #import <stdlib.h>
2 #import "Stack.h"
4 @implementation Stack
6 #define NULL_LINK (StackLink *) 0
8 + new
10   self = [super new];
11   top = NULL_LINK;
12   return self;
15 - free
17   StackLink *next;
19   while (top != NULL_LINK)
20     {
21       next = top->next;
22       free ((char *) top);
23       top = next;
24     }
25   return [super free];
28 - push: (int) value
30   StackLink *newLink;
31   
32   newLink = (StackLink *) malloc (sizeof (StackLink));
33   if (newLink == 0)
34     {
35       fprintf(stderr, "Out of memory\n");
36       return nil;
37     }
38   newLink->data = value;
39   newLink->next = top;
40   top = newLink;
41   size++;
42   
43   return self;
46 - (int) pop
48   int value;
49   StackLink *topLink;
51   if (0 != size)
52     {
53       topLink = top;
54       top = top->next;
55       value = topLink->data;
56       free (topLink);
57       size--;
58     }
59   else
60     {
61       value = 0;
62     }
63   return value;
66 - (unsigned int) size
68   return size;
71 - print
73   StackLink *startLink;
74   int i = size;
76   startLink = top;
77   
78   while (startLink) {
79     printf("Stack[%d] = %d\n", i, startLink->data);
80     startLink = startLink->next;
81     i--;
82   }
83   return self;
86 @end