Stack and cyclic queue using arrays (Java language)

Source: Internet
Author: User

This article will address two issues. First, how to use arrays to implement a stack, and the stack size can be scaled. Second, an array is used to implement a queue. In order to fully utilize the array space, a loop is implemented here to form a cyclic queue.

(1) Implement Stack Based on Array

Package COM. datastructure. stack; import Java. util. *; public class mystack <t> {private object [] array = NULL; private int Capality; private final int defaultsize = 10; private int size = 0; private int Top =-1; Public mystack () {This. capality = This. defaultsize; this. array = new object [this. capality]; this. size = 0;} public mystack (INT Capality) {This. capality = Capality; this. array = new object [Capality]; this. size = 0;} public boo Lean isempty () {return size = 0;} public int size () {return this. size;} public void push (t obj) {If (this. size <this. capality) {This. top ++; this. array [Top] = OBJ; this. size ++;} else {enablecapality (); push (OBJ) ;}} private void enablecapality () {This. capality = This. capality * 2; object [] newarray = new object [this. capality]; system. arraycopy (array, 0, newarray, 0, this. size); this. array = newarray;} public t POP () {If (isempty ()) {Try {Throw new exception ("Stack is empty! ");} Catch (exception e) {// todo auto-generated catch blocke. printstacktrace () ;}t temp = (t) array [this. top]; array [this. top] = NULL; this. top --; this. size --; return temp;} public static void main (string [] ARGs) {// todo auto-generated method stubmystack <integer> stack = new mystack <integer> (); stack. push (1); stack. push (5); stack. push (2); stack. push (4); stack. push (3); While (! Stack. isempty () {system. Out. println (stack. Pop ());}}}

The running result is as follows:

34251

(2) Implement cyclic queue based on Arrays

Package COM. datastructure. stack; public class myqueue {private object [] array = NULL; private int size; private int front; private int back; private Boolean beginflag = true; private Boolean endflag = false; public myqueue (INT size) {array = new object [size]; this. size = size; front = 0; back = 0;} public myqueue () {This (10);} public void enqueue (Object OBJ) {If (isfull ()) {try {Throw new exception ("the queue is full! ");} Catch (exception e) {// todo auto-generated catch blocke. printstacktrace () ;}} array [Back] = OBJ; back = (back + 1) % size;} public object dequeue () {If (isempty ()) try {Throw new exception ("the queue is empty! ");} Catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} object temp = array [Front]; array [Front] = NULL; front = (front + 1) % size; return temp;} public Boolean isempty () {If (front = back) {If (! Endflag) {endflag = true; return false;} return true;} elsereturn false;} private Boolean isfull () {If (back + 1) % size = front) {If (beginflag) {beginflag = false; return false;} return true;} else {return false;} public static void main (string [] ARGs) {// todo auto-generated method stubmyqueue queue = new myqueue (5); queue. enqueue (1); queue. enqueue (2); queue. enqueue (3); queue. enqueue (4); queue. enqueue (5); system. out. println (queue. dequeue (); system. out. println (queue. dequeue (); system. out. println (queue. dequeue (); queue. enqueue (5); system. out. println (queue. dequeue (); system. out. println (queue. dequeue (); system. out. println (queue. dequeue (); queue. enqueue (6); system. out. println (queue. dequeue ());}}

The running result is as follows:

1234556

 

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.