mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-27 22:24:29 +08:00
formatting source-code for d7af6fdc8c
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
#include "queue.h"
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -25,11 +25,12 @@ void queue<Kind>::display()
|
||||
{
|
||||
node<Kind> *current = queueFront;
|
||||
cout << "Front --> ";
|
||||
while(current != NULL) {
|
||||
cout<<current->data<< " ";
|
||||
current = current -> next;
|
||||
while (current != NULL)
|
||||
{
|
||||
cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
cout <<endl;
|
||||
cout << endl;
|
||||
cout << "Size of queue: " << size << endl;
|
||||
}
|
||||
|
||||
@@ -55,10 +56,13 @@ void queue<Kind>::enQueue(Kind item)
|
||||
newNode = new node<Kind>;
|
||||
newNode->data = item;
|
||||
newNode->next = NULL;
|
||||
if (queueFront == NULL) {
|
||||
if (queueFront == NULL)
|
||||
{
|
||||
queueFront = newNode;
|
||||
queueRear = newNode;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
queueRear->next = newNode;
|
||||
queueRear = queueRear->next;
|
||||
}
|
||||
@@ -78,13 +82,15 @@ template <class Kind>
|
||||
void queue<Kind>::deQueue()
|
||||
{
|
||||
node<Kind> *temp;
|
||||
if(!isEmptyQueue()) {
|
||||
if (!isEmptyQueue())
|
||||
{
|
||||
temp = queueFront;
|
||||
queueFront = queueFront->next;
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Queue is empty !" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,21 +14,20 @@ struct node
|
||||
template <class Kind>
|
||||
class queue
|
||||
{
|
||||
public:
|
||||
void display(); /* Show queue */
|
||||
queue(); /* Default constructor*/
|
||||
~queue(); /* Destructor */
|
||||
bool isEmptyQueue(); /* Determine whether the queue is empty */
|
||||
void enQueue (Kind item); /* Add new item to the queue */
|
||||
Kind front(); /* Return the first element of the queue */
|
||||
void deQueue(); /* Remove the top element of the queue */
|
||||
void clear();
|
||||
public:
|
||||
void display(); /* Show queue */
|
||||
queue(); /* Default constructor*/
|
||||
~queue(); /* Destructor */
|
||||
bool isEmptyQueue(); /* Determine whether the queue is empty */
|
||||
void enQueue(Kind item); /* Add new item to the queue */
|
||||
Kind front(); /* Return the first element of the queue */
|
||||
void deQueue(); /* Remove the top element of the queue */
|
||||
void clear();
|
||||
|
||||
private:
|
||||
node<Kind> *queueFront; /* Pointer to the front of the queue */
|
||||
node<Kind> *queueRear; /* Pointer to the rear of the queue */
|
||||
int size;
|
||||
private:
|
||||
node<Kind> *queueFront; /* Pointer to the front of the queue */
|
||||
node<Kind> *queueRear; /* Pointer to the rear of the queue */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "queue.h"
|
||||
#include "queue.cpp"
|
||||
#include "queue.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
queue<string> q;
|
||||
cout << "---------------------- Test construct ----------------------" << endl;
|
||||
cout << "---------------------- Test construct ----------------------"
|
||||
<< endl;
|
||||
q.display();
|
||||
cout << "---------------------- Test isEmptyQueue ----------------------" << endl;
|
||||
if(q.isEmptyQueue())
|
||||
cout << "PASS" <<endl;
|
||||
cout << "---------------------- Test isEmptyQueue ----------------------"
|
||||
<< endl;
|
||||
if (q.isEmptyQueue())
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" <<endl;
|
||||
cout << "---------------------- Test enQueue ----------------------" << endl;
|
||||
cout << "After Hai, Jeff, Tom, Jkingston go into queue: "<<endl;
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test enQueue ----------------------"
|
||||
<< endl;
|
||||
cout << "After Hai, Jeff, Tom, Jkingston go into queue: " << endl;
|
||||
q.enQueue("Hai");
|
||||
q.enQueue("Jeff");
|
||||
q.enQueue("Tom");
|
||||
@@ -25,14 +28,15 @@ int main()
|
||||
cout << "---------------------- Test front ----------------------" << endl;
|
||||
string value = q.front();
|
||||
if (value == "Hai")
|
||||
cout << "PASS" <<endl;
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" <<endl;
|
||||
cout << "---------------------- Test deQueue ----------------------" << endl;
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test deQueue ----------------------"
|
||||
<< endl;
|
||||
q.display();
|
||||
q.deQueue();
|
||||
q.deQueue();
|
||||
cout << "After Hai, Jeff left the queue: "<< endl;
|
||||
cout << "After Hai, Jeff left the queue: " << endl;
|
||||
q.display();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user