494 Chapter 11 Abstract Data Types and Encapsulation Constructs
// stack.m - interface and implementation of a simple stack
#import <Foundation/Foundation.h>
// Interface section
@interface Stack: NSObject {
int stackArray [100];
int stackPtr;
int maxLen;
int topSub;
}
-(void) push: (int) number;
-(void) pop;
-(int) top;
-(int) empty;
@end
// Implementation section
@implementation Stack
-(Stack *) initWith {
maxLen = 100;
topSub = -1;
stackPtr = stackArray;
return self;
}
-(void) push: (int) number {
if (topSub == maxLen)
NSLog(@"Error in push--stack is full");
else
stackPtr[++topSub] = number;
}
-(void) pop {
if (topSub == -1)
NSLog(@"Error in pop--stack is empty");
else
topSub--;
}
-(int) top {
if (topSub >= 0)
return stackPtr[topSub]);
else
NSLog(@"Error in top--stack is empty");