Get the App
SLTechnology News&Howtos  ›  Development  › 

The usage of Stack in Java data structure

Shulou Source: shulou.com Published: 2022-06-03 09:53:58 09月11日 Update

In this issue, Xiaobian will bring you about the use of stacks in Java data structures. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

Stack is a special first-in-last-out linear table, allowing only insertion and deletion at the end of the table. Two ways to implement stack will be introduced later, which are array-based implementation and link-based implementation.

class Mystack{public: Mystack() {} virtual void push(int &x) = 0; virtual bool pop(int &x) = 0; virtual bool Top(int &x) const = 0; virtual bool IsEmpty()const = 0; virtual bool IsFull()const = 0; virtual int getSize()const = 0;}; Sequential Stack----------Use arrays to represent stack space

Definition:

#pragma once#include "Mystack.h"#include #include using namespace std;const int stackIncreament = 20;class SeqStack : public Mystack{public: SeqStack(int sz = 50); //Create an empty stack ~SeqStack() { delete[]elements; } //destructor //overflow program processing if stack is full, otherwise insert x void push(int &x); //If the stack is empty, return FALSE, otherwise use x to pass the value at the top of the stack, top-1 bool pop(int &x); //returns FALSE if stack is empty, otherwise uses x to pass stack top value bool Top(int &x); //determine whether the stack is empty bool IsEmpty()const { return (top == -1) ? true : false; } //determine whether the stack is full bool IsFull()const { return (top == maxSize - 1) ? true : false; } //Get the current stack size int getSize()const { return top + 1; } //Leave the stack empty void MakeEmpty() { top = -1; } //overload operation

Tags: Array space processing data data structure structure element content pointer program analysis distribution maximum special successful professional small and medium advanced memory rich in content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Apple MySQL Docker