用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)綏#琧++語言實(shí)現(xiàn),不足之處,還望指正!
// MyStack.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//自己構(gòu)造一個(gè)鏈?zhǔn)綏#哂衟ush(入棧),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)綏#哂衟ush(入棧),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)文章
深入理解c++實(shí)現(xiàn)Qt信號(hào)和槽機(jī)制
信號(hào)和槽機(jī)制是 Qt 的核心機(jī)制,可以讓編程人員將互不相關(guān)的對(duì)象綁定在一起,實(shí)現(xiàn)對(duì)象之間的通信,本文就來深入理解c++實(shí)現(xiàn)Qt信號(hào)和槽機(jī)制,感興趣的可以了解一下2023-08-08詳解C/C++ Linux出錯(cuò)處理函數(shù)(strerror與perror)的使用
我們知道,系統(tǒng)函數(shù)調(diào)用不能保證每次都成功,必須進(jìn)行出錯(cuò)處理,這樣一方面可以保證程序邏輯正常,另一方面可以迅速得到故障信息。本文主要為大家介紹兩個(gè)出錯(cuò)處理函數(shù)(strerror、perror)的使用,需要的可以參考一下2023-01-01C++11用兩個(gè)線程輪流打印整數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++11用兩個(gè)線程輪流打印整數(shù)的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09詳解C++ 共享數(shù)據(jù)保護(hù)機(jī)制
這篇文章主要介紹了詳解C++ 共享數(shù)據(jù)保護(hù)機(jī)制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c++,感興趣的朋友可以了解下2021-02-02