当前位置:网站首页>CPT 102_ LEC 15
CPT 102_ LEC 15
2022-06-11 02:50:00 【NONE_ WHY】
1. A Stack using a Linked List with a header
1.1. Some Information of Stack
- LIFO -> Last In, First Out
- The Operation belows Only happen at the Stack Top
- Insertions (Push)
- Deletions (Pop)

1.2. Some Important Method in Implementation
- peek
public E peek(){ if (data==null) { throw new EmptyStackException(); } return data.value; }
- pop
public E pop(){ if (data==null) { throw new EmptyStackException(); } E ans = data.value; data = data.next; return ans; }
- push
public void push(E item){ if (item == null) { throw new IllegalArgumentException(); } data = new Node(item, data); }
- iterator
public Iterator <E> iterator(){ return new NodeIterator(data); } private class NodeIterator <E> implements Iterator <E>{ private Node<E> node; public NodeIterator (Node <E> node) { this.node = node; } public boolean hasNext () { return (node != null); } public E next () { if (node==null) { throw new NoSuchElementException(); } E ans = node.get(); node = node.next(); return ans; } public void remove(){ throw new UnsupportedOperationException(); } }
2. A Queue using a Linked List with a header
2.1. Some Information of Queue
- FIFO -> First In, First Out
- The Operations
- Insertions -> At the end (tail)
- Deletions -> In the front
2.2. Some Application
- user job queue
- print spooling queue
- I/O event queue
- incoming packet queue
- outgoing packet queue
2.3. Some Important Method in Implementation
- peek
public E peek(){ if (front==null) { return null; }else{ return front.value; } }
- poll
public E poll(){ if (front==null){ return null; } E ans = front.value; front = front.next; if (front==null) { back = null; } return ans; }
- offer
public boolean offer(E item){ if (item == null) { return false; } if (front == null){ back = new Node(item, null); front = back; }else { back.next = (new Node(item, null)); back= back.next; } return true; }
3. Linked Stack and Queue
- Uses a “header node
- contains link to head node, and maybe last node of linked list
- Important to choose the right end
- easy to add or remove from head of a linked list
- hard to add or remove from the last node of a linked list
- easy to add to last node of linked list if have pointer to tail
- Linked Stack and Queue
- all main operations are O(1)
- Can combine Stack and Queue
- addFirst, addLast, removeFirst
- also need removeLast to make a “Deque” (double-ended queue)
- ⇒ need doubly linked list (why?)
- See the java “LinkedList” class.
边栏推荐
- Unity HTC and Pico are the same
- How can Delma's own brand "take off" when Philips is listed on the market?
- Jetpack compose box control
- [C language classic]: inverted string
- Fundamentals of deep learning [4] build easyocr and carry out simple character recognition from 0
- OpenJudge NOI 1.13 18:Tomorrow never knows?
- Xampp is used under M1 chip, and the installation extension error
- Can Xiaoxiang life become the "Yonghui" in the discount industry after the completion of the round a financing of tens of millions of yuan?
- App test_ Summary of test points
- How to add cookie pop-up window in WordPress website (without plug-in)
猜你喜欢

Rs232/rs485 to 4G DTU uploading temperature and humidity sensor data based on Modbus protocol to remote TCP server

Istio安装与使用

CPT 102_LEC 16

码农的进阶之路 | 每日趣闻

JS memory leak

App test_ Summary of test points

ADVANCE.AI首席执行官寿栋将在2022新兴市场品牌出海线上峰会分享跨境电商运用AI技术合规

How to use phpMyAdmin to optimize MySQL database

Jetpack compose scaffold and topappbar (top navigation)

AOSP ~ 修改WebView默认实现
随机推荐
【长时间序列预测】Aotoformer 代码详解之[3]模型整体架构分析
Jetpack compose scaffold and bottomappbar (bottom navigation)
HUST软件工程(实验2)--TDD测试驱动开发实验。
Technology sharing | quick intercom, global intercom
CPT 102_LEC 17
Explanation of spark common parameters
GraphAcademy 课程讲解:《Neo4j 图数据科学基础》
企业展厅设计能为企业带来什么?
Problems with JDBC tool classes
【新晋开源项目】动态配置化任务编排框架 Gobrs-Async 加入Dromara开源社区
Win10 安装Office 2016出现错误代码30204-44怎么处理?
How to handle error code 30204-44 when installing office 2016 in win10?
When a logical deletion encounters a unique index, what are the problems and solutions?
Uni app - one click access to user information
年金保险理财产品可以复利吗?利率是多少?
If you understand the logic of mining and carbon neutrality, you will understand the 100 billion market of driverless mining areas
AOSP ~ WIFI默认开启 + GPS默认关闭 + 蓝牙默认关闭 + 旋转屏幕关闭
完成千万元A轮融资,小象生活能否成为折扣界的“永辉”?
Can Xiaoxiang life become the "Yonghui" in the discount industry after the completion of the round a financing of tens of millions of yuan?
App test_ Summary of test points