Last active
May 30, 2017 22:41
-
-
Save hardenchant/d075a23d829fba2d41643a373feffcf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// auto_wide_queue.cpp: auto expanding queue | |
#include <iostream> | |
using namespace std; | |
template <class T> | |
class queue //auto expanding queue | |
{ | |
class node //class which store array and pointer to next | |
{ | |
T* node_array; | |
int push_pos; | |
int pop_pos; | |
int array_length; | |
public: | |
node* next; | |
node(int array_length) { | |
node_array = new T[array_length]; | |
next = nullptr; | |
this->array_length = array_length; | |
push_pos = 0; | |
pop_pos = 0; | |
} | |
~node() { | |
delete[] node_array; | |
} | |
bool push(const T element) { | |
if (push_pos < array_length) { | |
node_array[push_pos] = element; | |
push_pos++; | |
return 1; | |
} | |
else { | |
return 0; //if node is full | |
} | |
} | |
bool is_empty() { | |
if (push_pos - pop_pos) { | |
return 0; | |
} | |
else { | |
return 1; | |
} | |
} | |
T pop() { | |
if (this->is_empty()) { | |
return NULL; | |
} | |
pop_pos++; | |
return node_array[pop_pos - 1]; | |
} | |
void print() { // class T output operator must be overloaded! (if not, comment thist function) | |
cout << "*****" << endl; | |
for (int i = pop_pos; i < push_pos; i++) { | |
cout << node_array[i] << endl; | |
} | |
} | |
}; | |
node* head; | |
node* tail; | |
int arr_length; | |
public: | |
queue(int arr_length) :head(nullptr), tail(nullptr) { | |
if (arr_length <= 0) { | |
cout << "Error: Arr_length <= 0" << endl; | |
return; | |
} | |
this->arr_length = arr_length; | |
}; | |
~queue() | |
{ | |
node *temp = head; | |
while (temp != nullptr) { | |
temp = head->next; | |
delete head; | |
head = temp; | |
} | |
} | |
void push(T element){ | |
if (head != nullptr) | |
{ | |
if (!tail->push(element)) //check space in node | |
{ | |
node* temp = new node(arr_length); | |
temp->push(element); | |
tail->next = temp; | |
tail = temp; | |
} | |
} | |
else { | |
node* temp = new node(arr_length); | |
temp->push(element); | |
head = tail = temp; | |
} | |
} | |
void print(){ | |
node* temp = head; //temporary pointer to head | |
while (temp != nullptr) //while exists pointer to next | |
{ | |
temp->print(); | |
temp = temp->next; | |
} | |
} | |
T pop(){ | |
if (head != nullptr) | |
{ | |
T obj = head->pop(); | |
if (head->is_empty()) { | |
if (head == tail) { | |
head = tail = nullptr; | |
} | |
else { | |
node* temp_node = head; | |
head = head->next; | |
delete temp_node; | |
} | |
} | |
return obj; | |
} | |
return NULL; | |
} | |
}; | |
int main() | |
{ | |
queue<int> q(4); | |
q.push(1); | |
q.push(2); | |
q.push(3); | |
q.push(4); | |
q.push(5); | |
q.push(6); | |
cout << "El:" << q.pop() << endl; | |
cout << "El:" << q.pop() << endl; | |
cout << "El:" << q.pop() << endl; | |
cout << "El:" << q.pop() << endl; | |
cout << "El:" << q.pop() << endl; | |
q.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment