棧、隊列、優先級隊列
-------------------
棧《刷盤子之後摞起來的盤子,最後放進去的,最先被使用。LIFO 後進先出。棧頂棧低。PUSH入棧 POP出棧》
棧只允許訪問一個數據項:即最後插入的數據項。移除這個數據項後才能訪問
倒數第二個插入的數據項,以此類推。《有個top指針一直指向棧頂元素》
基本操作:
PUSH入棧 新加進來的盤子 只能放在最頂上(棧頂指針移動+1)
POP出棧 用盤子只能拿最頂上的盤子 (棧頂指針移動-1)
棧實例
1.單詞逆序
字母從輸入的字符串中一個一個提取出來並壓入棧中。接着他們依次彈出棧,並顯示出來。
因爲棧具有後進先出的特性。所以字母的順序就顛倒過來了。
2.分隔符匹配
程序不斷讀取字符串,每次讀取一個字符。若發現他是左分隔符,則將它壓入棧中。
當輸入中讀到一個右分隔符時,彈出棧頂的左分隔符。並且查看它是否和右分隔符匹配。
如果不匹配程序報錯。
如果棧中沒有左分隔符和右分隔符匹配或者存在一直沒有匹配的分隔符。程序報錯。
分隔符沒有被匹配的表現是:棧中仍然留有分隔符。
可行性在於:最後出現的左邊分隔符 需要最先匹配。這個規律符合LIFO棧的特點。
demo:a{b(c[d]e)f}
所讀字符 棧中內容( _表示空棧 棧底-->棧頂)
a _
{ {
b {
( {(
c {(
[ {([
d {([
] {(
e {(
) {
f {
} _
棧的效率O(1):
(數組實現的棧) 入棧出棧的時間複雜度 都爲O(1)
-------------------
**隊列
有點類似棧的數據結構。隊列中第一個插入的數據也會最先被移除掉。(火車站排隊買票,FIFO先進先出。隊頭 隊尾)
敲擊鍵盤時也有一個存儲鍵入內容的隊列。同樣如果使用文字處理程序敲擊一個鍵,而
計算機有暫時要做其他的事情。敲擊的內容不會丟失。它會排在隊列中等待。直到文字處理程序
有時間來讀取它。利用隊列保證了鍵入內容在處理時其順序不會改變。
基本操作:
插入:插入一個數據項,即把一個數據項放入隊尾。
刪除:刪除一個數據項,即把從隊頭一個數據項。
循環隊列
爲了避免隊列不滿 卻不能插入新數據的問題,可以讓隊尾指針繞回到數組開始的位置。這就是循環隊列(又叫:緩衝環)
循環隊列的環繞式處理
隊列效率O(1):
和棧一樣,隊列中插入和移除數據的時間複雜度均爲O(1)。
雙端隊列
雙端隊列就是一個兩端都是結尾的隊列。隊列的每一端都可以插入和移除數據項。雙端隊列通過方法的限制可以實現棧和隊列的功能。
-------------------
**優先級隊列
和普通隊列一樣,優先級隊列有一個隊頭和隊尾,並且也是從隊頭移除數據項,不過在優先級隊列中,數據項
按照關鍵字的值有序,這樣關鍵字最小的數據項(某些實現中是關鍵字最大的數據項)總是在隊頭。
數據項插入的時候就會按照順序插入到合適的位置以確保隊列的順序。
優先級隊列用於對信件排序的例子:
每拿到一封信就根據信的優先級把他插入到沒看過的郵件堆裏。如果必須馬上回復 就放在最上面。
如果可以等空閒時間再回復,就可以把他放在最底下。中等優先的信件就放在中間。
級別越高的放的位置就越高。郵件堆的頂端對應優先級隊列的隊頭。
例子:
搶先式多任務操作系統中,程序排在優先級隊列中,這樣優先級最高的程序就會先得打時間片並得以運行。
優先級隊列效率:
插入操作O(N)
刪除操作O(1)
-------------------
總結:
1.棧、隊列、優先級隊列 這些數據結構中 只有一個數據項可以被訪問。
2.棧允許訪問最後一個插入的數據項(即棧頂數據),其最重要的操作 PUSH棧頂插入 和 POP 棧頂彈出
3.隊列只允許訪問第一個插入的數據項。其重要操作是在隊尾插入數據項 、在隊頭移除數據項
4.隊列可以基於數組 實現循環隊列(循環隊列的環繞式處理)
5.優先級隊列只允許訪問最小(或最大的)數據項。最重要操作 有序的插入新數據項 和移除關鍵字最小的數據項。
################################################################################
《1》棧
// stack.java
// demonstrates stacks
// to run this program: C>java StackApp
////////////////////////////////////////////////////////////////
class StackX
{
private int maxSize; // size of stack array
private long[] stackArray;
private int top; // top of stack
//--------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s; // set array size
stackArray = new long[maxSize]; // create array
top = -1; // no items yet
}
//--------------------------------------------------------------
public void push(long j) // put item on top of stack
{
stackArray[++top] = j; // increment top, insert item
}
//--------------------------------------------------------------
public long pop() // take item from top of stack
{
return stackArray[top--]; // access item, decrement top
}
//--------------------------------------------------------------
public long peek() // peek at top of stack
{
return stackArray[top];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
//--------------------------------------------------------------
public boolean isFull() // true if stack is full
{
return (top == maxSize-1);
}
//--------------------------------------------------------------
} // end class StackX
////////////////////////////////////////////////////////////////
class StackApp
{
public static void main(String[] args)
{
StackX theStack = new StackX(10); // make new stack
theStack.push(20); // push items onto stack
theStack.push(40);
theStack.push(60);
theStack.push(80);
while( !theStack.isEmpty() ) // until it's empty,
{ // delete item from stack
long value = theStack.pop();
System.out.print(value); // display it
System.out.print(" ");
} // end while
System.out.println("");
} // end main()
} // end class StackApp
////////////////////////////////////////////////////////////////
################################################################################
《2》隊列
// Queue.java
// demonstrates queue
// to run this program: C>java QueueApp
////////////////////////////////////////////////////////////////
class Queue
{
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
//--------------------------------------------------------------
public Queue(int s) // constructor
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//--------------------------------------------------------------
public void insert(long j) // put item at rear of queue
{
if(rear == maxSize-1) // deal with wraparound
rear = -1;
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}
//--------------------------------------------------------------
public long remove() // take item from front of queue
{
long temp = queArray[front++]; // get value and incr front
if(front == maxSize) // deal with wraparound
front = 0;
nItems--; // one less item
return temp;
}
//--------------------------------------------------------------
public long peekFront() // peek at front of queue
{
return queArray[front];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{
return (nItems==0);
}
//--------------------------------------------------------------
public boolean isFull() // true if queue is full
{
return (nItems==maxSize);
}
//--------------------------------------------------------------
public int size() // number of items in queue
{
return nItems;
}
//--------------------------------------------------------------
} // end class Queue
////////////////////////////////////////////////////////////////
class QueueApp
{
public static void main(String[] args)
{
Queue theQueue = new Queue(5); // queue holds 5 items
theQueue.insert(10); // insert 4 items
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove(); // remove 3 items
theQueue.remove(); // (10, 20, 30)
theQueue.remove();
theQueue.insert(50); // insert 4 more items
theQueue.insert(60); // (wraps around)
theQueue.insert(70);
theQueue.insert(80);
while( !theQueue.isEmpty() ) // remove and display
{ // all items
long n = theQueue.remove(); // (40, 50, 60, 70, 80)
System.out.print(n);
System.out.print(" ");
}
System.out.println("");
} // end main()
} // end class QueueApp
////////////////////////////////////////////////////////////////
################################################################################
《3》優先級隊列
// priorityQ.java
// demonstrates priority queue
// to run this program: C>java PriorityQApp
////////////////////////////////////////////////////////////////
class PriorityQ
{
// array in sorted order, from max at 0 to min at size-1
private int maxSize;
private long[] queArray;
private int nItems;
//-------------------------------------------------------------
public PriorityQ(int s) // constructor
{
maxSize = s;
queArray = new long[maxSize];
nItems = 0;
}
//-------------------------------------------------------------
public void insert(long item) // insert item
{
int j;
if(nItems==0) // if no items,
queArray[nItems++] = item; // insert at 0
else // if items,
{
for(j=nItems-1; j>=0; j--) // start at end,
{
if( item > queArray[j] ) // if new item larger,
queArray[j+1] = queArray[j]; // shift upward
else // if smaller,
break; // done shifting
} // end for
queArray[j+1] = item; // insert it
nItems++;
} // end else (nItems > 0)
} // end insert()
//-------------------------------------------------------------
public long remove() // remove minimum item
{ return queArray[--nItems]; }
//-------------------------------------------------------------
public long peekMin() // peek at minimum item
{ return queArray[nItems-1]; }
//-------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{ return (nItems==0); }
//-------------------------------------------------------------
public boolean isFull() // true if queue is full
{ return (nItems == maxSize); }
//-------------------------------------------------------------
} // end class PriorityQ
////////////////////////////////////////////////////////////////
class PriorityQApp
{
public static void main(String[] args)
{
PriorityQ thePQ = new PriorityQ(5);
thePQ.insert(30);
thePQ.insert(50);
thePQ.insert(10);
thePQ.insert(40);
thePQ.insert(20);
while( !thePQ.isEmpty() )
{
long item = thePQ.remove();
System.out.print(item + " "); // 10, 20, 30, 40, 50
} // end while
System.out.println("");
} // end main()
//-------------------------------------------------------------
} // end class PriorityQApp
////////////////////////////////////////////////////////////////
------------------------------------------------------------------------------