用C++實現(xiàn)一個鏈?zhǔn)綏5膶嵗a
更新時間:2013年05月29日 15:15:00 作者:
本篇文章是對使用C++實現(xiàn)一個鏈?zhǔn)綏5拇a進行了詳細的分析介紹,需要的朋友參考下
自定義一個鏈?zhǔn)綏?,c++語言實現(xiàn),不足之處,還望指正!
// MyStack.cpp : 定義控制臺應(yīng)用程序的入口點。
//自己構(gòu)造一個鏈?zhǔn)綏?,具有push(入棧),pop(出棧),top(取得棧頂元素),size(返回棧大?。?,empty(判斷是否為空)等功能
#include "stdafx.h"
#include <iostream>
using namespace std;
//構(gòu)造棧的節(jié)點
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;
}
}
//取得棧中元素個數(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 : 定義控制臺應(yīng)用程序的入口點。
//自己構(gòu)造一個鏈?zhǔn)綏?,具有push(入棧),pop(出棧),top(取得棧頂元素),size(返回棧大?。?,empty(判斷是否為空)等功能
#include "stdafx.h"
#include <iostream>
using namespace std;
//構(gòu)造棧的節(jié)點
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;
}
}
//取得棧中元素個數(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)管理平臺之實現(xiàn)自動清理早期數(shù)據(jù)功能
隨著時間的增加,存儲的歷史記錄也在不斷增加,如果設(shè)備數(shù)量很多,存儲間隔很短,不用多久,數(shù)據(jù)庫中的記錄就非常多,至少是百萬級別起步,而且有些用戶還是需要存儲每一次的采集的數(shù)據(jù)。本文將利用Qt實現(xiàn)自動清理早期數(shù)據(jù),需要的可以參考一下2022-07-07
C++算法之在無序數(shù)組中選擇第k小個數(shù)的實現(xiàn)方法
這篇文章主要介紹了C++算法之在無序數(shù)組中選擇第k小個數(shù)的實現(xiàn)方法,涉及C++數(shù)組的遍歷、判斷、運算等相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
C++詳解非類型模板參數(shù)Nontype與Template及Parameters的使用
除了類型可以作為模板參數(shù),普通值也可以作為模板函數(shù),即非類型模板參數(shù)(Nontype Template Parameters)。下面讓我們一起了解一下2022-06-06

