rename folder to data_structures

This commit is contained in:
Krishna Vedala
2020-06-04 16:15:10 -04:00
parent b8686d3071
commit 495bffcd72
32 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
CC= g++
CFLAGS = -c -Wall
all: test_queue
queue.o: queue.cpp
$(CC) $(CFLAGS) queue.cpp
test_queue: queue.o
$(CC) test_queue.cpp queue.o -o queue
clean:
rm *o queue

View File

@@ -0,0 +1,80 @@
#include "queue.h"
#include <assert.h>
#include <iostream>
using namespace std;
/* Default constructor*/
template <class Kind>
queue<Kind>::queue() {
queueFront = NULL;
queueRear = NULL;
size = 0;
}
/* Destructor */
template <class Kind>
queue<Kind>::~queue() {}
/* Display for testing */
template <class Kind>
void queue<Kind>::display() {
node<Kind> *current = queueFront;
cout << "Front --> ";
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
cout << "Size of queue: " << size << endl;
}
/* Determine whether the queue is empty */
template <class Kind>
bool queue<Kind>::isEmptyQueue() {
return (queueFront == NULL);
}
/* Clear queue */
template <class Kind>
void queue<Kind>::clear() {
queueFront = NULL;
}
/* Add new item to the queue */
template <class Kind>
void queue<Kind>::enQueue(Kind item) {
node<Kind> *newNode;
newNode = new node<Kind>;
newNode->data = item;
newNode->next = NULL;
if (queueFront == NULL) {
queueFront = newNode;
queueRear = newNode;
} else {
queueRear->next = newNode;
queueRear = queueRear->next;
}
size++;
}
/* Return the top element of the queue */
template <class Kind>
Kind queue<Kind>::front() {
assert(queueFront != NULL);
return queueFront->data;
}
/* Remove the element of the queue */
template <class Kind>
void queue<Kind>::deQueue() {
node<Kind> *temp;
if (!isEmptyQueue()) {
temp = queueFront;
queueFront = queueFront->next;
delete temp;
size--;
} else {
cout << "Queue is empty !" << endl;
}
}

View File

@@ -0,0 +1,31 @@
/* This class specifies the basic operation on a queue as a linked list */
#ifndef QUEUE_H
#define QUEUE_H
/* Definition of the node */
template <class Kind>
struct node {
Kind data;
node<Kind> *next;
};
/* Definition of the queue class */
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();
private:
node<Kind> *queueFront; /* Pointer to the front of the queue */
node<Kind> *queueRear; /* Pointer to the rear of the queue */
int size;
};
#endif

View File

@@ -0,0 +1,41 @@
#include <iostream>
#include <string>
#include "queue.cpp"
#include "queue.h"
using namespace std;
int main() {
queue<string> q;
cout << "---------------------- Test construct ----------------------"
<< endl;
q.display();
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;
q.enQueue("Hai");
q.enQueue("Jeff");
q.enQueue("Tom");
q.enQueue("Jkingston");
q.display();
cout << "---------------------- Test front ----------------------" << endl;
string value = q.front();
if (value == "Hai")
cout << "PASS" << endl;
else
cout << "FAIL" << endl;
cout << "---------------------- Test deQueue ----------------------"
<< endl;
q.display();
q.deQueue();
q.deQueue();
cout << "After Hai, Jeff left the queue: " << endl;
q.display();
return 0;
}