利用模板实现简单的栈类(数组和单链表)

本文介绍了两种实现栈的方法:一种是使用数组实现固定大小的栈,另一种是利用单链表实现动态调整大小的栈。文章提供了完整的源代码,并通过实例展示了如何进行入栈、出栈等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要的功能是实现一个后进先出的列表,有入栈、出栈、返回大小、判空等基本功能

#pragma once
using namespace std;
const int MAXSIZE = 0xfff;
template<class type>
class Class_Linkstack
{
    int top;
    type* my_s;
    int max_size;
public:
    Class_Linkstack() :top(-1), max_size(MAXSIZE)
    {
        my_s = new type[max_size]; 
        if (my_s == NULL)
        {
            cerr << "动态存储分配失败!" << endl;
            exit(1);
        }
    }
    Class_Linkstack(int size) :top(-1), max_size(size)
    {
        my_s = new type[size];
        if (my_s == NULL)
        {
            cerr << "动态存储分配失败!" << endl;
            exit(1);
        }
    }
    ~Class_Linkstack() { delete[] my_s; }
    bool Empty_Linkstack();
    void Push_Linkstack(type tp);
    void Pop_Linkstack();
    type Top_Linkstack();
    int Size_Linkstack();   
    void Print_Linkstack();
};


template<class type>
void Class_Linkstack<type>::Print_Linkstack()
{
    if (top == -1)
        cout << "空栈" << endl;
    else
    {
        for (int i = 0; i < top+1; i++)
            cout << my_s[i] << '\t';
    }
}

template<class type>
bool Class_Linkstack<type>::Empty_Linkstack()
{
    if (top == -1)
        return true;
    else
    {
        return false;
    }
}
template<class type>
void Class_Linkstack<type>::Push_Linkstack(type tp)
{
    if (top + 1 < max_size)
        my_s[++top] = tp;
    else
    {
        cout << "栈已满" << endl;
        exit(1);
    }
}
template<class type>
void Class_Linkstack<type>::Pop_Linkstack()
{
    if (top == -1)
    {
        cout << "为空栈" << endl;
        exit(1);
    }
    else
    {
        my_s[top--] = 0;
    }
}
template<class type>
type Class_Linkstack<type>::Top_Linkstack()
{
    if (top != -1)
        return my_s[top];
    else
    {
        cout << "为空栈" << endl;
        exit(1);
    }
}
template<class type>
int Class_Linkstack<type>::Size_Linkstack()
{
    return top + 1;
}

测试代码

#include "Class_Linkstack.h"
int main()
{
    Class_Linkstack<int> sk1(5);
    for (int i = 0; i < 5;i++ )
        sk1.Push_Linkstack(i * 2 + 1);
    sk1.Print_Linkstack();  
    system("pause");
    return 0;
}

补充(通过单链表实现)

上面是通过数组来实现,与数组相比,链表实现更灵活,更容易增删元素。
单链表实现的核心思想是不断更新栈顶指针,来实现出栈压栈,每一个节点是一个结构体,包含一个value和一个next指针指向下一个元素,初始化时将栈顶指针置为NULL。

#pragma once
using namespace std;

template<class type>
struct listnode
{
    type value;
    listnode* next;
    listnode(type v,listnode* p):value(v),next(p){  }
};

template<class type>
class List_stack
{
    listnode<type>* top;
    int size = 0;
public:
    List_stack();
    void Push(type &tp);
    void Pop();
    bool Empty();
    int Size();
    void Print();
    ~List_stack()
    {
        while (top)
        {
            listnode<type> * p = top;
            top = top->next;
            delete p;
        }
    }
};
template<class type>
bool List_stack<type>::Empty()
{
    if (top == NULL)
        return true;
    else
    {
        return false;
    }
}
template<class type>
List_stack<type>::List_stack()
{
    top = NULL;
    size = 0;
}
template<class type>
void List_stack<type>::Push(type &tp)
{
    listnode<type> *tmp=new listnode<type>(tp,top);
    top = tmp;
    size++;
}
template<class type>
void List_stack<type>::Pop()
{
    if (top == NULL)
    {
        cout << "为空栈" << endl;
    }
    else
    {
        top = top->next;
        size--;
    }

}
template<class type>
int List_stack<type>::Size()
{
    return size;
}
template<class type>
void List_stack<type>::Print()
{
    listnode<type>* tmp = top;
    while (tmp != NULL)
    {
        cout << tmp->value << '\t';
        tmp = tmp->next;
    }
}

简单测试:

int main()
{
    List_stack<int> ls;
    for (int i = 0; i < 5; i++)
        ls.Push(i);
    ls.Print();
    ls.Pop();
    ls.Pop();
    cout << endl;
    ls.Print();
    cout << endl;
    cout << ls.Size();
    system("pause");
    return 0;
}

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值