閱讀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技術交流群