Data Structure-stack, Data Structure

Source: Internet
Author: User

Data Structure-stack, Data Structure

First, let's talk about Stack:

1. Operations on data can only be performed on a fixed end, but cannot be performed on the middle or the other end.

TheData Storage Class (object)Stack.

It should be noted that the stack is applicable to all the data structures of the above two features and can be called stacks, No matter what basic container is used for implementation.

How to implement it:

AvailableArrayOrLinked ListTo implement the stack, We need to block some features of the linked list when using the linked list: Data Operations in the middle of the linked list.

 

Let's take a look at the stack in jdk:

Note that in Stack (Figure 1:Stack inherits the method type from Vactor Stack.

    Vector (Fig 2:Methods in Vector

Stack inherits from Vactor, so Stack can call operations such as set (int index, E element) and insertElementAt (E obj, int index) in the parent class, in this case, there is a conflict with the features of the stack. I have not fully understood this. Is it necessary to remove the second feature? Hope you can give me some advice after seeing it. Thank you!

 

Figure 1: Stack. java

    

Figure 2: Vector. java

Since jdk has its own stack, what else do we know about it?

In some special cases, You Need To encapsulate your stack according to the actual situation.

 

The following are two examples: one is an array stack and the other is a chain table stack.

Array Implementation:

Package com. datastructure;/*** @ author Perkins. zhu * @ date: July 17, 2016 9:13:01 * @ version: 1.1 **/public class ArrayStack implements Stack {private int maxSize; private Object [] myStack; private int top =-1; // private final int DEFAULT_SIZE = 100; private boolean canExpendSize = true; // whether to allow private int multiple = 2; // public ArrayStack () {myStack = new Object [DEFAULT_SIZE];} public ArrayStack (int size, boolean canExpendSize) {if (size <1) {size = DEFAULT_SIZE;} myStack = new Object [size]; this. canExpendSize = canExpendSize;} @ Override public void push (Object obj) {if (top = myStack. length-1) {if (canExpendSize) {exspandSize () ;}else {move () ;}} myStack [++ top] = obj ;}// Delete the bottom element of the stack, all elements move one digit backward and top points to myStack. length-2 private void move () {for (int I = 0; I <myStack. length-1; I ++) {myStack [I] = myStack [I + 1];} top = myStack. length-2;} // resize private void exspandSize () {Object [] temp = new Object [multiple * myStack. length]; for (int I = 0; I <myStack. length; I ++) {temp [I] = myStack [I];} top = myStack. length-1; myStack = temp;} @ Override public Object pop () {if (top =-1) return null; return myStack [top --];} @ Override public boolean isEmpty () {return top =-1 ;}}

 

 

Linked List Implementation: (only basic functions are implemented. You can add parameters or methods as needed)

Package com. datastructure; import java. util. using list;/*** @ author Perkins. zhu * @ date: July 17, 2016 1:16:26 * @ version: 1.1 **/public class LinkStack implements Stack {private Node top; private class Node {private Object obj; private Node nextNode; public Node (Object obj, Node node) {this. obj = obj; this. nextNode = node;} public void push (Object obj) {if (top! = Null) {top = new Node (obj, top);} else {top = new Node (obj, null) ;}} public Object pop () {Node res = top; res. nextNode = null; top = top. nextNode; return res. obj;} public boolean isEmpty () {return top = null ;}}

And then a test class:

Package com. datastructure; import org. junit. test;/*** @ author Perkins. zhu * @ date: July 17, 2016 9:16:50 * @ version: 1.1 **/public class StackTest {@ Test public void testArrayStack () {ArrayStack stack = new ArrayStack (100, false ); int loop = 0; while (loop <150) {stack. push (loop ++);} print (stack);} public void print (Object obj) {if (obj instanceof Stack) {Stack stack Stack = (stack) obj; while (! Stack. isEmpty () {System. out. print (stack. pop () + "") ;}} System. out. println () ;}@ Test public void testLinkStack () {LinkStack stack = new LinkStack (); int loop = 0; while (loop <150) {stack. push (loop ++) ;}print (stack );}}

 

 

Some problems have not been clarified yet. Make a record for the time being. I have encountered the following problems during the course of study. If you have any great gods, please don't hesitate to give me some advice. Thank you!

1. Are there official standard stack definitions?

2. What is the difference between generic T and object? What is the difference between receiving parameters ??

3. Do I need to delete, insert, and search stacks?

4. Is there any stack implementation method besides array and linked list?

This article will be updated later after you clarify these issues.

 

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.