Contents    Page-10    Prev    Next    Page+10    Index   

Stack in Plain Code

A stack is so simple that it can be written as plain code, without making a class out of it.

Stack using Linked List:

Cons mystack = null;       // init to empty

mystack = cons(item, mystack);  // push

item = first(mystack);          // pop
mystack = rest(mystack);

Stack using Array:

int [] mystack = new int[50];
int mystackp = 0;    // init to empty

mystack[mystackp++] = item;    // push

item = mystack[--mystackp];    // pop