国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C語言利用模板實現簡單的棧類

2020-05-23 13:22:09
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C語言利用模板實現簡單的棧類(數組和單鏈表),供大家參考,具體內容如下

主要的功能是實現一個后進先出的列表,有入棧、出棧、返回大小、判空等基本功能

#pragma onceusing 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 onceusing 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語言,模板,棧類

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兰坪| 白城市| 福鼎市| 丹东市| 常山县| 盖州市| 丹阳市| 宝兴县| 贵南县| 托克托县| 新竹县| 白朗县| 德保县| 巴马| 自治县| 抚州市| 庆阳市| 美姑县| 兰考县| 天气| 嘉兴市| 六盘水市| 济宁市| 乐山市| 谢通门县| 徐州市| 班玛县| 得荣县| 大田县| 通河县| 舒兰市| 淄博市| 绥滨县| 商洛市| 鄄城县| 师宗县| 社旗县| 哈尔滨市| 永寿县| 曲松县| 闽清县|