栈,队列和背包总结
来源:互联网 发布:中国高速铁路 知乎 编辑:程序博客网 时间:2024/05/21 10:07
1. 栈的实现
(1)栈数据结构
public class Stack<Item> implements Iterable<Item> { private Node<Item> first; // top of stack private int n; // size of the stack // helper linked list class private static class Node<Item> { private Item item; private Node<Item> next; }......}(2)压栈操作
public void push(Item item) { Node<Item> oldfirst = first; first = new Node<Item>(); first.item = item; first.next = oldfirst; n++;}(3)出栈操作
public Item pop() { if (isEmpty()) throw new NoSuchElementException("Stack underflow"); Item item = first.item; // save item to return first = first.next; // delete first node n--; return item; // return the saved item}
(4)栈迭代器
// return an iterator to this stack that iterates through the items in LIFO order.public Iterator<Item> iterator() { return new ListIterator<Item>(first);}// an iterator, doesn't implement remove() since it's optionalprivate class ListIterator<Item> implements Iterator<Item> { private Node<Item> current; public ListIterator(Node<Item> first) { current = first; } public boolean hasNext() { return current != null; } public void remove() { throw new UnsupportedOperationException(); } public Item next() { if (!hasNext()) throw new NoSuchElementException(); Item item = current.item; current = current.next; return item; }}
2. 队列的实现
(1)队列数据结构
public class Queue<Item> implements Iterable<Item> { private Node<Item> first; // beginning of queue private Node<Item> last; // end of queue private int n; // number of elements on queue // helper linked list class private static class Node<Item> { private Item item; private Node<Item> next; }......}(2)入队操作
public void enqueue(Item item) { Node<Item> oldlast = last; last = new Node<Item>(); last.item = item; last.next = null; if (isEmpty()) first = last; else oldlast.next = last; n++;}(3)出队操作
public Item dequeue() { if (isEmpty()) throw new NoSuchElementException("Queue underflow"); Item item = first.item; first = first.next; n--; if (isEmpty()) last = null; // to avoid loitering return item;}(4)队列迭代器
public Iterator<Item> iterator() { return new ListIterator<Item>(first);}// an iterator, doesn't implement remove() since it's optionalprivate class ListIterator<Item> implements Iterator<Item> { private Node<Item> current; public ListIterator(Node<Item> first) { current = first; } public boolean hasNext() { return current != null; } public void remove() { throw new UnsupportedOperationException(); } public Item next() { if (!hasNext()) throw new NoSuchElementException(); Item item = current.item; current = current.next; return item; }}
3. 背包的实现
(1)背包的数据结构
public class Bag<Item> implements Iterable<Item> { private Node<Item> first; // beginning of bag private int n; // number of elements in bag // helper linked list class private static class Node<Item> { private Item item; private Node<Item> next; }......}(2)入包操作
public void add(Item item) { Node<Item> oldfirst = first; first = new Node<Item>(); first.item = item; first.next = oldfirst; n++;}(3)背包迭代器
public Iterator<Item> iterator() { return new ListIterator<Item>(first);}// an iterator, doesn't implement remove() since it's optionalprivate class ListIterator<Item> implements Iterator<Item> { private Node<Item> current; public ListIterator(Node<Item> first) { current = first; } public boolean hasNext() { return current != null; } public void remove() { throw new UnsupportedOperationException(); } public Item next() { if (!hasNext()) throw new NoSuchElementException(); Item item = current.item; current = current.next; return item; }}
说明:用链表数据结构实现Bag API只需要将Stack中的push()改名为add(),并去掉pop()的实现即可。
参考文献:
[1] 算法(第4版)
阅读全文
0 0
- 栈,队列和背包总结
- 背包、队列和栈
- 背包、队列和栈
- 1.2:背包,队列和栈
- 1.3背包,队列和栈
- 数据结构与算法总结3_常用的数据结构(背包,栈和队列)
- 1.3背包,队列和栈 (Part1 -- Exercises)
- 第一章 1.3背包、队列和栈
- YTU.2244: 背包问题(栈和队列)
- 栈和队列总结
- 栈和队列总结
- 背包、队列、栈
- 栈、队列与背包
- 栈和队列算法总结
- 栈和队列的总结
- 栈和队列题目总结
- 栈和队列的总结
- 总结之栈和队列
- Struts2-01
- POJ
- 说说程序员的核心能力:第一个三年:后台、前端、移动应用,第二个三年呢?
- 10.14TG T3 tree
- Dubbo之Dubbo-admin管理平台搭建及常见问题-yellowcong
- 栈,队列和背包总结
- linux 信号笔记
- python爬虫第二节课
- 欢迎使用CSDN-markdown编辑器
- opencl笔记
- LeetCode Blog for course "Algorithms" -- Problem 7
- Druid是什么和用StatViewServlet用于展示Druid的统计信息
- Jzoj4714 公约数
- Hdu4859-【】-【拓扑排序+优先队列+逆向处理】