C语言利用模板实现简单的栈类-成都快上网建站

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

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

创新互联公司专注于宁江企业网站建设,成都响应式网站建设,成都商城网站开发。宁江网站建设公司,为宁江等地区提供建站服务。全流程按需定制,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

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

#pragma once
using namespace std;
const int MAXSIZE = 0xfff;
template
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
void Class_Linkstack::Print_Linkstack()
{
  if (top == -1)
    cout << "空栈" << endl;
  else
  {
    for (int i = 0; i < top+1; i++)
      cout << my_s[i] << '\t';
  }
}

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

测试代码

#include "Class_Linkstack.h"
int main()
{
  Class_Linkstack 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
struct listnode
{
  type value;
  listnode* next;
  listnode(type v,listnode* p):value(v),next(p){ }
};

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

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

简单测试:

int main()
{
  List_stack 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语言利用模板实现简单的栈类
文章分享:http://kswjz.com/article/iehsid.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流