#import "Stack.h" #import "Node.h" #import main (void){ id S = [Stack new]; // Create a new Stack char value[80]; while(1){ fgets(value, 80, stdin); switch(value[0]){ case 'P': // Big P pops the top value off the stack if([S empty] != 1){ printf("Pop: %d\n", [[S pop] getValue]); } else{ printf("Empty Stack\n"); } break; case 'p': // Lowercase p peeks the top of the stack if([S empty] != 1){ printf("Peek: %d\n", [[S peek] getValue]); } else{ printf("Empty Stack\n"); } break; case 'f': // Display all values in the stack [S displayStack]; break; default: // Add the integer value of whatever the input was to the stack (push) [S push: atoi(value)]; break; } } } #import /** Author: Devin Eldreth Node Header Information */ @interface Node: Object{ int value; // The integer value of the node id previous, next; // Previous and next Nodes relative to this node } - (id) getPrevious; // Returns a pointer to the previous node - (id) getNext; // Returns a pointer to the next node - (void) setPrevious: (id) pass; // Sets the pointer to this Node's previous node to a node 'pass' - (void) setNext: (id) pass; // Sets the pointer to this Node's next node to a node 'pass' - (int) getValue; // Gets the value of the node - (void) setValue: (int) pass; // Sets the value of the node to an integer value 'pass' @end #import #import "Node.h" #import /** Author: Devin Eldreth Implementation of the Node interface. */ @implementation Node{ } - (id) getPrevious{ return previous; } - (id) getNext{ return next; } - (void) setPrevious: (id) pass{ previous = pass; } - (void) setNext: (id) pass{ next = pass; } - (int) getValue{ return value; } - (void) setValue: (int) pass{ value = pass; } @end Stack.h #import /** Author: Devin Eldreth Header information for an Objective-C Stack */ @interface Stack: Object{ id top; // Point to the top of the stack int size; // Store the size of the stack } - (id) pop; // Get the top most value of the stack, removing it - (void) push: (int) value; // Put the value parameter onto the stack - (id) peek; // Get the top most element on the stack, but don't remove it. - (void) displayStack; // Display all of the values on the stack - (int) empty; // Check to see if Stack is empty @end Stack.m #import "Stack.h" #import "Node.h" #import /** Author: Devin Eldreth Implementation of a Stack in Objective-C This simple Stack uses elements referred to as Nodes (objects with a knowledge of value of previous for a Stack). In the case of a Queue these nodes would have knowledge of next. In a doubly linked list both previous and next. */ @implementation Stack{ } // Pop the top most Node from the stack, removing it, and decrementing the size of the stack. - (id) pop{ if(size > 0){ id ret = top; top = [top getPrevious]; size--; return ret; } else{ return NULL; } } // Return the top most Node on the Stack, do not remove it, and do not decrement the size of the Stack. - (id) peek{ if(size > 0){ return top; } else{ return NULL; } } // Push an integer value onto the top of the Stack. If the stack is new create a new Node to be the top. - (void) push: (int) passed{ if(size == 0){ top = [Node new]; [top setValue: passed]; [top setPrevious: NULL]; } else{ id newNode = [Node new]; id previous = top; [newNode setValue: passed]; top = newNode; [top setPrevious: previous]; } size++; } // Return 1 if the Stack is empty and 0 if it is not empty - (int) empty{ if(size == 0){ return 1; } else{ return 0; } } // Display all of the values held inside of the Stack. - (void) displayStack{ id counter; int position = size; if(size == 0){ printf("Empty Stack\n"); } else{ counter = top; while(counter != NULL){ printf("Stack[%d]: %d\n", position, [counter getValue]); counter = [counter getPrevious]; position--; } printf("\n"); } } @end
Thursday, March 13, 2008
Objective-C Stack & Node Implementation
After creating a Struct oriented Stack in C I decided it might not be a bad idea to work on a similar implementation in Objective-C. This simple program actually tries to mimic the function of the java.util.Stack data collection. It, however, does not inherit any of the class definitions that java.util.Stack does.
This Objective-C implementation of a Stack utilizes a class called Node. Node functionally serves as an object which only has three basic things. A next, previous and value. The Node class contains methods which are used to access each Node’s next, previous, and value. But functionally the Node has nothing to do with the algorithm of the Stack, other than through the use of its previous node.
In a Stack, or in my implementation of a Stack, the Node only needs to reference its previous node. A stack does not ever need to know of its next node. If it did it wouldn’t be a Stack! Data structures such as a doubly linked list, which contain elements (in this case Node’s) that have knowledge of both their next and previous elements can benefit from this setup. My Node class simply is ready to be used in the cases of other data structures such as Queues.
Here are the snippets of source code from my Objective-C implementation of a Stack.
Code:
main.m
Node.h
Node.m
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment