演算法與資料結構基礎6:C++隊列的簡單實現,演算法與資料結構隊列

來源:互聯網
上載者:User

演算法與資料結構基礎6:C++隊列的簡單實現,演算法與資料結構隊列

隊列也是基於隊列,弄懂了隊列,隊列就很好懂了


// Queue.h

#include <cstdio>#include <cassert>#include <iostream>using namespace std;class Queue{public:// **************************************************************************// 類的四大函數:建構函式、拷貝建構函式、重載賦值運算子、解構函式// **************************************************************************Queue();Queue(const Queue &Queue);Queue& operator=(const Queue &Queue);~Queue();// **************************************************************************// 增刪改查// **************************************************************************int Pop();void Push(const int& data);unsigned short Size();bool IsEmpty();void PrintQueue();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 Queue::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;}// **************************************************************************// 類的四大函數:建構函式、拷貝建構函式、重載賦值運算子、解構函式// **************************************************************************Queue::Queue(){m_head = NULL;m_tail = NULL;m_size = 0;}Queue::Queue(const Queue &Queue){m_head = NULL;m_tail = NULL;m_size = Queue.m_size;if(Queue.m_head){Node* tmp = Queue.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;}}Queue& Queue::operator=(const Queue &Queue){Free();m_size = Queue.m_size;if(Queue.m_head){Node* tmp = Queue.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;}Queue::~Queue(){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 Queue::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 Queue::Pop(){assert(m_size > 0);int val = 0;Node *cur, *pri;if (!m_head->next){val = m_head->data;delete m_head;m_head = NULL;m_tail = NULL;m_size = 0;}else{cur = m_head;while (cur->next->next){cur = cur->next;}pri = cur;m_tail = cur;cur = cur->next;pri->next = NULL;val = cur->data;delete cur;cur = NULL;--m_size;}return val;}unsigned short Queue::Size(){return m_size;}bool Queue::IsEmpty(){return (m_size == 0);}void Queue::PrintQueue(){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 Queue#include "Queue.h"#include <cstdlib>#include <iostream>using namespace std;int main(){Queue queue;for (int i = 1; i < 5; ++i){queue.Push(i);}queue.PrintQueue();Queue queueCopy(queue);queueCopy.PrintQueue();Queue queueAssig;queueAssig = queue;queueAssig.PrintQueue();int pop = queue.Pop();cout << "pop:" << pop << endl;queue.PrintQueue();system("pause");return 0;}

// 輸出


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.