java - Front 和 Rear 元素只显示当前添加的元素,而不是第一个添加的元素和最后一个添加的元素
问题描述
我在大学学习 DSA 概念时遇到了一些困难,我正在尝试自己学习。作为我尝试的一部分,我正在创建一个简单的应用程序,系统应在其中接受用户的预订并将它们插入队列。
我为此使用了 JAVA,但问题是输出始终只显示在该代码运行时添加的元素,而不是之前添加的元素,
例子:
我添加了名为 John 和 Steve 的 2 个元素,然后又进行了一轮并添加了一个名为 Peter 的人,根据 FIFO 概念,前面的索引值应该是 Peter,而后面的值应该是 John,因为首先添加了 John。
但是我的代码仅将 Peter 显示为第一和后索引值。
代码块如下。
节点类代码
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;
}
}
队列类代码
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;
}
}
主类代码
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();
}
}
解决方案
推荐阅读
- javascript - 如何仅删除一个特定的数据集标签chartJS?
- javascript - jquery获取在html中选择的整个选项
- c# - 列名未填充在 asp.net.cs 页面上
- php - show(Model $model) 在 Laravel 7 中不起作用
- c# - 从 Docker 运行的自动扩展 Kafka Consumer
- javascript - 我如何描述这两种价值传递方式之间的区别?
- java - 仅使用 Springboot 和 MongoDB 比较 YYYY-MM 部分日期
- pine-script - Strategy.long 后第二次交叉,Pine 脚本
- c# - 在长时间运行的工作人员服务中创建 HttpClient 对象的建议方法是什么
- bots - 对不和谐机器人的编码感到困惑