简体   繁体   中英

Front and Rear elements are showing only the currently added elements but not the firstly added and last one added

I had some trouble learning DSA concepts in college and i am trying to learn it all by myself. And as part of my attempts, i am creating a simple application where the system should take in reservations by the user and insert them into a queue.

I have used JAVA for this, but the problem is the output always displays only the elements which was added on that code run but not the ones which were added previously,

Example:

I add 2 elements named as John and Steve and i go another round and add a person named Peter and according to the FIFO concept the front index value should be Peter and the rear value should be John since John was added first.

But my code displays only Peter as first and rear index value.

Code blocks are as below.

节点类代码

队列类变量和入队操作

出列操作

使用扫描仪获取用户输入并将元素排入队列

延续

代码仅显示在代码运行中添加的元素

Node Class Code

public class Nodes {
    
    public int BanquetHall_ID;
    public int Reservation_ID;
    public String Customer_Name;
    public int Contact_No;
    public String Reservation_Date;
    public int Menu_Selection;
    public String Customer_Email_Address;
    Nodes next ;
    int data;

    
    public Nodes(int BanquetHall_ID, int Reservation_ID, String Customer_Name, 
            int Contact_No,String Customer_Email_Address ,String Reservation_Date, int Menu_Selection)
    {
        this.BanquetHall_ID=BanquetHall_ID;
        this.Reservation_ID=Reservation_ID;
        this.Customer_Name=Customer_Name;
        this.Contact_No=Contact_No;
        this.Customer_Email_Address=Customer_Email_Address;
        this.Reservation_Date=Reservation_Date;
        this.Menu_Selection=Menu_Selection;
        this.next=null;
    }
}

Queue Class Code

public class Queue {

Nodes front, rear;

public Queue()
{
    this.front = this.rear = null;
}      
        
    void enQueue(int BanquetHall_ID, int Reservation_ID, String Customer_Name, int Contact_No, 
            String Customer_Email_Address,String Reservation_Date, int Menu_Selection)
    {
        Nodes temp = new Nodes(BanquetHall_ID , Reservation_ID , Customer_Name , Contact_No , 
                Customer_Email_Address , Reservation_Date , Menu_Selection);
            
            if(this.rear == null)
            {                   
                this.front = this.rear = temp;
                return;  
            }
            
            this.rear.next = temp;
            this.rear = temp;
    }

    public void deQueue(int Reservation_ID)
    {
        if (this.front == null)
        {
            return;
        }

        Nodes temp = this.front;
        this.front= this.front.next;
            
        if (this.front == null)
        {
            this.rear=null;
        }
    }

Main Class Code

    else if(c==2)
    {
          int crun = 1;
            while(crun!=0)
            {
            System.out.println("***** You Have Selected To Make A Reservation *****");
            System.out.println("----- Please Provide The Accurate Details Below -----");
            
            System.out.println("Enter preferred Hall ID : ");
            int id = sc.nextInt();
            
            System.out.println("Enter preferred Reservation ID (Provide An ID In Numbers Which You Can Always Remember) : ");
            int rid=sc.nextInt();
            
            sc.nextLine();
            
            System.out.println("Enter Your Name : ");
            String name = sc.nextLine();
            
            System.out.println("Enter Your Contact Number : ");
            int contact = sc.nextInt();
            
            sc.nextLine();
            
            System.out.println("Enter Your Email Address : ");
            String email = sc.nextLine();
            
            System.out.println("Enter Your Reservation Date (YYYY-MM-DD): ");
            String date = sc.nextLine();
            
            System.out.println("Enter Your Menu Selection : ");
            int menu = sc.nextInt();
            
            Queue nd = new Queue();
            nd.enQueue(id, rid, name, contact, email, date, menu);
            System.out.println("Inserted Element Index : "+ nd.front.Customer_Name);
            System.out.println("Last element Index : "+nd.rear.Customer_Name);
            
            Scanner cs = new Scanner(System.in);
            
            System.out.println("Would you like to rerun this program? 1= Yes, 0 = No");
            crun = cs.nextInt();
            }
    }

The Queue follows FIFO. Your Order is John<--Steve <-- Peter. So, your FRONT is John. REAR is Peter. Hence, your output is correct.

Happy Learning!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM