blog posts

databases (1) django (1) drupal (2) java (4) processing (7) wamp (1) yahoo pipes (1)

Sunday, 13 February 2011

java stack

What is a stack. A stack is used my the operating system. So they are very useful to learn about.
A stack in java has certain main methods
push
pop

Before we go onto programming a stack we are going to look at the logic of a stack they are known as LIFO last in first out and this I will demonstrate for you now how a stack works
 This is an example of pushing an item onto the stack. 
In java you create an instance of the stack class and there is a method called push you then pass to the stack class what you want to add 
eg 
Stack countries = new Stack()
countries.push("BRAZIL")

Now if you want to add another item we could add the line
countries.push("ENGLAND");
So now if we remove an item from a stack which item do you think will be removed well since its Last in first Out. The last item we added is england so this is removed first. 

To remove an item from a stack you use the same instance of the stack class that we created earlier. Instead of adding an item to the stack we pop it from the stack
stack.pop();

Why do I need to know how a stack works
Stack are very important in computer science. A stack is made up stack frames These are machine dependent data structure containing relevent subroutine state information. Each of these frame are assigned to a call to a subroutine which has not yet been terminated. 
eg if we have a sub routine DrawPint and its running. And it has just been called be a subroutine called DrawLine having called by a DrawSquare subroutine.

The stack layout is shown in an image below 






No comments:

Post a Comment

Popular Posts