java - 在没有任何导入的队列中搜索对象
问题描述
我有一个 Java 程序,它使用扫描仪输入在我的循环队列中插入值,但我必须创建一个搜索方法而不使用任何导入。
例如:
public Projects(String sname, String name, int timeLeft)
Projects p = new Projects("ST1", "Title1", 16);
q.insert(p);
Projects r = new Projects("ST2", "Title1", 16);
q.insert(r);
在我的循环队列中插入元素后,我必须使用允许用户输入搜索条件来搜索字符串sname
并显示与sname
变量连接的位置的搜索方法。
有什么办法可以做到这一点?
解决方案
绝对有可能找到并知道特定信息在队列中的位置,您只需要编写搜索功能即可。
在此示例中,名称列表将等于您的队列类
public void search(list head, string sname)
{
boolean found = false;
int pos = 0;
list current = head;
while(!found)
{
//iterate through the list looking for the sname
//for each node you do not find it
//if found change found to true.
pos++;
if(!found)
current = current.next; //iterate to next node
if(current.sname == head.sname) //if it made it all the way around without finding the node
break; //this prevents an infinite loop.
}
if(found) //found = true
//put output of what you need here.
if(!found) //found = false
//output not found message.
因为这很可能是一项家庭作业,所以我没有给你完整的答案,你必须在其余部分编程才能完成程序。
推荐阅读
- angularjs - 如何销毁 angularjs 指令?
- python - Python中的可选链接
- vbscript - VBScript 添加验证对象运行时错误
- vb6 - VB6 是否能够将对象“转换”为“打印机”类型?
- javascript - 无法弄清楚为什么 opencv.js cv.fitEllipse() 会触发未捕获的异常与 1 个图像而不是另一个
- postgresql - 如何使用 typeorm 在 postgres 中保存 json 对象数组
- bash - 在 bash 脚本中使用命令行参数替换为脚本中的值
- r - 如何计算 DRPS(离散等级概率分数)
- database - Laravel:查询生成器 - 使用子句 WHERE NOT IN + AND WHERE 另一个表语句抛出错误
- apache - 为域和子域、Apache2、Ubuntu 声明 VirtualHost