C语言利用模板实现简单的栈类

所属分类: 软件编程 / C 语言 阅读数: 40
收藏 0 赞 0 分享

本文实例为大家分享了C语言利用模板实现简单的栈类(数组和单链表),供大家参考,具体内容如下

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

#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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C语言非递归后序遍历二叉树

这篇文章主要为大家详细介绍了C语言非递归后序遍历二叉树,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言单链表实现多项式相加

这篇文章主要为大家详细介绍了C语言单链表实现多项式相加,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言二叉排序(搜索)树实例

这篇文章主要为大家详细介绍了C语言二叉排序树实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

NDK 数据结构之队列与栈等的实现

这篇文章主要介绍了NDK 数据结构之队列与栈等的实现的相关资料,希望通过本文大家能理解掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享

C/C++经典实例之模拟计算器示例代码

最近在看到的一个需求,本以为比较简单,但花了不少时间,所以下面这篇文章主要给大家介绍了关于C/C++经典实例之模拟计算器的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

C语言中的getchar和putchar的使用方法

这篇文章主要介绍了C语言中的getchar和putchar的使用方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现洗牌发牌排序功能的示例代码

本篇文章主要介绍了C++实现洗牌发牌排序功能的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C++计算图任意两点间的所有路径

这篇文章主要为大家详细介绍了C++求图任意两点间的所有路径 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

zlib库压缩和解压字符串STL string的实例详解

这篇文章主要介绍了zlib库压缩和解压字符串STL string的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

C/C++ 获取Windows系统的位数32位或64位的实现代码

这篇文章主要介绍了C/C++ 获取Windows系统的位数32位或64位的实现代码的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多