数据结构与算法之美 栈的代码实现

java

// 基于数组实现的顺序栈
public class ArrayStack {
  private String[] items;  // 数组
  private int count;       // 栈中元素个数
  private int n;           // 栈的大小
 
  // 初始化数组,申请一个大小为 n 的数组空间
  public ArrayStack(int n) {
    this.items = new String[n];
    this.n = n;
    this.count = 0;
  }
 
  // 入栈操作
  public boolean push(String item) {
    // 数组空间不够了,直接返回 false,入栈失败。
    if (count == n) return false;
    // 将 item 放到下标为 count 的位置,并且 count 加一
    items[count] = item;
    ++count;
    return true;
  }
  
  // 出栈操作
  public String pop() {
    // 栈为空,则直接返回 null
    if (count == 0) return null;
    // 返回下标为 count-1 的数组元素,并且栈中元素个数 count 减一
    String tmp = items[count-1];
    --count;
    return tmp;
  }
}

swift

public struct Stack<T> {
    fileprivate var array = [T]()
    
    public var isEmpty: Bool {
        return array.isEmpty
    }
    
    public var count: Int {
        return array.count
    }
    
    public mutating func push(_ element: T) {
        array.append(element)
    }
    
    public mutating func pop() -> T? {
        return array.popLast()
    }
    
    public var top: T? {
        return array.last
    }
}

//新建一个栈
var stackOfAnimals = Stack(array: ["lion", "elephant", "giraffe"])

//压栈
stackOfAnimals.push("zebra")

//结果是:["lion", "elephant", "giraffe", "zebra"]
print(stackOfAnimals.array)

//出栈
stackOfAnimals.pop()

//栈顶的元素是:"giraffe"
stackOfAnimals.top

//false
stackOfAnimals.isEmpty

ES6 栈结构代码

push(element):push是进栈操作,其中element是需要进栈的元素,返回操作成功与否布尔值。
pop():pop移除栈顶元素操作,返回移除的栈顶元素。
size():获取栈中的栈元素数量,返回一个数字。
isEmpty():判断栈是否为空,返回一个布尔值。
peek():返回栈顶元素,但不移除栈顶元素。
bottom():返回栈底元素。
clear():清除所有栈元素,返回操作成功与否布尔值。


class Stack{
    constructor(){
        this.lists = [];
    }
    size(){
        return this.lists.length;
    }
    isEmpty(){
        return this.lists.length === 0;
    }
    peek(){
        const topIndex = this.size() -1;
        return this.lists[topIndex];
    }
    bottom(){
        return this.list[0];
    }
    push(element){
        this.lists.push(element);
        return true;
    }
    pop(){
        return this.lists.pop();
    }
    clear(){
        this.lists = [];
        return true;
    }
    toString(){
        let result = "";
        this.lists.forEach(value=>{
            result = result + ""+value;
        });
        return result;
    }
}

ES5 栈结构代码

push(element):push是进栈操作,其中element是需要进栈的元素,返回操作成功与否布尔值。
pop():pop移除栈顶元素操作,返回移除的栈顶元素。
size():获取栈中的栈元素数量,返回一个数字。
isEmpty():判断栈是否为空,返回一个布尔值。
peek():返回栈顶元素,但不移除栈顶元素。
bottom():返回栈底元素。
clear():清除所有栈元素,返回操作成功与否布尔值。

function Stack(){
    this.lists = [];
}
Stack.prototype.push = function(element){
    this.lists.push(element);
    return true;
}
Stack.prototype.pop = function(){
    return this.lists.pop();
}
Stack.prototype.clear = function(){
    this.lists = [];
    return true;
}
Stack.prototype.peek = function(){
    var topIndex = this.size() -1;
    return this.lists[topIndex];
}
Stack.prototype.size = function(){
    return this.lists.length;
}
Stack.prototype.isEmpty = function(){
    return this.lists.length === 0;
}
Stack.prototype.bottom = function(){
    return this.lists[0];
}
Stack.prototype.toString = function(){
    var result = "";
    for(var i=0;i<this.lists.length;i++){
        result = result + '' +this.lists[i];
    }
    return result;
}

C++

template<typename T>
class ArrayStack
{
public:
    ArrayStack(int s = 10);    //默认的栈容量为10
    ~ArrayStack();
 
public:
    T top();            //获取栈顶元素
    void push(T t);        //压栈操作
    T pop();            //弹栈操作
    bool isEmpty();        //判空操作
    int size();            //求栈的大小
 
private:
    int count;            //栈的元素数量
    int capacity;        //栈的容量
    T * array;            //底层为数组
};
 
/*栈的判空操作*/
template <typename T>
bool ArrayStack<T>::isEmpty()
{
     return count == 0; //栈元素为0时为栈空
};
 
/*返回栈的大小*/
 template <typename  T>
int ArrayStack<T>::size()
{
     return count;
};
 
/*插入元素*/
template <typename T>
void ArrayStack<T>::push(T t)
{
     if (count != capacity)    //先判断是否栈满
     {
         array[count++] = t;   
     }
};
 
/*弹栈*/
template <typename T>
T ArrayStack<T>::pop()
{
     if (count != 0)    //先判断是否是空栈
     {
         return array[--count];
     }
};
 
/*获取栈顶元素*/
template <typename T>
T ArrayStack<T>::top()
{
     if (count != 0)
     {
         return array[count - 1];
     }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值