以下是使用Ada、C++和Java分別實現堆疊(Stack)及相關函數,其中每個元素包含一個16位元組(Byte)的文字:
### Ada
```ada
-- Stack.ads
generic
type Element is private;
package Stack is
type Stack is limited private;
procedure Push(Item : Element; S : in out Stack);
procedure Pop(S : in out Stack);
function Top(S : Stack) return Element;
function IsEmpty(S : Stack) return Boolean;
private
type Node;
type NodePtr is access Node;
type Node is
record
Data : Element;
Next : NodePtr;
end record;
type Stack is
record
TopNode : NodePtr := null;
end record;
end Stack;
-- Stack.adb
package body Stack is
procedure Push(Item : Element; S : in out Stack) is
New_Node : NodePtr := new Node'(Data => Item, Next => S.TopNode);
begin
S.TopNode := New_Node;
end Push;
procedure Pop(S : in out Stack) is
Temp : NodePtr := S.TopNode;
begin
if Temp /= null then
S.TopNode := Temp.Next;
delete Temp;
end if;
end Pop;
function Top(S : Stack) return Element is
begin
if S.TopNode /= null then
return S.TopNode.Data;
else
raise Program_Error;
end if;
end Top;
function IsEmpty(S : Stack) return Boolean is
begin
return S.TopNode = null;
end IsEmpty;
end Stack;
```
### C++
```cpp
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
stack<string> myStack;
// Push elements onto the stack
myStack.push("Element 1");
myStack.push("Element 2");
myStack.push("Element 3");
// Pop elements from the stack and print them
while (!myStack.empty()) {
cout << "Top: " << myStack.top() << endl;
myStack.pop();
}
return 0;
}
```
### Java
```java
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<String> myStack = new Stack<>();
// Push elements onto the stack
myStack.push("Element 1");
myStack.push("Element 2");
myStack.push("Element 3");
// Pop elements from the stack and print them
while (!myStack.empty()) {
System.out.println("Top: " + myStack.peek());
myStack.pop();
}
}
}
```
這些程式碼示例實現了堆疊及相關函數,並在堆疊中存儲16位元組(Byte)的文字元素。