Simple implementation of 6:c++ queue based on algorithm and data structure

Source: Internet
Author: User

Queue is also based on the queue, understand the queue, the queue is very understood


Queue.h

#include <cstdio> #include <cassert> #include <iostream>using namespace Std;class queue{public://* * * Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/* * * * Queue (); Queue (const queue &queue); queue& operator= (const Queue &queue); ~queue ();//********************************************************* Delete and change//**************************************************************************int Pop (); void Push (const int& data), unsigned short Size (), bool IsEmpty (), void PrintQueue ();p rivate:void free ();p rivate: typedef struct NODE{NODE (int d):d ata (d), next (NULL) {}int data;struct node* next; Node; node* M_head; node* m_tail;/* This variable adds a bit of chicken, originally wanted to use to manipulate the end of the chain, found that only when the insertion can be used, delete the time is not used, because there is no previous pointer */unsigned short m_size;};/ /**************************************************************************//Private method//**************************** *****************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;} Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/ /**************************************************************************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;} Additions/deletions//*************************** void Queue::P ush (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::P op () {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::P rintqueue () {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;}

Output


Simple implementation of 6:c++ queue based on algorithm and data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.