你好我是java的新手我想尝试做的是,我在我的主类中创建了一个名为LinkedStackDemo的堆栈,对象就像这个LinkedStack s = new LinkedStack();
稍后在我的LinkedStack类中我创建了另一个堆栈得到了一个我在主班上创建的元素中有一定数量的元素。我的问题是,当我将第二个堆栈传递给我的LinkedStack类中的方法时,我希望能够保持从第一个堆栈(我的主堆栈)中弹出元素,但是当我尝试弹出它时,只会弹出我正在传递的那个是第二个
这是程序Main:
public class LinkedStackDemo
{
public static void main (String a[])
{
LinkedStack s = new LinkedStack();
LinkedQueue q = new LinkedQueue();
s.push(100,1.35);
s.push(50,1.40);
s.push(40,1.45);
System.out.println("Size of the stack: " + s.size());
System.out.println();
System.out.println("STACK contains following items till this moment:");
s.print();
System.out.println();
s.reciept(200);
System.out.println();
s.push(250, 1.60);
System.out.println();
System.out.println("STACK contains following items till this moment:");
s.print();
}
}
链接堆栈类
class LinkedStack
{
private Node head; //the first node
private int size; // number of items
private LinkedQueue q = new LinkedQueue();
private int check=0;
//nested class to define node
private static class Node
{
double item;
double price;
Node next;
}
//Zero argument constructor
public LinkedStack()
{
head = null;
size = 0;
}
public boolean isEmpty()
{
return (size == 0);
}
//Remove item from the beginning of the list.
public Node pop()
{
if(isEmpty()){
System.out.println("Stack is empty");
}
Node current= head;
head = head.next;
size--;
check-=current.item;
return current;
}
//Add item to the beginning of the list.
public void push(double item, double price)
{
Node oldHead = head;
head = new Node();
head.item = item;
head.price= price;
head.next = oldHead;
size++;
check+=item;
if(!q.isEmpty()){
q.CalMissOrders(check,q);
}
}
public int size()
{
return size;
}
public void print(){
Node current= head;
while(current!=null){
System.out.println("Widgets "+current.item+" Price $"+current.price);
current=current.next;
}
}
public void printReciept(LinkedStack s, double order){
Node current;
double totalPrice=0;
double bookPrice=0;
System.out.println("Reciept:");
while(!s.isEmpty()){
current=s.pop();
totalPrice=current.price;
bookPrice+=current.item*current.price;
System.out.println("Widgets "+current.item+" price of each $"+current.price+" the total is $"+current.item*current.price);
}
System.out.println("Ordered Widgets "+order+" price of each $"+ (totalPrice+(totalPrice*.4))+" Total plus 40% = $"+Math.round((totalPrice+(totalPrice*.4))*order) );
System.out.println("Total cost = $"+bookPrice+" for the bookKeeper’s records.");
}
public void reciept(double order){
LinkedStack s = new LinkedStack();
double count =0;
Node mrecently;
while(!isEmpty()&&count<order){
mrecently=pop();
count+=mrecently.item; //adding widgets
if(count>order){
double extraWidgets;
extraWidgets=count-order;
push(extraWidgets,mrecently.price);
s.push(mrecently.item-extraWidgets, mrecently.price);
break;
}
s.push(mrecently.item, mrecently.price);
}
if(count<order){
double miss=order-count;
q.enQ(s,order,miss);
System.out.println("We are out of widgets your order will be placed in a queue until next shipment");
}
else{
printReciept(s,order);
}
}
public void MissOrders(double missing,LinkedStack s,double order){
double count =0;
Node mrecently;
System.out.println("test1");
s.print();
while(!isEmpty()&&count<missing){
mrecently=pop();
count+=mrecently.item; //adding widgets
if(count>missing){
double extraWidgets;
extraWidgets=count-missing;
push(extraWidgets,mrecently.price);
s.push(mrecently.item-extraWidgets, mrecently.price);
break;
}
s.push(mrecently.item, mrecently.price);
}
System.out.println("test2");
s.print();
s.printReciept(s,order);
}
}
LinkedQueue的代码:
import java.util.*;
public class LinkedQueue{
private int total;
private Node front, rear;
private static class Node {
static LinkedStack ele;
double miss;
double order;
Node next;
}
public LinkedQueue() {
front = null;
rear = null;
total = 0;
}
public void enQ(LinkedStack s, double order, double missing)
{
Node current = rear;
rear = new Node();
rear.ele = s;
rear.order=order;
rear.miss=missing;
if (total++ == 0) front = rear;
else current.next = rear;
}
public Node deQ()
{
if (total == 0) throw new java.util.NoSuchElementException();
Node current = front;
front = front.next;
if (--total == 0) rear = null;
return current;
}
public Node peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return front;
}
public void print(){
Node current= front;
int pos=0;
while(current!=null){
LinkedStack s;
s=current.ele;
s.print();
System.out.println("Position in Queue "+(++pos)+" Order Widgets: "+current.order+" Missing Widgets "+current.miss);
current=current.next;
}
}
public void CalMissOrders(int check, LinkedQueue q){
Node current=q.front;
if(check>=current.miss){
current=q.deQ();
LinkedStack s= current.ele;
double miss=current.miss;
double order=current.order;
s.MissOrders(miss,s,order);
}
}
public boolean isEmpty() {
return (front == null);
}
public int getSize()
{
return total;
}
}
reciept方法可以很好地弹出主类中创建的堆栈中的元素,并将其推送到此处创建的堆栈中。如果我有足够的项目,它将打印一个reciept,如果不是新的堆栈将被保存并等待在第一个堆栈中推送更多的元素。 MissOrders方法给我带来了麻烦。当我尝试从主堆栈类中弹出元素时,它不会让我。它从通过reciept方法创建的那个中弹出。任何sugestions感谢您的时间。