【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-TextStackAndQueue.java,希望本篇文章对您学习有所帮助。
package com.zf.s4;//鍒涘缓涓涓寘
class Stack { // 瀹炵幇椤哄簭鏍堢殑绫?
long stackArray[];// 鏍堟暟缁?
int size;// 鏍堢殑澶у皬
int top;// 鏍堢殑椤堕儴
public Stack(int size) {// 鏋勯犳柟娉曞垵濮嬪寲澶у皬涓簊ize鐨勬爤
this.size = size;
this.stackArray = new long[size];
this.top = -1;
}
public long pop() {// 鍑烘爤鎿嶄綔
return stackArray[top--];
}
public void push(long value) {// 鍏ユ爤鎿嶄綔
stackArray[++top] = value;
}
public boolean isEmpty() {// 鍒ゆ柇鏍堟槸鍚︿负绌?
return top == -1;
}
public boolean isFull() {// 鍒ゆ柇鏍堟槸鍚﹀凡婊?
return top == size - 1;
}
public long peek() {// 鍙栨爤椤跺厓绱?
return stackArray[top];
}
}
class Queue {// 瀹炵幇椤哄簭闃熷垪鐨勭被
private long queueArray[];// 闃熷垪鏁扮粍
private int front;// 闃熷垪鐨勫墠绔笅鏍?
private int rear;// 闃熷垪鐨勫熬绔笅鏍?
private int size;// 闃熷垪鐨勫ぇ灏?
private int count;// 闃熷垪涓厓绱犵殑涓暟
public Queue(int size) {// 鏋勯犳柟娉曞垵濮嬪寲澶у皬涓簊ize鐨勯槦鍒?
this.queueArray = new long[size];
this.size = size;
this.front = 0;
this.rear = -1;
this.count = 0;
}
public void insert(long value) {// 鎻掑叆鎿嶄綔
if (rear == size - 1)// 闃熷垪宸叉弧
rear = -1;
queueArray[++rear] = value;
count++;
}
public long remove() {// 鍒犻櫎鎿嶄綔
long temp = queueArray[front++];
if (front == size)
front = 0;
count--;
return temp;
}
public long peakFront() {// 杩斿洖闃熷垪绗竴涓厓绱?
return queueArray[front];
}
public boolean isEmpty() {// 鍒ゆ柇鏄惁涓虹┖
return count == 0;
}
public boolean isFull() {// 鍒ゆ柇鏄惁宸叉弧
return count == size;
}
public int Count() {// 杩斿洖闃熷垪涓厓绱犵殑涓暟
return count;
}
public void print() {// 杈撳嚭闃熷垪鍏冪礌
for (int i = front; i < front + count; i++) {
System.out.print(queueArray[i] + "\t");
}
System.out.println();
}
}
class PriorityQueue {// 瀹炵幇浼樺厛闃熷垪鐨勭被
private int count;// 闃熷垪涓厓绱犵殑涓暟
private long priorityArray[];// 闃熷垪鏁扮粍
private int size;// 闃熷垪鐨勫ぇ灏?
public PriorityQueue(int size) {// 鏋勯犳柟娉曞垵濮嬪寲澶у皬涓簊ize鐨勯槦鍒?
this.size = size;
this.priorityArray = new long[size];
this.count = 0;
}
public void insert(long value) {// 鎻掑叆鎿嶄綔
int i;
if (count == 0)
priorityArray[count++] = value;
else {
for (i = count - 1; i >= 0; i--) {// 寰幆鎵惧埌姣旀彃鍏ュ煎ぇ鐨勪綅缃?
if (value < priorityArray[i]) {
priorityArray[i + 1] = priorityArray[i];// 渚濇绉诲姩浣嶇疆
} else
break;
}
priorityArray[i + 1] = value;// 鎻掑叆鍊兼斁鍒版寚瀹氫綅缃?
count++;
}
}
public long remove() {// 鍒犻櫎鎿嶄綔
return priorityArray[--count];
}
public boolean isEmpty() {// 鍒ゆ柇鏄惁涓虹┖
return count == 0;
}
public boolean isFull() { // 鍒ゆ柇鏄惁宸叉弧
return count == size;
}
public void print() { // 杈撳嚭闃熷垪鍏冪礌
for (int i = 0; i < count; i++)
System.out.print(priorityArray[i] + "\t");
System.out.println();
}
}
public class TextStackAndQueue {// 鎿嶄綔椤哄簭鏍堜笌闃熷垪鐨勭被
public static void main(String[] args) {// java绋嬪簭涓诲叆鍙e
System.out.println("1.鏁扮粍瀹炵幇椤哄簭鏍?);
Stack stack = new Stack(6);// 瀹炰緥鍖栭『搴忔爤锛屾爤鐨勫ぇ灏忎负6
while (!stack.isFull()) {// 鍙鏍堜笉婊′究寰幆
long r = (long) (Math.random() * 20);
stack.push(r); // 鍏ユ爤
System.out.print(r + "\t");
}
System.out.println();
while (!stack.isEmpty()) {// 鍙鏍堜笉绌轰究寰幆
long value = stack.pop();// 鑾峰緱鏍堥《鍏冪礌
System.out.print(value + "\t");
}
System.out.println();
System.out.println("-------------------------------------");
System.out.println("2.鏁扮粍瀹炵幇椤哄簭闃熷垪");
Queue queue = new Queue(6);// 瀹炰緥鍖栭『搴忛槦鍒楋紝闃熷垪鐨勫ぇ灏忎负6
while (!queue.isFull()) {// 鍙闃熷垪涓嶆弧渚垮惊鐜?
long value = (long) (Math.random() * 20);
queue.insert(value); // 鍏冪礌鎻掑叆闃熷垪
}
queue.print();// 杈撳嚭闃熷垪鍏冪礌
while (!queue.isEmpty()) {// 鍙鏍堜笉绌轰究寰幆
queue.remove();// 鍏冪礌绉婚櫎
queue.print();// 杈撳嚭闃熷垪鍏冪礌
}
queue.print(); // 杈撳嚭闃熷垪鍏冪礌
System.out.println(queue.isEmpty());// 闃熷垪鏄惁涓虹┖?
System.out.println("-------------------------------------");
System.out.println("3.鏁扮粍瀹炵幇浼樺厛闃熷垪");
PriorityQueue priority = new PriorityQueue(6); // 瀹炰緥鍖栭『搴忛槦鍒楋紝闃熷垪鐨勫ぇ灏忎负6
while (!priority.isFull()) {// 鍙闃熷垪涓嶆弧渚垮惊鐜?
long value = (long) (Math.random() * 20);
priority.insert(value);// 鍏冪礌鎻掑叆闃熷垪
}
priority.print(); // 杈撳嚭闃熷垪鍏冪礌
}
}
上一篇:
TextSort.java
下一篇:
法律专业开题报告范文