Lapping data structures and algorithms-03 stacks and queues

Source: Internet
Author: User

One, stack

public class Mystack {

The underlying implementation is an array

Private long[] arr;

private int top;

/**

* Default method of construction

*/

Public Mystack () {

arr = new LONG[10];

top =-1;

}

/**

* with parameter construction method, parameter 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 if it is empty

*/

public Boolean isEmpty () {

return top = =-1;

}

/**

* Judging if it's full

*/

public Boolean isfull () {

return top = = Arr.length-1;

}

}

Test:

public class Testmystack {

public static void Main (string[] args) {

Mystack ms = new Mystack (4);

Ms.push (23);

Ms.push (12);

Ms.push (1);

Ms.push (90);

System.out.println (Ms.isempty ());

System.out.println (Ms.isfull ());

System.out.println (Ms.peek ());

System.out.println (Ms.peek ());

while (!ms.isempty ()) {

System.out.print (Ms.pop () + ",");

}

System.out.println (Ms.isempty ());

System.out.println (Ms.isfull ());

}

}

Two, the queue


public class Myqueue {

Using arrays at the bottom

Private long[] arr;

Size of valid data

private int elements;

Team Head

private int front;

End of Team

private int end;

/**

* Default Constructor method

*/

Public Myqueue () {

arr = new LONG[10];

elements = 0;

Front = 0;

end =-1;

}

/**

* Construction method with parameters, size of parameter array

*/

Public myqueue (int maxsize) {

arr = new Long[maxsize];

elements = 0;

Front = 0;

end =-1;

}

/**

* Add data, insert from end of team

*/

public void Insert (Long value) {

Arr[++end] = value;

elements++;

}

/**

* Delete data, remove from team header

*/

Public long Remove () {

elements--;

return arr[front++];

}

/**

* View data, view from Team head

*/

Public long Peek () {

return Arr[front];

}

/**

* Determine if it is empty

*/

public Boolean isEmpty () {

return elements = = 0;

}

/**

* Judging if it's full

*/

public Boolean isfull () {

return elements = = Arr.length;

}

}


Three, circular queue


public class Mycyclequeue {

Using arrays at the bottom

Private long[] arr;

Size of valid data

private int elements;

Team Head

private int front;

End of Team

private int end;

/**

* Default Constructor method

*/

Public Mycyclequeue () {

arr = new LONG[10];

elements = 0;

Front = 0;

end =-1;

}

/**

* Construction method with parameters, size of parameter array

*/

Public mycyclequeue (int maxsize) {

arr = new Long[maxsize];

elements = 0;

Front = 0;

end =-1;

}

/**

* Add data, insert from end of team

*/

public void Insert (Long value) {

if (end = = arr.length-1) {

end =-1;

}

Arr[++end] = value;

elements++;

}

/**

* Delete data, remove from team header

*/

Public long Remove () {

Long value = arr[front++];

if (front = = arr.length) {

Front = 0;

}

elements--;

return value;

}

/**

* View data, view from Team head

*/

Public long Peek () {

return Arr[front];

}

/**

* Determine if it is empty

*/

public Boolean isEmpty () {

return elements = = 0;

}

/**

* Judging if it's full

*/

public Boolean isfull () {

return elements = = Arr.length;

}

}

Test:

public class Testmyqueue {

public static void Main (string[] args) {

Mycyclequeue MQ = new Mycyclequeue (4);

Myqueue MQ = new Myqueue (4);

Mq.insert (23);

Mq.insert (45);

Mq.insert (13);

Mq.insert (1);

System.out.println (Mq.isfull ());

System.out.println (Mq.isempty ());

System.out.println (Mq.peek ());

System.out.println (Mq.peek ());

while (!mq.isempty ()) {

System.out.print (Mq.remove () + "");

}

System.out.println ();

Mq.insert (23);

Mq.insert (45);

Mq.insert (13);

Mq.insert (1);

while (!mq.isempty ()) {

System.out.print (Mq.remove () + "");

}

}

}


This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1696275

Lapping data structures and algorithms-03 stacks and queues

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.