用C++實(shí)現(xiàn)一個(gè)鏈?zhǔn)綏5膶?shí)例代碼
更新時(shí)間:2013年05月29日 15:15:00 作者:
本篇文章是對(duì)使用C++實(shí)現(xiàn)一個(gè)鏈?zhǔn)綏5拇a進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
自定義一個(gè)鏈?zhǔn)綏?,c++語(yǔ)言實(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;
}
復(fù)制代碼 代碼如下:
// 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;
}
相關(guān)文章
Qt物聯(lián)網(wǎng)管理平臺(tái)之實(shí)現(xiàn)自動(dòng)清理早期數(shù)據(jù)功能
隨著時(shí)間的增加,存儲(chǔ)的歷史記錄也在不斷增加,如果設(shè)備數(shù)量很多,存儲(chǔ)間隔很短,不用多久,數(shù)據(jù)庫(kù)中的記錄就非常多,至少是百萬(wàn)級(jí)別起步,而且有些用戶(hù)還是需要存儲(chǔ)每一次的采集的數(shù)據(jù)。本文將利用Qt實(shí)現(xiàn)自動(dòng)清理早期數(shù)據(jù),需要的可以參考一下2022-07-07
C語(yǔ)言實(shí)現(xiàn)搶紅包程序代碼精簡(jiǎn)版
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)搶紅包程序代碼的精簡(jiǎn)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
c語(yǔ)言詳解動(dòng)態(tài)內(nèi)存分配及常見(jiàn)錯(cuò)誤的解決
給數(shù)組分配多大的內(nèi)存空間?你是否和初學(xué)C時(shí)的我一樣,有過(guò)這樣的疑問(wèn)。這一期就來(lái)聊一聊動(dòng)態(tài)內(nèi)存的分配,讀完這篇文章,你可能對(duì)內(nèi)存的分配有一個(gè)更好的理解2022-04-04
C++內(nèi)存池的簡(jiǎn)單實(shí)現(xiàn)
內(nèi)存池是一種動(dòng)態(tài)內(nèi)存分配與管理技術(shù)。本文主要介紹了C++內(nèi)存池的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
C++算法之在無(wú)序數(shù)組中選擇第k小個(gè)數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++算法之在無(wú)序數(shù)組中選擇第k小個(gè)數(shù)的實(shí)現(xiàn)方法,涉及C++數(shù)組的遍歷、判斷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Qt實(shí)現(xiàn)簡(jiǎn)易秒表設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)簡(jiǎn)易秒表設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C++詳解非類(lèi)型模板參數(shù)Nontype與Template及Parameters的使用
除了類(lèi)型可以作為模板參數(shù),普通值也可以作為模板函數(shù),即非類(lèi)型模板參數(shù)(Nontype Template Parameters)。下面讓我們一起了解一下2022-06-06

