C語言利用模板實現(xiàn)簡單的棧類
本文實例為大家分享了C語言利用模板實現(xiàn)簡單的棧類(數(shù)組和單鏈表),供大家參考,具體內(nèi)容如下
主要的功能是實現(xià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é)點是一個結構體,包含一個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)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 04-02c語言函數(shù)調用后清空內(nèi)存 c語言調用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求階乘


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內(nèi)存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法


