//大家谁能具体给我讲讲这段代码是怎么回事
//弄的我有点晕
//有些地方懂,有些地方又很迷糊
class LinkedList //定义双项链表
{
char data;
LinkedList back;
LinkedList forward;
}
interface Access //定义从队列和栈存取操作的接口
{
void put(char c);
char get();
}
class Queue implements Access //定义队列类
{
private LinkedList QHead=new LinkedList();
private LinkedList QRear=QHead; //初始化队头和队尾
public void put(char c) //实现向队列存数的方法
{
QRear.forward=new LinkedList();
QRear.forward.data=c;
QRear.forward.back=QRear;
QRear=QRear.forward;
}
public char get() //实现从队列取数的方法
{
if(QHead!=QRear) //如果队列不为空则取数
{
QHead.forward.back=null;
QHead=QHead.forward;
return QHead.data;
}
else
{
System.out.println("The queue is empty! ");
return '\0';
}
}
}
class Stack implements Access //定义栈类
{
private LinkedList bottom=new LinkedList();
private LinkedList top=bottom; //初始化栈顶与斩底
public void put(char c) //实现从栈中存数的方法
{
top.forward=new LinkedList();
top.forward.data=c;
top.forward.back=top;
top=top.forward;
}
public char get() //实现从栈中取数的方法
{
if(top!=bottom) //如果栈不为空则取数
{
char ch=top.data;
top.back.forward=null;
top=top.back;
return ch;
}
else
{
System.out.println("The stack is empty! ");
return '\0';
}
}
}
public class StackQueue
{
public static void main(String[] args)
{
char ch;
Queue q=new Queue(); //创建一个队列q
Stack s=new Stack(); //创建一个栈s
q.put('x');
q.put('y');
q.put('z'); //向队列q存入3个字符
s.put('x');
s.put('y');
s.put('z'); //向栈s存入3个字符
System.out.println("Queue: ");
if((ch=q.get())!='\0')
System.out.println(ch);
if((ch=q.get())!='\0')
System.out.println(ch);
if((ch=q.get())!='\0')
System.out.println(ch);
if((ch=q.get())!='\0')
System.out.println(ch);
//从队列q中取数并显示
System.out.println("Stack: ");
if((ch=s.get())!='\0')
System.out.println(ch);
if((ch=s.get())!='\0')
System.out.println(ch);
if((ch=s.get())!='\0')
System.out.println(ch);
if((ch=s.get())!='\0')
System.out.println(ch);
//从栈s中取数并显示
}
}