Java-based Data Structure stack code explanation, java Data Structure

Source: Internet
Author: User

Java-based Data Structure stack code explanation, java Data Structure

Recently, I reviewed the data structure and implemented the stack myself. Stack is a type of table that restricts the insertion and deletion of only one position. The most basic operation is stack import and exit. Therefore, it is also called an "advanced post-release" table.

First, understand the concept of the next stack:

Stack is a linear table that only inserts and deletes data from the table header. Sometimes it is also called LIFO ). To understand this concept, we must first understand the Original Meaning of "stack" so that we can grasp the essence.

"Stack", stores goods or places for passengers to stay, can be extended to warehouses, transfer stations, so introduced to the computer field, is to index the temporary storage of data, so there is a saying that the stack goes into and out.

The implementation method is as follows: first define an interface, and then implement linear stack and chain stack through this interface. The code is relatively simple, as shown below:

Package com. peter. java. dsa. interfaces;/*** Stack operation definition ** @ author Peter Pan */public interface Stack <T> {/* empty */Boolean isEmpty (); /* clear stack */void clear ();/* Roll stack */T pop ();/* inbound stack */Boolean push (T data ); /* stack length */int length ();/* view the elements at the top of the stack, but do not remove it */T peek (); /* return the position of the object in the stack */int search (T data );}

Linear Stack: Implemented in array mode.

Package com. peter. java. dsa. common; import com. peter. java. dsa. interfaces. stack;/*** linear Stack *** @ author Peter Pan */public class LinearStack <T> implements Stack <T >{@ SuppressWarnings ("unchecked ") private T [] t = (T []) new Object [16]; private int size = 0; @ Override public Boolean isEmpty () {// TODO Auto-generated method stubreturn size = 0;} @ Override public void clear () {// TODO Auto-generated method stubfor (int I = 0; I <t. length; I ++) {t [I] = null;} size = 0 ;}@ Override public T pop () {// TODO Auto-generated method stubif (size = 0) {return null;} T tmp = t [size-1]; t [size-1] = null; size --; return tmp ;}@ Override public Boolean push (T data) {// TODO Auto-generated method stubif (size> = t. length) {resize () ;}t [size ++] = data; return true ;}@ Override public int length () {// TODO Auto-generated method stubreturn size ;} @ Override public T peek () {// TODO Auto-generated method stubif (size = 0) {return null;} else {return t [size-1];} /* return index of data, return-1 if no data */@ Override public int search (T data) {// TODO Auto-generated method stubint index =-1; for (int I = 0; I <t. length; I ++) {if (t [I]. equals (data) {index = I; break;} return index;} @ SuppressWarnings ("unchecked") private void resize () {T [] tmp = (T []) new Object [t. length * 2]; for (int I = 0; I <t. length; I ++) {tmp [I] = t [I]; t [I] = null;} t = tmp; tmp = null ;} /* from the left to the right is from the top to the bottom of the stack */@ Override public String toString () {// TODO Auto-generated method stubStringBuffer buffer = new StringBuffer (); buffer. append ("Linear Stack Content: ["); for (int I = t. length-1; I>-1; I --) {buffer. append (t [I]. toString () + ",");} buffer. append ("]"); buffer. replace (buffer. lastIndexOf (","), buffer. lastIndexOf (",") + 1, ""); return buffer. toString ();}}

Chain Stack: implemented through a single-chain table.

package com.peter.java.dsa.common;import com.peter.java.dsa.interfaces.Stack;public class LinkedStack<T> implements Stack<T> {private Node top;private int size;@Override public Boolean isEmpty() {// TODO Auto-generated method stubreturn size == 0;}@Override public void clear() {// TODO Auto-generated method stubtop = null;size = 0;}@Override public T pop() {// TODO Auto-generated method stubT topValue = null;if (top != null) {topValue = top.data;Node oldTop = top;top = top.prev;oldTop.prev = null;size--;}return topValue;}@Override public Boolean push(T data) {// TODO Auto-generated method stubNode oldTop = top;top = new Node(data);top.prev = oldTop;size++;return true;}@Override public int length() {// TODO Auto-generated method stubreturn size;}@Override public T peek() {// TODO Auto-generated method stubT topValue = null;if (top != null) {topValue = top.data;}return topValue;}@Override public int search(T data) {// TODO Auto-generated method stubint index = -1;Node tmp = top;for (int i = size - 1; i > -1; i--) {if (tmp.data.equals(data)) {index = i;break;} else {tmp = tmp.prev;}}tmp = null;return index;}@Override public String toString() {// TODO Auto-generated method stubStringBuffer buffer = new StringBuffer();buffer.append("Linked Stack Content:[");Node tmp = top;for (int i = 0; i < size - 1; i++) {buffer.append(tmp.toString() + ",");tmp = tmp.prev;}tmp = null;buffer.append("]");buffer.replace(buffer.lastIndexOf(","), buffer.lastIndexOf(",") + 1, "");return super.toString();}private class Node {T data;Node prev;public Node(T data) {// TODO Auto-generated constructor stubthis.data = data;}}}

The learning is still in progress and the code will be updated later.

This is the full description of the Code for implementing the data structure stack in Java, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.