用Java实现双端队列,并完成以下操作:
1.addFirst(o):将对象o插入到队列的前面
2.addLast(o):将对象o插入到队列的后面
3.removeFirst():返回并删除队列前面的元素,如果deque为空,则抛出EmptyDequeException
4.removeLast():返回并删除队列后面的元素,如果deque为空,则抛出EmptyDequeException
用Java实现双端队列
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
6条回答 默认 最新
- Java大魔王 2022-11-28 17:22关注
class ListNode { //数据域 public int val; //指针 public ListNode next; //初始化值 public ListNode(int val) { this.val = val; } } public class Deque { public ListNode head;//头结点 public ListNode last;//尾节点 public void addFirst(int val) { //创建对象初始化值建立新节点 ListNode node = new ListNode(val); //判断尾节点是否为空 if (this.last == null) { //若为空就是头结点尾节点都是这个新创建的节点 this.head = node; this.last = node; } //node成为新的头节点 node.next = this.head; this.head = node; } public void addLast(int val) { //创建对象初始化值建立新节点 ListNode node = new ListNode(val); //判断尾节点是否为空 if (this.last == null) { //若为空就是头结点尾节点都是这个新创建的节点 this.head = node; this.last = node; } //node成为新的尾节点 this.last.next = node; this.last = node; } //从头结点那边拿出Deque的一个节点 public int removeFirst() throws Exception { //判断头节点是否为空,如果是就输出! if (this.head == null) { throw new Exception("EmptyDequeException"); } //如果不为空,把头结点指向的值拿出来 int oldValue = this.head.val; //判断头结点尾节点是否重合,如果重合就表明双端队列为空 if (this.head == this.last) { this.head = null; this.last = null; } else { //没有重合就接着找下一个节点变成新的头结点 this.head = this.head.next; } return oldValue; } //从尾结点那边拿出Deque的一个节点 public int removeLast() throws Exception { //判断尾节点是否为空,如果就输出! if (this.last == null) { throw new Exception("EmptyDequeException"); } // //如果不为空,把尾结点指向的值拿出来 int oldValue = this.last.val; //判断头结点尾节点是否重合,如果重合就表明双端队列为空 if (this.head == this.last) { this.last = null; this.head = null; } else { //遍历找到新的尾节点 ListNode cur = this.head; while (cur.next != last) { cur = cur.next; } //把找到的最后一个节点做为尾节点 this.last = cur; //尾节点.next=null this.last.next = null; } return oldValue; } //获取Deque处第一个节点的值 public int peekFirst() { //判断头结点是否为空,是就输出! if (this.head == null) { System.out.println("!"); return -1; } //返回头结点值 return this.head.val; } //获取Deque上最后一个节点的值 public int peekLast() { //判断尾结点是否为空,是就输出! if (this.last == null) { System.out.println("!"); return -1; } //返回尾结点值 return this.last.val; } //Check whether the Deque is empty public boolean empty() { return this.head == null; } public void display(){ ListNode cur =head; while (cur!=last) { System.out.print(cur.val); cur = cur.next; } System.out.print(cur.val); } public static void main(String[] args) { try { Deque deque=new Deque(); deque.addFirst(1); deque.addFirst(2); deque.addFirst(3); System.out.print("在队列前面插入了数据后的结果:"); deque.display(); System.out.println(); deque.addLast(4); deque.addLast(5); deque.addLast(6); System.out.print("在队列后面插入了数据后的结果:"); deque.display(); System.out.println(); deque.removeFirst(); System.out.print("在队列前面删除了数据后的结果:"); deque.display(); System.out.println(); deque.removeLast(); System.out.print("在队列后面删除了数据后的结果:"); deque.display(); System.out.println(); }catch(Exception e) { e.printStackTrace(); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
- ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
- ¥20 腾讯企业邮箱邮件可以恢复么
- ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
- ¥15 错误 LNK2001 无法解析的外部符号
- ¥50 安装pyaudiokits失败
- ¥15 计组这些题应该咋做呀
- ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
- ¥15 让node服务器有自动加载文件的功能
- ¥15 jmeter脚本回放有的是对的有的是错的