阅读874 返回首页    go 阿里云 go 技术社区[云栖]


数据结构之栈与队列

一。栈
1。概念:栈(stack)是一种线性数据结构,只能访问它的一端来存储或者读取数据。栈是一种后进先出的结构(LIFO)
2。栈的主要操作:
.clear()——清栈
.isEmpty()——检查栈是否为空
.push(e)——压栈
.pop()——出栈
.topEl()——返回栈顶元素

3。栈的java实现:使用数组链表实现

ExpandedBlockStart.gifContractedBlock.gif/** *//**
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

None.gif
package com.sohu.blog.denns_zane.stackqueue;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** *//**
InBlock.gif * 
@author dennis
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public abstract class AbstractStack dot.gif{
InBlock.gif    
public abstract Object pop();
InBlock.gif
InBlock.gif    
public abstract void push(Object obj);
InBlock.gif
InBlock.gif    
public abstract Object topEl();
InBlock.gif
InBlock.gif    
public abstract boolean isEmpty();
InBlock.gif
InBlock.gif    
public abstract void clear();
ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** *//**
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

None.gif
package com.sohu.blog.denns_zane.stackqueue;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** *//**
InBlock.gif * 
@author dennis
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public class Stack extends AbstractStack dot.gif{
InBlock.gif    
private java.util.ArrayList pool = new java.util.ArrayList();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Stack() dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Stack(int n) dot.gif{
InBlock.gif        pool.ensureCapacity(n);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void clear() dot.gif{
InBlock.gif        pool.clear();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public boolean isEmpty() dot.gif{
InBlock.gif        
return pool.isEmpty();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Object topEl() dot.gif{
InBlock.gif        
if (isEmpty())
InBlock.gif            
throw new java.util.EmptyStackException();
InBlock.gif        
return pool.get(pool.size() - 1);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Object pop() dot.gif{
InBlock.gif        
if (isEmpty())
InBlock.gif            
throw new java.util.EmptyStackException();
InBlock.gif        
return pool.remove(pool.size() - 1);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void push(Object el) dot.gif{
InBlock.gif        pool.add(el);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String toString() dot.gif{
InBlock.gif        
return pool.toString();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

4。栈的应用:
1)程序中的匹配分割符,如(,[,{等符号
2)大数的运算,比如大数相加,转化为字符串,利用栈处理
3)回文字,例子:
ExpandedBlockStart.gifContractedBlock.gif/** *//**
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

None.gif
package com.sohu.blog.denns_zane.stackqueue;
None.gif
None.gif
import java.io.BufferedReader;
None.gif
import java.io.IOException;
None.gif
import java.io.InputStreamReader;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** *//**
InBlock.gif * 
@author dennis
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public class HuiWen dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 
@param args
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        BufferedReader buffer 
= new BufferedReader(new InputStreamReader(
InBlock.gif                System.in));
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            String str 
= buffer.readLine();
InBlock.gif            
if (str != null)
InBlock.gif                isHuiWen(str);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (IOException e) dot.gif{
InBlock.gif            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// TODO Auto-generated method stub
InBlock.gif

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void isHuiWen(String str) dot.gif{
InBlock.gif        
int length = str.length();
InBlock.gif        
char[] ch = str.toCharArray();
InBlock.gif        Stack stack 
= new Stack();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (length % 2 == 0dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i = 0; i < length / 2; i++dot.gif{
InBlock.gif                stack.push(ch[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i = length / 2; i < length - 1; i++dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (ch[i] != ((Character) stack.pop()).charValue()) dot.gif{
InBlock.gif                    System.out.println(
"不是回文字!!");
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            System.out.println(str 
+ "是回文字!!");
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i = 0; i < length / 2; i++dot.gif{
InBlock.gif                stack.push(ch[i]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (int i = (length / 2 + 1); i < length - 1; i++dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (ch[i] != ((Character) stack.pop()).charValue()) dot.gif{
InBlock.gif                    System.out.println(
"不是回文字!!");
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            System.out.println(str 
+ "是回文字!!");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

4)java虚拟机中的本地方法栈


二。队列(queue)
1。概念:队列也是线性结构,从尾部添加元素,从头部删除元素。与栈相反,队列是先入先出结构(FIFO)
2。队列主要操作:
.clear() ——清空队列
.isEmpty()——判断队列是否为空
.enqueue(el)——从尾部插入元素el
.dequeue()——从队列中起出第一个元素,并删除
.firstEl()——返回队列第一个元素,不删除。

3。队列的实现:
1)环形数组:需要考虑队列已满的两种情况,1,第一个元素在最后一个元素之后;2,第一个元素在第一单元,最后一个元素在最后单元。给出一个java实现:
ExpandedBlockStart.gifContractedBlock.gif/** *//**
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

None.gif
package com.sohu.blog.denns_zane.stackqueue;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** *//**
InBlock.gif * 
@author dennis
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

None.gif
// queue implemented as an array
ExpandedBlockStart.gifContractedBlock.gif
public class ArrayQueue dot.gif{
InBlock.gif    
private int first, last, size;
InBlock.gif
InBlock.gif    
private Object[] storage;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public ArrayQueue() dot.gif{
InBlock.gif        
this(100);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public ArrayQueue(int n) dot.gif{
InBlock.gif        size 
= n;
InBlock.gif        storage 
= new Object[size];
InBlock.gif        first 
= last = -1;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public boolean isFull() dot.gif{
InBlock.gif        
return first == 0 && last == size - 1 || first == last + 1;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public boolean isEmpty() dot.gif{
InBlock.gif        
return first == -1;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void enqueue(Object el) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (last == size - 1 || last == -1dot.gif{
InBlock.gif            storage[
0= el;
InBlock.gif            last 
= 0;
InBlock.gif            
if (first == -1)
InBlock.gif                first 
= 0;
ExpandedSubBlockEnd.gif        }
 else
InBlock.gif            storage[
++last] = el;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Object dequeue() dot.gif{
InBlock.gif        Object tmp 
= storage[first];
InBlock.gif        
if (first == last)
InBlock.gif            last 
= first = -1;
InBlock.gif        
else if (first == size - 1)
InBlock.gif            first 
= 0;
InBlock.gif        
else
InBlock.gif            first
++;
InBlock.gif        
return tmp;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void printAll() dot.gif{
InBlock.gif        
for (int i = 0; i < size; i++)
InBlock.gif            System.out.print(storage[i] 
+ " ");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
2)更适合实现队列的双向链表:
ExpandedBlockStart.gifContractedBlock.gif
/** *//**
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

None.gif
package com.sohu.blog.denns_zane.stackqueue;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** *//**
InBlock.gif * 
@author dennis
InBlock.gif * 
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public class Queue dot.gif{
InBlock.gif    
private java.util.LinkedList list = new java.util.LinkedList();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Queue() dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void clear() dot.gif{
InBlock.gif        list.clear();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public boolean isEmpty() dot.gif{
InBlock.gif        
return list.isEmpty();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Object firstEl() dot.gif{
InBlock.gif        
return list.getFirst();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Object dequeue() dot.gif{
InBlock.gif        
return list.removeFirst();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void enqueue(Object el) dot.gif{
InBlock.gif        list.add(el);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String toString() dot.gif{
InBlock.gif        
return list.toString();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
文章转自庄周梦蝶  ,原文发布时间5.17

4。队列的应用:线性规划方面

最后更新:2017-05-17 11:36:28

  上一篇:go  算法之简单排序
  下一篇:go  欢迎加入阿里云云HBase技术交流群