栈--链表实现

本文详细介绍了使用C++模板实现了一个栈的数据结构,包括栈的链表实现、构造函数、入栈、出栈、获取栈顶元素和栈的长度等方法,并通过示例展示了栈的使用。
摘要由CSDN通过智能技术生成

栈–链表实现

全部代码

#include <iostream>
#include <stdexcept>
using namespace std;
template<typename T>
//栈的链表实现
class Stack{
private:
    struct Node{
        T data;
        Node *next;
        Node(T d) : data(d),next(NULL){}
    };
    Node *head;//栈顶指针
    int size;
public:
    Stack():head(NULL),size(0) {}
    ~Stack();//析构函数(销毁栈)
    void push(T element);//入栈函数
    T pop();//出栈函数
    T top() const;//获取栈顶元素
    int getsize() const;//获取栈的长度
};
//析构函数(销毁栈)
template<typename T>
Stack<T>::~Stack(){
    while(head){
        Node*temp = head;
        head = head->next;
        delete temp;
    }
}
//入栈函数
template<typename T>
void Stack<T>::push(T element){
    Node *newnode = new Node(element);
    newnode->next = head;
    head = newnode;
    ++size;
}
//出栈函数
template<typename T>
T Stack<T>::pop(){
    if(head == NULL){
        throw std::underflow_error("Stack is empty");
    }
    T result = head->data;
    Node *temp = head;
    head = head->next;
    delete temp;
    --size;
    return result;
    
}
//获取栈顶元素
template<typename T>
T Stack<T>::top() const{
    if(head == NULL){
        throw std::underflow_error("Stack is empty");
    }
    return head->data;
}
//获取栈的长度
template<typename T>
int Stack<T>::getsize() const{
    return size;
}

int main(){
    Stack<int> st;
    st.push(4);
    st.push(7);
    st.push(13);
    cout<<st.top()<<endl;
    st.push(17);
    cout<<st.top()<<endl;
    st.pop();
    st.pop();
    cout<<st.top()<<endl;
    cout<<st.getsize() << endl;
    return 0;
}

结构体

class Stack{
private:
    struct Node{
        T data;
        Node *next;
        Node(T d) : data(d),next(NULL){}
    };
    Node *head;//栈顶指针
    int size;
public:
    Stack():head(NULL),size(0) {}
    ~Stack();//析构函数(销毁栈)
    void push(T element);//入栈函数
    T pop();//出栈函数
    T top() const;//获取栈顶元素
    int getsize() const;//获取栈的长度
};

析构函数

template<typename T>
Stack<T>::~Stack(){
    while(head){
        Node*temp = head;
        head = head->next;
        delete temp;
    }
}

入栈函数

template<typename T>
void Stack<T>::push(T element){
    Node *newnode = new Node(element);
    newnode->next = head;
    head = newnode;
    ++size;
}

出栈函数

template<typename T>
T Stack<T>::pop(){
    if(head == NULL){
        throw std::underflow_error("Stack is empty");
    }
    T result = head->data;
    Node *temp = head;
    head = head->next;
    delete temp;
    --size;
    return result;
}

获取栈顶元素

template<typename T>
T Stack<T>::top() const{
    if(head == NULL){
        throw std::underflow_error("Stack is empty");
    }
    return head->data;
}

获取栈长度

template<typename T>
int Stack<T>::getsize() const{
    return size;
}

测试

int main(){
    Stack<int> st;
    st.push(4);
    st.push(7);
    st.push(13);
    cout<<st.top()<<endl;
    st.push(17);
    cout<<st.top()<<endl;
    st.pop();
    st.pop();
    cout<<st.top()<<endl;
    cout<<st.getsize() << endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值