将数据推送到堆栈

时间:2012-08-04 05:15:22

标签: java stack

package michael.week.pkg5;

class Employee {
String Fname ;
        String Lname;
        String PhoneNum;
        String Address;
         void setFirst(String First){
             Fname = First ;
         }
         void setlast(String Last){
             Lname  = Last ;
         }
          void setAddress (String address){
             Address = address ;
         }
         void setPhone (String Phone){
             PhoneNum  = Phone ;
         }          
         void display (){
             System.out.println ("the Fist name is :"+ Fname + "  , the last name is :        " + Lname  + "  ,the address is : "+ Address+ "  ,the phone is : "+ PhoneNum);
         }
         }

    package michael.week.pkg5;

     import java.io.BufferedReader;
     import java.io.IOException;
      import java.io.InputStreamReader;





   public class MichaelWeek5 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    class Stck {
    Employee stck [] = new Employee[10];
    int x ;
     void stck (){
        x= -1 ;
    }
    Employee push (Employee item){
        if (x == 9){
            System.out.println ("Stack is full");
        }else stck[++x] = item ;
        return stck[x];
    }
        Employee pop (){
        if (x <0){
            System.out.println (" Stack underflow");
           return stck [x];
        }else 
            return stck[x++];
    }
    }
    InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    String info2 = null ;
    Stck obj = new Stck();
    Employee obj2 = new Employee ();
    int w = -1 ;
    for (int r=1 ;r>0; ){
   System.out.println("please enter add  to add new employee");
   System.out.println("please enter pop to pop the last added employee");
   System.out.println("please enter exit to exit");
   String choice = br.readLine();
   if(choice.equals("add")){
       System.out.println(w);
       if (w >= 9){
           System.out.println("you reached the maxmum number !");
           continue  ;
       } 
       else {
           w++;
           obj.stck ();
       String info  ;
       System.out.println ("please enter Employee first name :");
       info = br.readLine();

       System.out.println ("please enter Employee last name name :");
       info = br.readLine();

       System.out.println ("please enter Employee address :");
       info = br.readLine();

       System.out.println ("please enter Employee phone number :");
       info = br.readLine();
       }
   } else if(choice.equals("pop")){
           obj.pop();
           w--;
   }else if(choice.equals("exit"))
           break ;
       else {System.out.println (choice + " is wrong choice !") ;
   }
    }

    } 

   }

问候, 我是java新手,我正在研究这个程序.......我需要知道如何将数据推送到stck? 注意:push的参数是employee类型,Employee包含First,Last,Phone和address。我怎么能推动他们每个人? 这就是我所做的,我的导师拒绝了

package week.pkg5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Week5 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
      class Employee {


     String [] Fname = new String [10];
        String[] Lname= new String [10];
        String[] PhoneNum= new String [10];
        String[] Address = new String [10];
        int x= -1 ;
         void increment(){
           x++;

        }
         void PushFirst(String First){
             Fname[x] = First ;
         }
         void Pushlast(String Last){
             Lname [x] = Last ;
         }
         void PushPhone (String Phone){
             PhoneNum [x] = Phone ;
             System.out.println ("the Fist name is :"+ Fname [x]+ "  , the last name is : " + Lname [x] + "  ,the address is : "+ Address[x] + "  ,the phone is : "+ PhoneNum[x]);
         }
         void PushAddress (String address){
             Address [x] = address ;
         }
         void pop (){
             if (x < 0){
                 System.out.println (" No Empolyee !");
             }
             else {

        System.out.println ("the Fist name is :"+ Fname [x]+ "  , the last name is : " + Lname [x] + "  ,the address is : "+ Address[x] + "  ,the phone is : "+ PhoneNum[x]);
        x--;
        }
}
 void display (){
    if (x < 0){
        System.out.println (" No Empolyee !");
    }
    else {
        for (int q = 0 ; q <=x ; q++){
        System.out.println ((q+1)+"- "+"the First name is :"+ Fname [q]+ "  , the last name is : " + Lname [q] + "  ,the address is : "+ Address[q] + "  ,the phone is : "+ PhoneNum[q]);
        }
    }
}
 }
 InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    Employee obj = new Employee();
    int w = -1 ;
    for (int r=1 ;r>0; ){
   System.out.println("please enter add  to add new employee");
   System.out.println("please enter display  to display all employees list");
   System.out.println("please enter pop to pop the last added employee");
   System.out.println("please enter exit to exit");
   String choice = br.readLine();

   if(choice.equals("add")){


       System.out.println(w);
       if (w >= 9){
           System.out.println("you reached the maxmum number !");
           continue  ;
       } 
       else {
           w++;
           obj.increment();
       String info  ;
       System.out.println ("please enter Employee first name :");
       info = br.readLine();
       obj.PushFirst(info);
       System.out.println ("please enter Employee last name name :");
       info = br.readLine();
       obj.Pushlast(info);
       System.out.println ("please enter Employee address :");
       info = br.readLine();
       obj.PushAddress(info);
       System.out.println ("please enter Employee phone number :");
       info = br.readLine();
       obj.PushPhone(info);}
   }  else if(choice.equals("display")){
           obj.display();
   } else if(choice.equals("pop")){
           obj.pop();
           w--;
   }else if(choice.equals("exit"))
           break ;
       else {System.out.println (choice + " is wrong choice !") ;
   }
    }

} 
 }

3 个答案:

答案 0 :(得分:0)

您需要创建一堆员工,然后将整个员工对象推送到堆栈。

import java.util.Stack;
...
Employee emp = new Employee();
Stack<Employee> stack = new Stack<Employee>();
stack.push(emp);

答案 1 :(得分:0)

创建一个类的对象后说(这里是stck),它将通过调用下面显示的方法来保存数据

class std
{

  int id;
  string name;
  public:
  void set_id(int i)
  {
      id=i;
  }
  void set_name(string n)
  {
      name = n;
   }
  void disp()
  {
     system.out.println("name"+name+" and id = "+id);
  }
 };

  main()
  {
      std s = new std();
      s.set_id(1);
      s.set_name("sunmoon");
      s.disp();
 }

所以在这里你可以观察到s被认为是堆栈,我们推送一个人的细节,就像你可以创建一个对象数组并推送n个人的细节。

答案 2 :(得分:0)

您对建模数据的方式有几个问题。

问题#1(最大的问题): 员工不应该拥有堆栈的任何内部表示。想想现实世界中的物体。在您的示例中,每个Employee都应该具有:

  • 名字
  • 姓氏
  • 地址
  • 电话号码

所以成员字段:

 String [] Fname = new String [10];
 String[] Lname= new String [10];
 String[] PhoneNum= new String [10];
 String[] Address = new String [10];
 int x= -1 ;

不属于。这导致我们......

问题#2 :您对堆栈建模的方式只允许固定数量的条目 - 在您的情况下为10。这不是堆栈应该如何工作。如果你不确定,请阅读Stack是什么。您可以使用LinkedList在Java中建模堆栈 - 或者使用Java提供的内置Stack。如果您决定创建自己的版本,则需要创建的重要操作是:

  • 弹出
  • 的isEmpty

我的建议是首先对员工和实践进行建模,然后从JDK中推出并将其从内置堆栈中弹出,直到您在尝试创建自己的操作之前真正了解操作。