欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言利用模板實現(xiàn)簡單的棧類

 更新時間:2018年12月24日 15:10:44   作者:chi_mian  
這篇文章主要為大家詳細(xì)介紹了C語言利用模板實現(xiàn)簡單的棧類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

#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 << "動態(tài)存儲分配失??!" << endl;
      exit(1);
    }
  }
  Class_Linkstack(int size) :top(-1), max_size(size)
  {
    my_s = new type[size];
    if (my_s == NULL)
    {
      cerr << "動態(tài)存儲分配失??!" << 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;
}

補充(通過單鏈表實現(xiàn))

上面是通過數(shù)組來實現(xiàn),與數(shù)組相比,鏈表實現(xiàn)更靈活,更容易增刪元素。
單鏈表實現(xiàn)的核心思想是不斷更新棧頂指針,來實現(xiàn)出棧壓棧,每一個節(jié)點是一個結(jié)構(gòu)體,包含一個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;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++?opencv圖像處理實現(xiàn)灰度變換示例

    C++?opencv圖像處理實現(xiàn)灰度變換示例

    這篇文章主要為大家介紹了C++?opencv圖像處理灰度變換的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • C++中的可移植性和跨平臺開發(fā)教程詳解

    C++中的可移植性和跨平臺開發(fā)教程詳解

    這篇文章主要為大家介紹了C++中的可移植性和跨平臺開發(fā)教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • C++中內(nèi)存分區(qū)及其作用分析

    C++中內(nèi)存分區(qū)及其作用分析

    C++內(nèi)存分區(qū)包括棧區(qū)、堆區(qū)、全局靜態(tài)區(qū)、常量區(qū),各自負(fù)責(zé)不同的數(shù)據(jù)存儲和回收,棧區(qū)主要用于存放函數(shù)局部變量和參數(shù),堆區(qū)用于動態(tài)分配內(nèi)存,全局靜態(tài)區(qū)用于存放全局靜態(tài)變量和靜態(tài)成員變量,常量區(qū)用于存放常量和字符串常量
    2023-04-04
  • Qt中JSON操作的具體使用

    Qt中JSON操作的具體使用

    本文主要介紹了Qt中JSON操作的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • C語言中變量與其內(nèi)存地址對應(yīng)的入門知識簡單講解

    C語言中變量與其內(nèi)存地址對應(yīng)的入門知識簡單講解

    這篇文章主要介紹了C語言中變量與其內(nèi)存地址對應(yīng)的入門知識簡單講解,同時這也是掌握指針部分知識的基礎(chǔ),需要的朋友可以參考下
    2015-12-12
  • 關(guān)于雙向鏈表的增刪改查和排序的C++實現(xiàn)

    關(guān)于雙向鏈表的增刪改查和排序的C++實現(xiàn)

    下面小編就為大家?guī)硪黄P(guān)于雙向鏈表的增刪改查和排序的C++實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • C++實現(xiàn)信息管理系統(tǒng)

    C++實現(xiàn)信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++詳細(xì)分析講解函數(shù)參數(shù)的擴(kuò)展

    C++詳細(xì)分析講解函數(shù)參數(shù)的擴(kuò)展

    在C++中,定義函數(shù)時可以給形參指定一個默認(rèn)的值,這樣調(diào)用函數(shù)時如果沒有給這個形參賦值(沒有對應(yīng)的實參),那么就使用這個默認(rèn)的值。也就是說,調(diào)用函數(shù)時可以省略有默認(rèn)值的參數(shù)
    2022-04-04
  • 淺談C++類型轉(zhuǎn)化(運算符重載函數(shù))和基本運算符重載(自增自減)

    淺談C++類型轉(zhuǎn)化(運算符重載函數(shù))和基本運算符重載(自增自減)

    下面小編就為大家?guī)硪黄獪\談C++類型轉(zhuǎn)化(運算符重載函數(shù))和基本運算符重載(自增自減)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • C++中引用、內(nèi)聯(lián)函數(shù)、auto關(guān)鍵字和范圍for循環(huán)詳解

    C++中引用、內(nèi)聯(lián)函數(shù)、auto關(guān)鍵字和范圍for循環(huán)詳解

    本文主要梳理了C++當(dāng)中一些瑣碎的知識點,包括有命名空間,缺省參數(shù),引用,auto關(guān)鍵字和內(nèi)聯(lián)函數(shù),文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-02-02

最新評論