Mobile development stack-Basic queue knowledge learning and basic queue knowledge

Source: Internet
Author: User

Mobile development stack-Basic queue knowledge learning and basic queue knowledge
Stack

First-in-first-out and later-in-first-out

Package ch03; public class MyStack {// The underlying implementation is an array private long [] arr; private int top; // stack top/*** default constructor */public MyStack () {arr = new long [10]; top =-1 ;} /*** construction method with parameters. The parameter is the array initialization size */public MyStack (int maxsize) {arr = new long [maxsize]; top =-1 ;} /*** add data */public void push (int value) {arr [++ top] = value;}/*** remove data */public long pop () {return arr [top --];}/*** View data */public long peek () {return arr [top];} /*** determine whether it is empty */public boolean isEmpty () {return top =-1;}/*** determine whether it is full */public boolean isFull () {return top = arr. length-1 ;}}

 

Queue

First-in, first-out, and then-out

Package ch03;/** queue class */public class MyQueue {// The underlying layer uses the array private long [] arr; // the size of valid data private int elements; // private int front of the team head; // private int end of the team end;/*** default constructor */public MyQueue () {arr = new long [10]; elements = 0; front = 0; end =-1;}/*** construction method with parameters, the parameter is the size of the array */public MyQueue (int maxsize) {arr = new long [maxsize]; elements = 0; front = 0; end =-1;}/*** add data, insert */public void insert (long value) {arr [++ end] = value; elements ++;}/*** from the end of the team to delete the data, delete */public long remove () {elements --; return arr [front ++];}/*** from the team header to view data, view */public long peek () {return arr [front];}/*** from the queue header to determine whether it is null */public boolean isEmpty () {return elements = 0;}/*** determines if it is full */public boolean isFull () {return elements = arr. length ;}}
Cyclic queue

When inserting data, when the end of the team points to the length of the array, set the end value to-1.
When deleting data, when the length of the array is exceeded, set the value to 0.

Package ch03;/** queue class */public class MyCycleQueue {// The underlying layer uses the array private long [] arr; // the size of valid data private int elements; // private int front of the team head; // private int end of the team end;/*** default constructor */public MyCycleQueue () {arr = new long [10]; elements = 0; front = 0; end =-1;}/*** construction method with parameters, the parameter is the size of the array */public MyCycleQueue (int maxsize) {arr = new long [maxsize]; elements = 0; front = 0; end =-1;}/*** add data, insert */public void insert (long value) {if (end = arr. length-1) {end =-1;} arr [++ end] = value; elements ++;}/*** delete data, delete */public long remove () {long value = arr [front ++]; if (front = arr. length) {front = 0;} elements --; return value;}/*** View data, View data from the team header */public long peek () {return arr [front];}/*** determines whether it is empty */public boolean isEmpty () {return elements = 0 ;} /*** determine if it is full */public boolean isFull () {return elements = arr. length ;}}

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.