栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++使用模板类实现链式栈

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++使用模板类实现链式栈

本文实例为大家分享了C++使用模板类实现链式栈的具体代码,供大家参考,具体内容如下

一、实现程序:

1.Stack.h

#ifndef Stack_h
#define Stack_h
 
template 
class Stack {
public:
  Stack(){}; // 构造函数
  void Push(const T x); // 新元素进栈
  bool Pop(); // 栈顶元素出栈
  virtual bool getTop(T &x) const = 0; // 读取栈顶元素,由x返回
  virtual bool isEmpty() const = 0; // 判断栈空否
  // virtual bool isFull() const = 0; // 判断栈满否,因为链式栈不存在不满的情况
  virtual int getSize() const = 0; // 计算栈中元素个数
};
 
 
#endif 

2.linkedStack.h

#ifndef linkedStack_h
#define linkedStack_h
#include 
#include "Stack.h"
using namespace std;
 
template 
struct linkNode {
  T data;
  linkNode *link;
};
 
//类的前置声明
template 
class linkedStack;
 
//友元函数的声明
template 
ostream& operator<<(ostream& out, linkedStack& s);
template 
class linkedStack: public Stack {
public:
  linkedStack(); // 构造函数
  ~linkedStack();// 析构函数
  void Push(const T x); // 进栈
  bool Pop(); // 出栈
  bool getTop(T &x) const; // 读取栈顶元素
  bool isEmpty()const; // 判断栈是否为空
  int getSize()const; // 求栈的元素个数
  void makeEmpty(); // 清空栈的内容
  friend ostream& operator << (ostream& out, linkedStack& s); // 重载输出函数
private:
  linkNode *top; // 栈顶指针,即链头指针
};
template 
linkedStack::linkedStack() {
  // 构造函数,置空栈
  top = new linkNode(); // 引入头指针:不存放数据
  top->link = NULL;
}
template 
linkedStack::~linkedStack() {
  // 析构函数,释放内存空间
  makeEmpty();
}
template 
void linkedStack::Push(const T x) {
  // 进栈:将元素值x插入到链式栈的栈顶,即链头
  linkNode *newNode = new linkNode(); // 创建包含x的新结点
  if(newNode == NULL) {
    cerr << "内存空间分配失败!" << endl;
    exit(1);
  }
  newNode->data = x;
  newNode->link = top->link; // 指向头指针的下一个结点:即栈中第一个存放有效数据的结点
  top->link = newNode; // 头指针往前移
}
template 
bool linkedStack::Pop() {
  // 出栈:删除栈顶结点
  if(isEmpty())
    return false; // 栈空,不出栈
  linkNode *p = top->link; // 暂存栈顶元素
  top->link = p->link; // 栈顶指针退到新的栈顶位置
  delete p;
  p = NULL;
  return true;
}
 
template 
bool linkedStack::getTop(T &x) const {
  // 读取栈顶元素
  if(isEmpty())
    return false;
  x = top->link->data; // 栈不空,返回栈顶元素的值。这里top为头指针,所以栈顶元素为:top->link
  return true;
}
 
template 
bool linkedStack::isEmpty()const {
  // 判断栈是否为空
  if(top->link == NULL) // 栈为空
    return true;
  return false;
}
 
template 
int linkedStack::getSize()const {
  // 求栈的元素个数
  int len = 0;
  
  linkNode *current = top->link;
  while(current != NULL) {
    len++;
    current = current->link;
  }
  return len;
}
 
template 
void linkedStack::makeEmpty() {
  // 清空栈的内容
  linkNode *current = top->link;
  while(current != NULL) {
    top->link = current->link; // 保存链式栈准备要删除的结点的下一个结点,防止丢失
    delete current; // 释放
    current = NULL; // 先指向空
    current = top->link; // 再指向剩下链表的首结点
  }
}
 
template 
ostream& operator<<(ostream& out, linkedStack& s) {
  // 重载输出函数
  linkNode *current = s.top->link;
  while(current != NULL) {
    out << current->data << " ";
    current = current->link;
  }
  return out;
}
#endif 

3.main.cpp

#include "linkedStack.h"
using namespace std;
 
int main(int argc, const char * argv[]) {
  int n, x, choice, len; // val存储值,choose存储用户的选择
  bool finished = false;
  linkedStack L; // 对象
  
  while(!finished) {
    cout << "1:建栈:" << endl;
    cout << "2:进栈" << endl;
    cout << "3:出栈:" << endl;
    cout << "4:读取栈顶元素:" << endl;
    cout << "5:栈是否为空:" << endl;
    cout << "6:栈中的元素个数:" << endl;
    cout << "7:清空栈的内容:" << endl;
    cout << "8:输出栈中元素的值:" << endl;
    cout << "9:退出" << endl;
    cout << "请输入你的选择[1-9]:" << endl;
    cin >> choice;
    switch(choice) {
      case 1:
 cout << "请输入要进栈的数的个数:";
 cin >> n;
 cout << "请输入要进栈的数(以空格隔开):" << endl;
 for(int i=0; i < n; i++) {
   cin >> x;
   L.Push(x);
 }
 break;
      case 2:
 cout << "请输入要进栈的数:";
 cin >> x;
 L.Push(x);
 break;
      case 3:
 if(L.Pop())
   cout << "出栈成功!" << endl;
 else
   cout << "栈为空!" << endl;
 break;
      case 4:
 if(L.getTop(x))
   cout << "栈顶元素的值为:" << x << endl;
 else
   cout << "栈为空!" << endl;
 break;
      case 5:
 if(L.isEmpty())
   cout << "栈为空!" << endl;
 else
   cout << "栈不为空!" << endl;
 break;
      case 6:
 len = L.getSize();
 cout << "栈中的元素个数为:" << len << endl;
 break;
      case 7:
 L.makeEmpty(); // 清空栈
 break;
      case 8:
 if(L.isEmpty())
   cout << "栈为空!" << endl;
 else
   cout << L << endl;
 break;
      case 9:
 finished = true;
 break;
      default:
 cout << "输入错误,请重新输入!" << endl;
    } // switch
  } // while
  return 0;
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/61717.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号