class MyQueue {
int a[ ] ;
int maxSize;
int front;
int rear;
int nItems;
public MyQueue ( int size) {
maxSize = size;
a = new int [ maxSize] ;
front = 0 ;
rear = - 1 ;
nItems = 0 ;
}
public void insert ( int value) {
if ( rear == maxSize - 1 ) {
rear = - 1 ;
}
a[ ++ rear] = value;
nItems ++ ;
}
public int remove ( ) {
int tmp = a[ front++ ] ;
if ( front == maxSize) {
front = 0 ;
}
nItems -- ;
return tmp;
}
public int peekFront ( ) {
return a[ front] ;
}
public boolean isFull ( ) {
return nItems == maxSize;
}
public boolean isEmpty ( ) {
return nItems == 0 ;
}
public int size ( ) {
return nItems;
}
public static void main ( String[ ] args) {
MyQueue myQueue = new MyQueue ( 10 ) ;
myQueue. insert ( 10 ) ;
myQueue. insert ( 20 ) ;
myQueue. insert ( 30 ) ;
myQueue. insert ( 40 ) ;
myQueue. insert ( 50 ) ;
System. out. println ( "删除一次:" + myQueue. remove ( ) ) ;
System. out. println ( "删除两次:" + myQueue. remove ( ) ) ;
System. out. println ( "删除三次:" + myQueue. remove ( ) ) ;
myQueue. insert ( 60 ) ;
myQueue. insert ( 70 ) ;
myQueue. insert ( 80 ) ;
myQueue. insert ( 90 ) ;
myQueue. insert ( 100 ) ;
myQueue. insert ( 600 ) ;
while ( ! myQueue. isEmpty ( ) ) {
int value = myQueue. remove ( ) ;
System. out. print ( value + " " ) ;
}
}
}
package com. zoujc. test2;
import java. util. Stack;
public class MList < T> {
private Stack< T> stack1 = new Stack < > ( ) ;
private Stack< T> stack2 = new Stack < > ( ) ;
public MList ( ) {
}
public void appendTail ( T t) {
stack1. add ( t) ;
}
public T deleteHead ( ) {
if ( stack2. isEmpty ( ) ) {
while ( ! stack1. isEmpty ( ) ) {
stack2. add ( stack1. pop ( ) ) ;
}
}
if ( stack2. isEmpty ( ) ) {
throw new RuntimeException ( "No more element." ) ;
}
return stack2. pop ( ) ;
}
}