C語言利用模板實(shí)現(xiàn)簡單的棧類
本文實(shí)例為大家分享了C語言利用模板實(shí)現(xiàn)簡單的棧類(數(shù)組和單鏈表),供大家參考,具體內(nèi)容如下
主要的功能是實(shí)現(xiàn)一個(gè)后進(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 << "動(dòng)態(tài)存儲(chǔ)分配失敗!" << endl;
exit(1);
}
}
Class_Linkstack(int size) :top(-1), max_size(size)
{
my_s = new type[size];
if (my_s == NULL)
{
cerr << "動(dòng)態(tài)存儲(chǔ)分配失敗!" << 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;
}
測(cè)試代碼
#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;
}
補(bǔ)充(通過單鏈表實(shí)現(xiàn))
上面是通過數(shù)組來實(shí)現(xiàn),與數(shù)組相比,鏈表實(shí)現(xiàn)更靈活,更容易增刪元素。
單鏈表實(shí)現(xiàn)的核心思想是不斷更新棧頂指針,來實(shí)現(xiàn)出棧壓棧,每一個(gè)節(jié)點(diǎn)是一個(gè)結(jié)構(gòu)體,包含一個(gè)value和一個(gè)next指針指向下一個(gè)元素,初始化時(shí)將棧頂指針置為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;
}
}
簡單測(cè)試:
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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++容器適配與棧的實(shí)現(xiàn)及dequeque和優(yōu)先級(jí)詳解
這篇文章主要介紹了C++容器適配與棧的實(shí)現(xiàn)及dequeque和優(yōu)先級(jí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
C++的動(dòng)態(tài)內(nèi)存管理你真的了解嗎
這篇文章主要為大家詳細(xì)介紹了C++的動(dòng)態(tài)內(nèi)存管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
用while判斷輸入的數(shù)字是否回文數(shù)的簡單實(shí)現(xiàn)
這篇文章主要介紹了用while判斷輸入的數(shù)字是否回文數(shù)的簡單實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
詳解C語言中typedef和#define的用法與區(qū)別
這篇文章主要給大家介紹了關(guān)于C語言中typedef和#define的的用法、區(qū)別,以及陷阱。文中通過示例進(jìn)行了詳細(xì)講解,感興趣的小伙伴可以了解一下2022-07-07

