Using C + + to implement queue program code _c language

Source: Internet
Author: User
C + + Implementation queue, if there are deficiencies, also hope to correct
Copy Code code as follows:

MyQueue.cpp: Defines the entry point for a console application.
Implements a chained queue (queue), including a header node. Queue operations include team header (POP), Team tail (push),
Gets the team head element (front_element), gets the tail element (back_element), the number of queue elements (size),
Whether the queue is empty (empty).
#include "stdafx.h"
#include <iostream>
using namespace Std;
Defining the node structure of a queue
Template <class t>
struct NODE
{
Node<t>* Next;
T data;
};
Template <class t>
Class Myqueue
{
Public
Myqueue ()
{
node<t>* p = new node<t>;
if (NULL = p)
{
cout << "Failed to malloc the node." << Endl;
}
P->data = NULL;
P->next = NULL;
Front = p;
Rear = P;
}
Team up at the end of the line
void push (T e)
{
node<t>* p = new node<t>;
if (NULL = p)
{
cout << "Failed to malloc the node." << Endl;
}
P->data = e;
P->next = NULL;
Rear->next = p;
Rear = P;
}
In the team's head out of the team
T pop ()
{
T e;
if (front = = rear)
{
cout << "The queue is empty." << Endl;
return NULL;
}
Else
{
node<t>* p = front->next;
Front->next = p->next;
E = p->data;
Note that when there is only one element, and after it is deleted, the node to which rear points is deleted
It should be pointed at the head node.
if (rear = = p)
{
Rear = front;
}
Delete p; p = NULL;
return e;
}
}
Get the team head element
T front_element ()
{
if (front = = rear)
{
cout << "The queue is empty." << Endl;
return NULL;
}
Else
{
node<t>* p = front->next;
Return p->data;
}
}
T back_element ()
{
if (front = = rear)
{
cout << "The queue is empty." << Endl;
return NULL;
}
Else
{
Return rear->data;
}
}

To get the number of queue elements
int size ()
{
int count (0);
node<t>* p = Front;
while (P!= rear)
{
p = p->next;
count++;
}
return count;
}

To determine whether a queue is empty
BOOL Empty ()
{
if (front = = rear)
{
return true;
}
Else
{
return false;
}
}
Private
Node<t>* Front; A pointer to a header node. Front->next->data is the first element of the team's head.
node<t>* rear;//A pointer to the end of the team (the last element added)
};
int _tmain (int argc, _tchar* argv[])
{
Myqueue<int> Myqueue;
cout << myqueue.size () << Endl;
Myqueue.push (10);
Myqueue.push (20);
Myqueue.push (30);
cout << myqueue.front_element () << Endl;
cout << myqueue.back_element () << Endl;
Myqueue.pop ();
if (Myqueue.empty ())
{
cout << "The queue is empty now." << Endl;
}
Else
{
cout << "The queue has" << myqueue.size () << "Elements now" << Endl;
}
Myqueue.pop ();
Myqueue.pop ();
if (Myqueue.empty ())
{
cout << "The queue is empty now." << Endl;
}
Else
{
cout << "The queue has" << myqueue.size () << "Elements now" << Endl;
}
return 0;
}

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.