演算法與資料結構基礎5:C++棧的簡單實現,演算法與資料結構
堆棧是基於隊列的,只需要要稍微改一下就ok了,把代碼貼在下面
// Stack.h
#include <cstdio>#include <cassert>#include <iostream>using namespace std;class Stack{public:// **************************************************************************// 類的四大函數:建構函式、拷貝建構函式、重載賦值運算子、解構函式// **************************************************************************Stack();Stack(const Stack &Stack);Stack& operator=(const Stack &Stack);~Stack();// **************************************************************************// 增刪改查// **************************************************************************void Push(const int& data);int Pop();unsigned short Size();bool IsEmpty();void PrintStack();private:void Free();private:typedef struct Node{Node(int d):data(d),next(NULL){}int data;struct Node* next;}Node;Node* m_head;Node* m_tail;unsigned short m_size;};// **************************************************************************// 私人方法// **************************************************************************void Stack::Free(){if(m_head){Node* tmp = NULL;while(m_head){tmp = m_head;m_head = m_head->next;delete tmp;}}m_head = NULL;m_tail = NULL;m_size = 0;}// **************************************************************************// 類的四大函數:建構函式、拷貝建構函式、重載賦值運算子、解構函式// **************************************************************************Stack::Stack(){m_head = NULL;m_tail = NULL;m_size = 0;}Stack::Stack(const Stack &Stack){m_head = NULL;m_tail = NULL;m_size = Stack.m_size;if(Stack.m_head){Node* tmp = Stack.m_head;Node* q = NULL;while(tmp){Node* node = new Node(tmp->data);if(!m_head){m_head = node;q = m_head;}else{q->next = node;q = node;}tmp = tmp->next;}m_tail = q; }else{m_head = NULL;m_tail = NULL;}}Stack& Stack::operator=(const Stack &Stack){Free();m_size = Stack.m_size;if(Stack.m_head){Node* tmp = Stack.m_head;Node* q = NULL;while(tmp){Node* node = new Node(tmp->data);if(!m_head){m_head = node;q = m_head;}else{q->next = node;q = node;}tmp = tmp->next;}m_tail = q; }else{m_head = NULL;m_tail = NULL;}return *this;}Stack::~Stack(){if (m_head){Node* tmp = m_head;while (tmp){m_head = m_head->next;delete tmp;tmp = m_head;}}m_head = NULL;m_tail = NULL;}// **************************************************************************// 增刪改查// **************************************************************************void Stack::Push(const int& data){Node* tmp = new Node(data);if (!m_head) {m_head = tmp;m_tail = tmp;}else{tmp->next = m_head;m_head = tmp;}++m_size;}int Stack::Pop(){assert(m_size > 0);Node* tmp = m_head;m_head = m_head->next;int val = tmp->data;delete tmp;--m_size;return val;}unsigned short Stack::Size(){return m_size;}bool Stack::IsEmpty(){return (m_size == 0);}void Stack::PrintStack(){cout << "size : " << m_size << " , content : ";if (m_head){Node* tmp = m_head;while (tmp){cout << tmp->data << " -> ";tmp = tmp->next;}}cout << "NULL" << endl;}
// main.cpp
// test for Stack#include "Stack.h"#include <cstdlib>#include <iostream>#include <list>using namespace std;int main(){Stack stack;for (int i = 1; i < 5; ++i){stack.Push(i);}stack.PrintStack();Stack stackCopy(stack);stackCopy.PrintStack();Stack stackAssig;stackAssig = stack;stackAssig.PrintStack();int pop = stack.Pop();cout << "pop:" << pop << endl;stack.PrintStack();system("pause");return 0;}
// 輸出