Linked list implementation of the stack, in the imagination of the stack, it is best to think of him vertically as a standing container.
Then add a node to the inside one:
1. Join top node first
2. Then join the Node1 node on the top node, and the Node1 node points to the top node
3. Add the Node2 node to the Node1 node, and the Node2 node points to the Node1 node
..........
The following describes the code for implementing a chained stack in Java:
Stack.java-Link Stack interface
1 PackageCom.yeyan.linkstack;2 /**3 * Link Stack interface4 * @authorYeyan5 * @date 2014-12-116 * API:7 * Push (Object obj) into the stack8 * POP () out stack9 * GETTOP () gets the top element of the stackTen * ISEMPTY () Determine if the stack is empty One */ A Public InterfaceStack { - //into the stack - Public voidPush (Object obj)throwsException; the //out of the stack - PublicObject pop ()throwsException; - //get top of stack element - PublicObject GetTop ()throwsException; + //determine if the stack is empty - Public BooleanIsEmpty ()throwsException; +}
Node.java node data types for chained stacks
1 PackageCom.yeyan.linkstack;2 /**3 * Linked list node data structure4 * @authorYeyan5 * @date 2012-12-116 */7 Public classNode {8 //data fields9 PublicObject data;Ten //pointer field, pointing to the next node One PublicNode Next; A - //first Node constructor - PublicNode (Object data) { the This. data =data; - This. Next =NULL; - } - + //other node Constructors - Publicnode (Object data, node next) { + This. data =data; A This. Next =Next; at } - - /** - * Get Next node - */ - PublicNode GetNext () { in return This. Next; - } to /** + * Set Next node - */ the Public voidSetnext (Node next) { * This. Next =Next; $ }Panax Notoginseng /** - * Get current node data the */ + PublicObject GetData () { A return This. Data; the } + /** - * Set current node data $ * @paramData $ */ - Public voidsetData (Object data) { - This. data =data; the } -}
The implementation class of Linkstack.java chain-stack
1 PackageCom.yeyan.linkstack;2 /**3 * The implementation of chain-stack4 * @authorYeyan5 * @date 2012-12-116 */7 Public classLinkstackImplementsStack {8 PrivateNode top;//chained stack top node9 Private intSize//Number of nodesTen One PublicLinkstack () { Atop =NULL; -Size = 0; - } the /** - * into the stack - */ - @Override + Public voidPush (Object obj)throwsException { -Node node =NewNode (obj); + if(IsEmpty ()) { Atop =node; at}Else{ - Node.setnext (top); -top =node; - } -Size + +; - } in /** - * out of the stack to */ + @Override - PublicObject pop ()throwsException { the if(IsEmpty ()) { * Throw NewException ("Stack is empty!") "); $ }Panax NotoginsengObject obj =Top.getdata (); -top =Top.getnext (); theSize--; + returnobj; A } the + /** - * Get top of stack element $ */ $ @Override - PublicObject GetTop ()throwsException { - if(IsEmpty ()) { the Throw NewException ("Stack is empty!") "); - }WuyiObject obj =Top.getdata (); the returnobj; - } Wu - /** About * Determine if the stack node is empty $ */ - @Override - Public BooleanIsEmpty ()throwsException { - returntop = =NULL; A } + the}
Well, the implementation of the chain stack is also very simple. Oh, welcome to continue to focus on the update of the algorithm ~
The implementation of chain-stack