- 浏览: 2551658 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Interview(2)Stack and Queue
Stack
Last-in-first-out LIFO
Push put one item into the stack,
Pop get one item out of the stack
Some popular example will be the browser back and editor undo.
Some Popular operations
push(x) pop()
getSize() isEmpty()
top()
In JAVA, we have a class java.util.Stack
API - application programming interface - Interface
How to Implement Stack
#1 array S with N items, variable top
ExcpetionStackFull if data is over N items for the array.
Some core methods are as follow:
public Object pop() throws ExceptionStackEmpty {
Object elem;
if(isEmpty()){
throw new ExceptionStackEmpty(“Error while pop method: Stack empty.");
}
elem = S[top];
S[top - -] = null;
return elem;
}
It is O(1) for all methods.
Java Method Stack
Java is call-by-value.
Fac(n) = n!
public static long factorial(long n) {
if(n<=1) return 1;
else return n*factorial(n-1);
}
Operand Stack
We can use stack to see if all (), {}, [], if they are all matched.
ParentMatch(X, n)
Inputs: array X with n items
Outputs: if () match in X
{
initial stack S;
for ( i = 0 to n -1){
if(X[i] is ‘(‘){
S.push(X[i]);
}
else if(X[i] is ‘)’){
if(S.isEmpty()){
return not_match;
}
if( S.pop() is not X[i]){
return not_match;
}
}
}
if(S.isEmpty()){ return match; }
else { return not_match; }
}
HTML tag match
Queue
First-In-First-Out, FIFO
Basic method:
enqueue(x) : put item x into the queue
dequeue() : fetch the item from the queue
Other method:
getSize(), isEmpty(), front()
System can use array Q,
f - starter index of the Array
r - end index + 1 of the Array
Some import method implementation
public boolean isEmpty(){
return (f == r);
}
public void enqueue(Object obj) throws ExceptionQueueFull{
if(getSize() == capacity - 1){
throw new ExceptionQueueFull(“Queue overflow");
}
Q[r] = obj;
r = (r+1) % capacity;
}
public Object dequeue(){
Object elem;
if(isEmpty()){
throw new ExceptionQueueEmpty(“Error dequeue: array empty");
}
elem = Q[f];
Q[f] = null;
f = (f+1) % capacity;
return elem;
}
O(1)
Queue Application Samples
CPU, event and etc
Linked List
Singly Linked List
head - tail
element — content, next
This can save space.
Implement Stack on top of Singly Linked List
public class StackList implements Stack {
protected Node top; //top elem
protected int size; //
public StackList(){
top = null;
size = 0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return (top == null) ? true: false:
}
public void push(Object elem){
Node v = new Node(elem, top); //create new node, next is the original top
top = v; //update top
size++; //update size
}
public Object pop() throws ExceptionStackEmpty{
if(isEmpty()){
throw new Exception
}
Object temp = top.getElem();
top = top.getNext(); // update the top
size—; //update size
return temp;
}
}
Implement Queue on top of Singly Linked List
public class QueueList implements Queue {
protected Node head; //
protected Node tail;
protected int size;
public QueueList(){
head = tail = null;
size = 0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return (0 == size) ? true: false;
}
public void enqueue(Object obj){
Node node = new Node();
node.setElem(obj);
node.setNext(null);
if( 0==size){
head = node;
}else{
tail.setNext(node); // current node will be the tail
}
tail = node; //update the tail
size++; // update the size
}
public Object dequeue() throws ExceptionQueueEmpty{
if ( 0 == size){
throw new ExceptionQueueEmpty(“dequeue Error: array is empty");
}
Object obj = head.getElem();
head = head.getNext();
size—;
if(0 == size){
tail = null;
}
return obj;
}
}
Position
interface for position:
public interface Position{
public Object getElem();
public Object settled(Object e);
}
Double-ended Queue
insertFirst(x) - insertLast(x)
removeFirst() - removeLast()
first() - last()
getSize() - isEmpty()
Double Linked List
header - tailer
prev - next, element
public class DoubleEndQueue implement Deque{
protected DLNode header;
protected DLNode trailer;
protected int size;
public DoubleEndQueue(){
header = new DLNode();
trailer = new DLNode();
header.setNext(trailer);
trailer.setPrev(header);
size = 0;
}
public boolean isEmpty(){
return (0 == size) ? true: false;
}
public void insertFirst(Object obj){
DLNode second = header.getNext();
DLNode first = new DLNode(obj, header, second);
second.setPrev(first);
header.setNext(first);
size++
}
public void insertLast(Object obj) {
DLNode second = trailer.getPrev();
DLNode first = new DLNode(obj, second, trailer);
second.setNext(first);
trailer.setPrev(first);
size++;
}
public Object removeFirst() throws ExceptionQueueEmpty {
if(isEmpty()){
throw new ExceptionQueueEmpty(“Error: double end queue is empty");
}
DLNode first = header.getNext();
DLNode second = first.getNext();
Object obj = first.getElem();
header.setNext(second);
second.setPrev(header);
size - - ;
return obj;
}
public Objet removeLast() throws ExceptionQueueEmpty {
if(isEmpty()){
throw new ExceptionQueueEmpty(“Error: double end queue is empty");
}
DLNode first = trailer.getPrev();
DLNode second = first.getPrev();
Object obj = first.getElem();
tailer.setPrev(second);
second.setNext(trailer);
size - - ;
return obj;
}
}
References:
https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
Stack
Last-in-first-out LIFO
Push put one item into the stack,
Pop get one item out of the stack
Some popular example will be the browser back and editor undo.
Some Popular operations
push(x) pop()
getSize() isEmpty()
top()
In JAVA, we have a class java.util.Stack
API - application programming interface - Interface
How to Implement Stack
#1 array S with N items, variable top
ExcpetionStackFull if data is over N items for the array.
Some core methods are as follow:
public Object pop() throws ExceptionStackEmpty {
Object elem;
if(isEmpty()){
throw new ExceptionStackEmpty(“Error while pop method: Stack empty.");
}
elem = S[top];
S[top - -] = null;
return elem;
}
It is O(1) for all methods.
Java Method Stack
Java is call-by-value.
Fac(n) = n!
public static long factorial(long n) {
if(n<=1) return 1;
else return n*factorial(n-1);
}
Operand Stack
We can use stack to see if all (), {}, [], if they are all matched.
ParentMatch(X, n)
Inputs: array X with n items
Outputs: if () match in X
{
initial stack S;
for ( i = 0 to n -1){
if(X[i] is ‘(‘){
S.push(X[i]);
}
else if(X[i] is ‘)’){
if(S.isEmpty()){
return not_match;
}
if( S.pop() is not X[i]){
return not_match;
}
}
}
if(S.isEmpty()){ return match; }
else { return not_match; }
}
HTML tag match
Queue
First-In-First-Out, FIFO
Basic method:
enqueue(x) : put item x into the queue
dequeue() : fetch the item from the queue
Other method:
getSize(), isEmpty(), front()
System can use array Q,
f - starter index of the Array
r - end index + 1 of the Array
Some import method implementation
public boolean isEmpty(){
return (f == r);
}
public void enqueue(Object obj) throws ExceptionQueueFull{
if(getSize() == capacity - 1){
throw new ExceptionQueueFull(“Queue overflow");
}
Q[r] = obj;
r = (r+1) % capacity;
}
public Object dequeue(){
Object elem;
if(isEmpty()){
throw new ExceptionQueueEmpty(“Error dequeue: array empty");
}
elem = Q[f];
Q[f] = null;
f = (f+1) % capacity;
return elem;
}
O(1)
Queue Application Samples
CPU, event and etc
Linked List
Singly Linked List
head - tail
element — content, next
This can save space.
Implement Stack on top of Singly Linked List
public class StackList implements Stack {
protected Node top; //top elem
protected int size; //
public StackList(){
top = null;
size = 0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return (top == null) ? true: false:
}
public void push(Object elem){
Node v = new Node(elem, top); //create new node, next is the original top
top = v; //update top
size++; //update size
}
public Object pop() throws ExceptionStackEmpty{
if(isEmpty()){
throw new Exception
}
Object temp = top.getElem();
top = top.getNext(); // update the top
size—; //update size
return temp;
}
}
Implement Queue on top of Singly Linked List
public class QueueList implements Queue {
protected Node head; //
protected Node tail;
protected int size;
public QueueList(){
head = tail = null;
size = 0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return (0 == size) ? true: false;
}
public void enqueue(Object obj){
Node node = new Node();
node.setElem(obj);
node.setNext(null);
if( 0==size){
head = node;
}else{
tail.setNext(node); // current node will be the tail
}
tail = node; //update the tail
size++; // update the size
}
public Object dequeue() throws ExceptionQueueEmpty{
if ( 0 == size){
throw new ExceptionQueueEmpty(“dequeue Error: array is empty");
}
Object obj = head.getElem();
head = head.getNext();
size—;
if(0 == size){
tail = null;
}
return obj;
}
}
Position
interface for position:
public interface Position{
public Object getElem();
public Object settled(Object e);
}
Double-ended Queue
insertFirst(x) - insertLast(x)
removeFirst() - removeLast()
first() - last()
getSize() - isEmpty()
Double Linked List
header - tailer
prev - next, element
public class DoubleEndQueue implement Deque{
protected DLNode header;
protected DLNode trailer;
protected int size;
public DoubleEndQueue(){
header = new DLNode();
trailer = new DLNode();
header.setNext(trailer);
trailer.setPrev(header);
size = 0;
}
public boolean isEmpty(){
return (0 == size) ? true: false;
}
public void insertFirst(Object obj){
DLNode second = header.getNext();
DLNode first = new DLNode(obj, header, second);
second.setPrev(first);
header.setNext(first);
size++
}
public void insertLast(Object obj) {
DLNode second = trailer.getPrev();
DLNode first = new DLNode(obj, second, trailer);
second.setNext(first);
trailer.setPrev(first);
size++;
}
public Object removeFirst() throws ExceptionQueueEmpty {
if(isEmpty()){
throw new ExceptionQueueEmpty(“Error: double end queue is empty");
}
DLNode first = header.getNext();
DLNode second = first.getNext();
Object obj = first.getElem();
header.setNext(second);
second.setPrev(header);
size - - ;
return obj;
}
public Objet removeLast() throws ExceptionQueueEmpty {
if(isEmpty()){
throw new ExceptionQueueEmpty(“Error: double end queue is empty");
}
DLNode first = trailer.getPrev();
DLNode second = first.getPrev();
Object obj = first.getElem();
tailer.setPrev(second);
second.setNext(trailer);
size - - ;
return obj;
}
}
References:
https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
发表评论
-
Update Site will come soon
2021-06-02 04:10 1677I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 430Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 435Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 455VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 356Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 405PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 720Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 295Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 294Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 241MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 294MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 325Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 312Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 332Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 283Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 326K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 362Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 444Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 373Redis Cluster 2019(3)Redis Clus ...
相关推荐
We will be looking into a linked list, stack, queue, trees, heap, hash table and graphs. We will be looking into sorting, searching techniques. Then we will be looking into algorithm analysis, we ...
We will be looking into a Linked List, Stack, Queue, Trees, Heap, Hash Table and Graphs. We will be looking into Sorting & Searching techniques. Then we will be looking into algorithm analysis, we ...
We will be looking into a linked list, stack, queue, trees, heap, hash table and graphs. We will be looking into sorting, searching techniques. Then we will be looking into algorithm analysis, we ...
We will be looking into a linked list, stack, queue, trees, heap, hash table and graphs. We will be looking into sorting, searching techniques. Then we will be looking into algorithm analysis, we ...
We will be looking into a Linked-List, Stack, Queue, Trees, Heap, Hash-Table and Graphs. We will also be looking into Sorting, Searching techniques. In last few chapters, we will be looking into ...
1. **容器**:熟悉vector、list、deque、stack、queue、set、map等容器的特性和使用场景。 2. **迭代器**:理解迭代器的概念,能够使用迭代器遍历容器中的元素。 3. **算法**:掌握STL提供的常用算法,如sort、find...
玩转算法面试-- Leetcode真题分门别类 资源列表: 00-The-Opening ...06-Stack-and-Queue 07-Binary-Tree-and-Recursion 08-Recurion-and-Backstracking 09-Dynamic-Programming 10-Greedy-Algorithms
#### BFS and DFS(广度优先搜索与深度优先搜索) - **广度优先搜索**:从根节点开始逐层向下遍历,直到找到目标节点。 - **深度优先搜索**:尽可能深地搜索树的分支。当路径走到尽头时,就回溯到上一个节点,然后...
7. **集合与数据结构**:理解ArrayList、LinkedList、Dictionary、HashSet、Queue、Stack等常用集合的特性和使用场景。 8. **泛型**:知道如何使用泛型定义类、接口和方法,以及泛型的约束条件。 9. **委托与事件*...
Stack Queue Graph traversals(BFS, DFS) 最短路径数学题 从之前的采访中吸取的错误和教训: 调度: 更加注意时间安排,在公司现场面试前保持2-3天的开放时间。 除非非常关键,否则不要立即安排任何事情。 可以联系 ...
leetcode二维数组搜索DSA 准备资料库 FAANG 和其他人的数据结构和问题...Queue q = new LinkedList(); 哈希映射 - // Single Element HashMap Map.Entry majorityEle = null; // Interator for Hashmap Iterator ite
用Array实现Stack和Queue 用简单的Hashing function实现一个hashmap 用Adjacency Matrix和Adjacency List来实现Graph,然后在此基础上进行BFS和DFS Binary Search的迭代和递归 Merge Sort,Quick Sort,Counting ...
6. **STL深入**:熟悉容器(如map、unordered_map、deque等)和算法(如排序、查找)的内部工作原理,以及适配器(如stack和queue)的使用。 7. **C++11及以后的新特性**:包括Lambda表达式、右值引用、auto关键字...
queue_stack包为队列和栈专题记录。 linked_list包为链表专题记录。 hash_table包为哈希表专题记录。 data_structure_binary_tree包为二叉树专题记录。 introduction_to_data_structure_binary_search_tree包为二叉...
包含:list(列表),llist(链表),queue(队列),stack(堆),tree(树) 2)interview ———— 常见面试题目,练手 1ECMAScript5新增Array方法forEach的实现 2求最大公约数和最小公倍数 3声明提升 4判断字符...
2. 栈和队列(Stack & Queue):栈是后进先出(LIFO)的数据结构,只能在一端进行插入和删除操作;队列是先进先出(FIFO)的数据结构,允许在一端插入,在另一端删除。 3. 树(Tree):包括二叉树的遍历和重建...
- **栈(Stack)**:Python的列表可以用来实现后进先出(LIFO)的栈操作,如`append()`(压栈)和`pop()`(弹栈)。 - **队列(Queue)**:可以使用`collections.deque`实现先进先出(FIFO)的队列。 - **堆...
Stack是元素的集合,有两个主要操作: push ,添加到集合中, pop删除最近添加的元素 后进先出数据结构(LIFO) :最近添加的对象最先被删除 时间复杂度: 访问: O(n) 搜索: O(n) 插入: O(1) 删除: O(1) 队列 ...
- 队列(Queue)遵循先进先出(FIFO)原则,数据的添加在一端(入队),删除在另一端(出队)。 - 栈(Stack)遵循后进先出(LIFO)原则,数据的添加和删除在同一端(顶)进行。 11. **递归函数的输出** ```c #...