用C++實(shí)現(xiàn)一個(gè)鏈?zhǔn)綏5膶?shí)例代碼
自定義一個(gè)鏈?zhǔn)綏#琧++語言實(shí)現(xiàn),不足之處,還望指正!
// MyStack.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//自己構(gòu)造一個(gè)鏈?zhǔn)綏?,具有push(入棧),pop(出棧),top(取得棧頂元素),size(返回棧大?。?,empty(判斷是否為空)等功能
#include "stdafx.h"
#include <iostream>
using namespace std;
//構(gòu)造棧的節(jié)點(diǎn)
template <class T>
struct NODE
{
NODE<T>* next;
T data;
};
template <class T>
class MyStack
{
public:
MyStack()
{
phead = new NODE<T>;
if (phead == NULL)
{
cout << "Failed to malloc a new node. " << endl;
}
else
{
phead->data = NULL;
phead->next = NULL;
}
}
//入棧
void push(T e)
{
NODE<T>* p = new NODE<T>;
if (p == NULL)
{
cout << "Failed to malloc a new node. " << endl;
}
else
{
p->data = e;
p->next = phead->next;
phead->next = p;
}
}
//出棧
T pop()
{
T e;
NODE<T>* p = phead->next;
if(p != NULL)
{
phead->next = p->next;
e = p->data;
delete p;
return e;
}
else
{
cout << "There is no elements in the stack." << endl;
return NULL;
}
}
//取得棧頂元素
T top()
{
T e;
NODE<T>* p = phead->next;
if (p != NULL)
{
e = p->data;
return e;
}
else
{
cout << "There is no elements in the stack." << endl;
return NULL;
}
}
//取得棧中元素個(gè)數(shù)
int size()
{
int count(0);
NODE<T>* p = phead->next;
while (p != NULL)
{
p = p->next;
count++;
}
return count;
}
//判斷stack是否為空
bool empty()
{
NODE<T>* p = phead;
if (p->next == NULL)
{
return true;
}
else
{
return false;
}
}
private:
NODE<T>* phead;
};
int _tmain(int argc, _TCHAR* argv[])
{
MyStack<int> sta;
sta.push(1);
sta.push(2);
sta.push(3);
cout << "The size of the stack now is " << sta.size() << endl;
sta.pop();
cout << "The top element is " << sta.top() << endl;
cout << "The size of the stack now is" << sta.size() << endl;
if (sta.empty())
{
cout << "This stack is empty." << endl;
}
else
{
cout << "This stack is not empty." << endl;
}
return 0;
}
上一篇:探討:C++實(shí)現(xiàn)鏈?zhǔn)蕉鏄?用非遞歸方式先序,中序,后序遍歷二叉樹)
欄 目:C語言
下一篇:C++中靜態(tài)存儲(chǔ)區(qū)與棧以及堆的區(qū)別詳解
本文標(biāo)題:用C++實(shí)現(xiàn)一個(gè)鏈?zhǔn)綏5膶?shí)例代碼
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4404.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用分析詳解
- 01-10深入理解C++中常見的關(guān)鍵字含義


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