I am trying to implement a minHeap of my own and i am getting an error related to my templated class
Here is the main.cpp:
#include <iostream>
#include"road.h"
#include"region.h"
#include"minHeap.h"
using namespace std;
int main()
{
// int numCities;
int numOldRoads;
cin >> numOldRoads;
minHeap<int> roadHeap(numOldRoads);
roadHeap.push(1);
roadHeap.push(4);
roadHeap.push(5);
roadHeap.push(2);
roadHeap.push(7);
roadHeap.push(6);
roadHeap.push(3);
roadHeap.push(9);
roadHeap.push(8);
int temp;
for(int i = 0; i<numOldRoads; i++){
temp = roadHeap.top();
roadHeap.pop();
cout << temp;
}
return 0;
}
Here is the header file:
#ifndef MIN_HEAP
#define MIN_HEAP
template<class T>
class minHeap{
public:
minHeap(int);
void push(T);
void pop();
T top();
void doubleHeapCap();
bool isEmpty();
private:
T *heap;
int heapSize;
int capacity;
};
#endif
Here is the implementation of the minHeap (also in the header because it was giving me errors otherwise):
#include"minHeap.h"
template<class T>
minHeap<T>::minHeap(int theCapacity = 10){
if(theCapacity < 1) throw "Capacity must be >=1.";
capacity = theCapacity;
heapSize = 0;
heap = new T[capacity + 1]; //heap [0] is not used
}
template<class T>
void minHeap<T>::push(const T& e){
//inserts e into min heap
if(heapSize == capacity){ //doubles capacity if Heap is too small
minHeap.doubleHeapCap;
capacity *=2;
}
int currentNode == ++heapSize;
while(currentNode != 1 && heap[currentNode/2] > e){
//bubble up node
heap[currentNode] = heap[currentNode/2]; //moves parent down
currentNode /= 2; //moves current node
}
heap[currentNode] = e;
}
template<class T>
void minHeap<T>::pop(){
//Deletes smallest element from heap and restructures heap
if(isEmpty()) throw "Heap is empty. Cannot delete.";
//deelt smallest element
heap[1].~T();
//remove last element from heap
T lastE = heap[heapSize--];
//trick down to restructure heap
int currentNode = 1; //root of heap
int child = 2; // first child of heap
while(child <= heapSize){
//set child to smaller child of currentNode
if(child < heapSize && heap[child] > heap[child+1]) child++;
//can we put lastE in currenNode?
if(lastE >= heap[child]) break; //yes we can
//no we can't, Obama
heap[currentNode] = heap[child]; //move child up
currentNode = child; child *= 2; // move a level down
}
//after you finally find one, place the node in the corrent position
heap[currentNode] = lastE;
}
template<class T>
bool minHeap<T>::isEmpty(){
//says whether or not hear is empty
if(heapSize == 0) return 1;
else return 0;
}
template<class T>
void minHeap<T>::doubleHeapCap(){
int currentcapacity = this->capacity;
int newCapacity = (this->capacity)*2;
minHeap *temp;
T *newHeap;
//create a new heap with twic the size
newHeap = new T[newCapacity + 1];
//copy elements over
for(int i=0; i<=capacity; i++){
newHeap[i] = this->heap[i];
}
//delete the old heap
temp = heap;
heap = newHeap;
newHeap = 0;
delete[] temp;
}
and here is the error:
In instantiation of 'void minHeap<T>::doubleHeapCap() [with T = int]':
required from 'void minHeap<T>::push(const T&) [with T = int]'
required from here
error: cannot convert 'int*' to 'minHeap<int>*' in assignment|
warning: unused variable 'currentcapacity' [-Wunused-variable]|
I pretty much copied the code from my data structures book and modified it (the book shows an implementation for a Max Heap and i am interested in a min heap).
As you can see all i am trying to do with the main right now is print out the ascending list of integers that i try to push to the min heap.
I think i dont fully understand how to implement template class functions...
In your class declaration you have this:
T *heap;
In doubleHeapCap you have this:
minHeap *temp;
// code...
temp = heap;
If T is int then you are trying to assign an int* (heap) to a minHeap<int>* (temp). Basically, make temp into a T*, too, and it should work. Or you could just dispense with temp altogether and write:
std::swap(heap, newHeap);
delete[] newHeap;
Related
I had to implement a Linked HashTable for a project. Now I have to come up with an excercise and a solution to it using my hashtable. Everything works just fine, except I get random Segfault errors.
By Random I mean: It is the same line of code that causes it, but always at different times, calls.
I tested my code in Atom, Codeblocks and in Visual Studio Code. Both Atom and CB threw SegFault error, but VS Code ran it just fine without a problem.
NOTE: THIS IS NOT THE FULL/REAL CODE. It's part of a header file that is included in the main.cpp file which is then compiled and ran.
The Code:
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
//List:
template<class T>
struct Node
{
string data;
Node *next;
};
class List
{
private:
Node *head, *tail;
int length;
friend class HashTable;
public:
List();
List(const List &L);
//~List() {delete this;};
List& operator =(List L);
int find(string);
void insert(string value);
void remove_head();
void remove_poz(int);
void remove_tail();
void clear();
void display();
};
List::List()
{
head = NULL;
tail = NULL;
length = 0;
}
template<>
string List<string>::findByIndex(int ind)
{
int i = 0;
Node<string>* temp = new Node<string>;
temp = head;
while (temp != NULL)
{
i++;
if (i == ind) return temp->data;
temp = temp->next;
}
delete temp;
return "-1";
}
template<class T>
void List<T>::remove_head()
{
Node<T>* temp = new Node<T>;
temp = head;
head = head->next;
delete temp;
length--;
}
template<class T>
void List<T>::remove_pos(int pos)
{
int i;
Node<T>* curr = new Node<T>;
Node<T>* prev = new Node<T>;
curr = head;
for (i = 1; i < pos; ++i)
{
prev = curr;
curr = curr->next;
}
if (curr)
{
prev->next = curr->next;
length--;
}
else cout << "Error" << endl;
}
template<class T>
void List<T>::remove_tail()
{
Node<T>* curr = new Node<T>;
Node<T>* prev = new Node<T>;
curr = head;
while (curr->next != NULL)
{
prev = curr;
curr = curr->next;
}
tail = prev;
prev->next = NULL;
delete curr;
length--;
}
//HashTable:
class HashTable
{
private:
List *table;
float load, stored;
int slots;
friend class List;
public:
HashTable();
HashTable(int);
~HashTable();
int hashFunc(string key);
int findTable(string);
int findList(string);
HashTable& operator =(const HashTable&);
void resize(); //I need this one
void insert(string);
void remove(string);
void clear(int);
void clear();
void display();
};
HashTable::HashTable()
{
stored = 0;
load = 0.00;
slots = 15;
table = new List[slots];
}
int HashTable::hashFunc(string key)
{
int g, h = 0;
unsigned int i;
for (i = 0; i < key.size(); ++i)
{
h = (h << 4) + (int)(key[i]);
g = h & 0xF0000000L;
if (g != 0)
{
h = h ^ (g >> 24);
}
h = h & ~g;
}
return h % slots;
}
template<class T>
void HashTable<T>::remove(T value)
{
int ind = hashFunc(value);
int findInd = table[ind].findByValue(value);
if (findInd == 0)
table[ind].remove_head();
else if (findInd < table[ind].length)
table[ind].remove_pos(findInd);
else table[ind].remove_tail();
if (table[ind].isEmpty()) occupied--;
stored--;
load = stored / slots;
}
The function that would cause the segfault:
(This would be called over and over again in a loop till I don't have more elements in my table)
string reakcio(HashTable<string>& HT, int tarolok)
{
const int anyagszam = rand() % 4 + 2; //Min 2, Max 5 anyag hasznalodik
int i = 0, j;
string anyagok[5];
string eredmeny;
for(j = 0; j < tarolok && i < anyagszam; ++j) //elemek kivetele
{
while(!HT.table[j].isEmpty())
{
anyagok[i++] = HT.table[j].findByIndex(1); //This line right here is the culprit :(
HT.remove(anyagok[i-1]);
}
}
const int siker = rand() % 4 + 0; //75% esely a sikerre
if (siker)
{
eredmeny = anyagok[0];
for(i = 1; i < anyagszam; ++i)
eredmeny += " + " + anyagok[i];
}
else
eredmeny = "Sikertelen reakcio";
return eredmeny;
}
(Note: only the functions that might be needed are shown here)
Every element of my hashtable, or of my lists is a 10 character long random string value.
srand(time(NULL)) is used before the function call in main.cpp
Any help or advice would be much appreciated, as I'm stuck at this and I really need to move on to the next portion of my exercise, but I can't without this.
The main.cpp file:
#include <iostream>
//#include "LinkedHash.h"
#include "functions.cpp"
int main()
{
HashTable<string> Anyagok;
int tarolok;
tarol(Anyagok); //Stores the data from file, no problem here, functions.cpp
tarolok = Anyagok.getSlots();
srand(time(NULL));
int i = 1;
while (Anyagok.getStored() > 5 )
cout<<reakcio(Anyagok, tarolok)<<" "<<i++<<endl;
return 0;
}
The LinkedHash.h contains the hashtable and the list, the functions.cpp contains the problematic function.
EDIT:
By suggestion I changed out the
Node<string>* temp = new Node<string>;
temp = head;
part to
Node<string>* temp = head;
Also removed the delete line.
But my problem is still the same :/
Everything works just fine, except I get random Segfault errors
Then nothing works at all.
A first review show little care to the cornercases in the list class. You need to define a correct behavior for
operation on empty lists
operation on first and last element
key not found during search
Notable errors found:
remove_head, remove_tail will segfault on empty list. head is NULL. head->next is invalid memory access. Similar errors are all over the implementation.
HashTable<T>::remove(T value) will always remove something. Even if the value argument is not in the hashtable. This is deeply flawed
findByIndex returning "-1" make no sense. "-1" is a valid input.
Node<T>* temp = new Node<T>;temp = head;. You just leaked memory. You need a pointer to manipulate node addresses. You should not instantiate Nodes to get a pointer. This is not an issue (ie not noticeable) for a small projet, but unacceptable for a real implementation.
My program is simple, make a tree (min heap), and through a inorder traversal to store data to an array, then destroy the tree (heap).
When I run to the store step, the program crash. I can't find why, even I know the bug position (I comment some code and run to see whether it works and finally I find the bug position)
I think maybe I delete a invalid pointer, but I can't find where; please help me to find it.
When I input
6
1 2 3 4 5 6
it works
when I input
8
90 80 30 40 10 45 20 50
then crash.
I don't know what different between this two group numbers
#include <iostream>
typedef int ElementType;
struct BTree
{
ElementType data;
BTree *Left;
BTree *Right;
};
class BuildingMinHeap
{
ElementType *Elements;
int Size;
int Capacity;
BTree *root;
ElementType *inorder_array; // to store members inorderly
public:
BuildingMinHeap(int MaxSize, ElementType MinData): Size(0), Capacity(MaxSize+1)
{
Elements = new ElementType[MaxSize+1];
Elements[0] = MinData; // sentinel
root = NULL;
inorder_array = new ElementType[Size];
}
~BuildingMinHeap()
{
if (Elements)
delete [] Elements;
if (inorder_array)
delete [] inorder_array;
if (root != NULL)
DeleteBTree(root);
}
void Insert(ElementType item);
void genBTree(ElementType *a, int position); // call CreateBTree()
void ModifyArray(ElementType *input, int size); // call gen_inorder_array()
ElementType *getElements() {return Elements;}
BTree *getRoot() {return root;}
void DeleteBTree(BTree *root);
private:
BTree *CreateBTree(ElementType *a, int position);
void gen_inorder_array(const BTree *node);
};
void BuildingMinHeap::Insert(ElementType item)
{
int i;
i = ++Size;
for (; Elements[i/2] > item; i /= 2)
{
// Elements[0] is sentinel
Elements[i] = Elements[i/2];
}
Elements[i] = item;
}
BTree *BuildingMinHeap::CreateBTree(ElementType *a, int position) // array to Binary Tree list
{
BTree *new_node = new BTree;
if (position > Size)
return NULL;
new_node->data = a[position];
new_node->Left = CreateBTree(a, 2*position);
new_node->Right = CreateBTree(a, 2*position+1);
return new_node;
}
void BuildingMinHeap::genBTree(ElementType *a, int position) // call CreateBTree()
{
root = CreateBTree(a, position);
}
void BuildingMinHeap::DeleteBTree(BTree *root)
{
if (root == NULL)
return;
DeleteBTree(root->Left);
DeleteBTree(root->Right);
delete root;
root = NULL;
return;
}
void BuildingMinHeap::gen_inorder_array(const BTree *node) // to generate members of tree root inorderly
{
static int index = 0;
// like print inorder tree
if (node)
{
gen_inorder_array(node->Left);
inorder_array[index++] = node->data;
// std::cout << node->data << " ";
gen_inorder_array(node->Right);
}
}
void BuildingMinHeap::ModifyArray(int *input, int size) // call gen_inorder_array()
{
gen_inorder_array(root); // generate inorder_array, when I comment this line, it work
// below commented code is nothing about tree root member
//
// // generate<elements of inorder_array,elements' address of Elements> map
// std::map<int, int*> inorder_Ele_map;
// for (int i = 0; i != Size; i++)
// {
// ElementType *it = std::find(Elements+1, Elements+Size, *(inorder_array+i));
// inorder_Ele_map[*(inorder_array+i)] = Elements + (it-Elements);
// }
//
//
// // change Elements array according input array
// for (int i = 0; i != size; i++)
// {
// if (*(inorder_array+i) != *(input+i))
// {
// *(inorder_Ele_map[*(inorder_array+i)]) = *(input+i);
// }
// }
}
int main(void)
{
int n;
std::cin >> n;
int *input = new int[n];
for (int i = 0; i != n; i++)
std::cin >> input[i];
BuildingMinHeap h(n, -999); // empty heap;
for (int i = 0; i != n; i++)
h.Insert(input[i]); // insert element to heap
h.genBTree(h.getElements(), 1); // generate Binary Tree(pointer) from Elements array, index 1 begin
// so far it work
h.ModifyArray(input, n); // if I comment this line, it would work
h.DeleteBTree(h.getRoot()); // I have already do call this function in destructor,
// but in destructor I call it when root != NULL,
// I have no idea when I comment this line, there are a crash.
delete [] input;
return 0;
}
I am trying to make a dynamic array in my member function, however, it seems to create a new dynamic array each time I call the function. Is there anyway to create a dynamic array inside a member function so it doesn't remake itself.
class predator
{
private:
string name;
string species;
protected:
string *list;
public:
predator(string theSpecies);
void killsRecorded(string kills); // add a new kill to the end of the predator's list of kills
string *killsList(); // return a pointer to the array of all kills by this predator
int noOfTotalKills(); // how many kills have been recorded
int k;
static int n;
};
//The header file
void predator::killsRecorded(string kills)
{
k = 0;
list = new string[5];
*(list + k) = kills;
k = n++;
cout<< k<< endl;
}
string* predator::killsList()
{
//cout<< (sizeof(list)/sizeof(list[0]))<< endl;
for(int i=0; i<5; i++)
{
cout<< *(list + i)<< endl;
}
}
Above is my class and header file, void killsRecorded(string kills) should add kills to my array, however, when I try that in my main.
predator *prey;
prey = new predator("Cheetah");
prey->killsRecorded("Mouse");
prey->KillsRecorded("Donkey");
prey->killsList();
It prints out
Created a hunter that is a Cheetah
0
1
Donkey
*BLANK LINE
*BLANK LINE
*BLANK LINE
*BLANK LINE
Instead, Mouse should be in the first line and Donkey in the second. Am I doing something wrong? Also, I can't use vectors, it's for an assignment.
In your constructor, assign n a default value, say 5. Then create an array of that size.
predator::predator()
: n(5),
k(0)
{
kills = new string[n];
}
Then recordKills checks to see if there is space in kills, reallocating if necessary:
recordKills(string kill)
{
if(k >= n) {
string* oldKills = kills;
kills = new string[2*n];
// copy
for(int i = 0; i< n: i++) {
kills[i] = oldKills[i];
}
n *= 2;
delete [] oldKills;
}
kills[k++] = kill;
}
It's generally a bad idea to call a variable by the name of a data structure, so I renamed 'list' to 'kills'.
Then when printing the kills, loop until k:
string* listKills()
{
for(int i = 0; i < k; i++) {
cout << kills[i] << endl;
}
return kills;
}
Remember to delete kills in the destructor!
Hmm, your killsRecorded(string kills) method is an example of how not to program...
you erase list losing all previously recorded kill
you lose the pointer obtained by a previous new[] which leads to a memory leak (how could you free them now your program has forgotten what had been allocated)
What should be done (what vector class does under the hood):
define a chunk of slots that you initially allocate
add the recorded strings to this simple array until it is full
when it is full allocate another array say of twice the size, carefully copy the values from the old array, release the old array and only them affect the new array to the saved pointer
do not forget to release the allocated array in class destructor
and store in the class the current size (number of kills) and the maximum size (allocated size)
Code could be:
class predator
{
private:
string name;
string species;
protected:
string *list;
size_t max_size;
size_t cur_size;
public:
predator(string theSpecies);
void killsRecorded(string kills); // add a new kill to the end of the predator's list of kills
string *killsList(); // return a pointer to the array of all kills by this predator
int noOfTotalKills(); // how many kills have been recorded
/*int k; what it that???
static int n;*/
};
//The implementation file
predator(string theSpecies): species(species) {
list = new string[5];
max_size = 5;
cur_size = 0;
// what do you do with name ?
}
void predator::killsRecorded(string kills)
{
if (cur_size >= max_size) { /* need a bigger array */
max_size *= 2;
temp = new string[max_size];
for(int i=0; i<cursize; i++) { // copy previous recorded values
temp[i] = list[i];
}
delete[] list; // free previous allocated array
list = temp; // ok list is now big enough
}
list[cur_size++] = kills;
}
You should use std::vector...
to do that you have to
#include <vector>
with the command
std::vector<string> kills;
you can create a new vector of strings
with the command
kills.pushback(stringvalue);
you can add a new string into your vector "list" also you don't have to count your kills... you can use
kills.size();
to get the number of strings back.
To get the values (strings) back you can use the vector like an array
string name = kills[3];
btw: you should save the vector as a member... to do that you have to save it in your class definition (header)
If you arn't allowed to use std::vector, you can write your own list...
class list
{
private:
node* head;
int size = 0;
struct node
{
node* next;
string value;
}
public:
list();
~list();
void PushBack(string);
string GetElement(int index);
int GetSize();
};
list::list()
{
head = new list();
head->next = nullptr;
}
list::~list()
{
node* temp = head;
node* temp2 = temp;
do //delete hole list
{
temp2 = temp->next;
delete temp;
temp = temp2;
}while(temp != nullptr);
}
void list::PushBack(string item)
{
node* temp = head;
while(temp->next != nullptr)
{
temp = temp->next;
}
//found the end of the list
node* newNode = new node();
newNode->value = item;
newNode->next = nullptr;
temp->next = newNode;
size++;
}
int list::GetSize()
{
return size;
}
string list::GetElement(int index)
{
node* temp = head;
while(temp->next != nullptr)
{
temp = temp->next;
if(index == 0)
{
return temp->value;
}
index--;
}
//index out of bounds
return "";
}
I can not check if the code is correct at the moment, because on this computer is no IDE... but I think it should word ;)
BTW: you can use this list instead of an array to do that you have to write:
list kills;
kills.PushBack("Peter");
kills.PushBack("Thomas");
kills.PushBack("Alex");
for(int i = 0; i< kills.GetSize();i++)
{
std::cout<<kills.GetElement(i)<<std::endl;
}
I am having following code. Following code is crashing when I am deleting the allocated memory i.e., Rational::deleteMemPool();
Getting output as
mempool value is 00000000 mempool value is 003462E8 mempool value is 003462E8
// Here for delete crashing though we have valid pointer.
Can any one please help me what is bug here?
#include <string>
#include <iostream>
template < class T >
class MemoryPool {
public:
MemoryPool (size_t size = EXPANSION_SIZE);
~MemoryPool ();
inline void* alloc (size_t size); // Allocate a T element from the free list.
inline void free (void *someElement); // Return a T element to the free list.
private:
MemoryPool<T> *next; // next element on the free list.
enum { EXPANSION_SIZE = 32}; // If the freeList is empty, expand it by this amount.
void expandTheFreeList(int howMany = EXPANSION_SIZE); // Add free elements to the free list
};
template < class T > MemoryPool < T > :: MemoryPool (size_t size) {
expandTheFreeList(size);
}
template < class T > MemoryPool < T > :: ~MemoryPool () {
MemoryPool<T> *nextPtr = next;
for (nextPtr = next; nextPtr != NULL; nextPtr = next) {
next = next->next;
delete [] nextPtr;
}
}
template < class T > inline void* MemoryPool < T > :: alloc (size_t) {
if (!next) {
expandTheFreeList();
}
MemoryPool<T> *head = next;
next = head->next;
return head;
}
template < class T > inline void MemoryPool < T > :: free (void *doomed) {
MemoryPool<T> *head = static_cast <MemoryPool<T> *> (doomed);
head->next = next;
next = head;
}
template < class T > void MemoryPool < T > :: expandTheFreeList(int howMany) {
// We must allocate an object large enough to contain the next pointer.
size_t size = (sizeof(T) > sizeof(MemoryPool<T> *)) ? sizeof(T) : sizeof(MemoryPool<T> *);
void *pNewAlloc = new char[size];
MemoryPool<T> *runner = static_cast <MemoryPool<T> *> (pNewAlloc);
next = runner;
for (int i = 0; i < howMany ; i++) {
void *pNewAlloc = new char[size];
runner->next = static_cast <MemoryPool<T> *> (pNewAlloc);
runner = runner->next;
}
runner->next = 0;
}
class Rational {
public:
Rational (int a = 0, int b = 1 ) : n(a), d(b) {}
void *operator new(size_t size) { return memPool->alloc(size); }
void operator delete(void *doomed,size_t size) { memPool->free(doomed); }
static void newMemPool() {
std::cout << "mempool value is " << Rational::memPool << std::endl;
memPool = new MemoryPool <Rational>;
std::cout << "mempool value is " << Rational::memPool << std::endl;
}
static void deleteMemPool() {
std::cout << "mempool value is " << Rational::memPool << std::endl;
delete memPool;
}
private:
int n; // Numerator
int d; // Denominator
static MemoryPool <Rational> *memPool;
};
MemoryPool <Rational> *Rational::memPool = 0;
int main() {
Rational *array[1000];
Rational::newMemPool();
// Start timing here
for (int j = 0; j < 1; j++) {
for (int i = 0; i < 10; i++) {
array[i] = new Rational(i);
}
for (int i = 0; i < 10; i++) {
delete array[i];
}
}
// Stop timing here
Rational::deleteMemPool();
}
Stacktrace:
Stack trace:>
ReadParsing.exe!Rational::deleteMemPool() Line 75 C++
ReadParsing.exe!main() Line 107 C++
ReadParsing.exe!__tmainCRTStartup() Line 586 + 0x19 bytes C
ReadParsing.exe!mainCRTStartup() Line 403 C
kernel32.dll!7c817077()
You have a problem with how you're deleting the blocks in your memory pool. When you allocate blocks, you use new char[size] in expandTheFreeList(i).
But when you delete those blocks, you use delete [] nextPtr:
template < class T > MemoryPool < T > :: ~MemoryPool () {
MemoryPool<T> *nextPtr = next;
for (nextPtr = next; nextPtr != NULL; nextPtr = next) {
next = next->next;
delete [] nextPtr; // <-- problem
}
}
And since nextPtr is a memoryPool<T>*, you're freeing the wrong thing (and it ends up recursing into this destrcutor to boot).
Changing that problem line to:
delete [] reinterpret_cast<char*>(nextPtr);
seems to make thing not crash. Of course, there may be other problems.
Hi everyone: Here i have created a queue from two stacks: You add to the one and remove from the other - when you want to remove the first stack dumps all its data into the second one, and it works perfectly - BUT
whenever i try to execute this loop without the bottom for loop or cin
the program receives a segmentation fault, i mean the most bottom for loop doesn't even execute but take it out and see what happens. Could this be some sort of buffer overflow
and Gcc needs time to manage the memory?
=====================================================================
struct Node
{
int DataMember;
Node* Next;
};
class Que
{
public:
Que();
~Que();
void Add(int);
void Pop();
int getSize();
void Purge();
private:
Node* Head;
bool StackOrQue; //True = Que False = Stack
int Size;
int Remove();
void Reverse();
};
void Que::Purge()
{
while(Head != NULL)
Pop();
if(StackOrQue)
StackOrQue = false;
}
int Que::getSize()
{
return Size;
}
Que::Que()
{
Head = NULL;
Size = 0;
StackOrQue = false;
}
Que::~Que()
{
Head = NULL;
}
void Que::Add(int q)
{
if(StackOrQue)
Reverse();
Size += 1;
Node* Temp = new Node;
Temp->DataMember = q;
Temp->Next = Head;
Head = Temp;
}
int Que::Remove()
{
int i = Head->DataMember;
Node* Temp = Head->Next;
delete Head;
Size -= 1;
Head = Temp;
return i;
}
void Que::Pop()
{
if(!StackOrQue)
Reverse();
cout << Remove();
}
void Que::Reverse()
{
Que TempStack;
int k = Size;
for(int i = 0; i < k; i++)
TempStack.Add(this->Remove());
delete this;
*this = TempStack;
if(!StackOrQue)
StackOrQue = true;
else
StackOrQue = false;
}
=====================================================================
Que q;
char a = NULL;
while(a != 'x')
{
q.Purge();
q.Add(1);
q.Add(2);
q.Add(3);
q.Add(4);
q.Add(5);
q.Add(6);
q.Add(7);
q.Add(8);
int size = q.getSize();
for(int i = 0; i < size; i++)
q.Pop();
//cin >> a;
for(int i = 0; i < 0; i++)
;
}
Thanks in-advance
delete this;
*this = TempStack;
There are some extreme corner cases in which delete this; actually does the right thing. This is not one of them. Specially since your Queue is placed in the stack, and you further try to delete it. If you intend to call the destructor instead do this->~Queue(), however after a manual destruction the only sensible thing to do next is a placement new. Assigning to *this is almost always a bad idea (if you bring inheritance into the picture, you have just caused a slice object to be created and more problems ahead the road). Also, your class should be implementing a copy constructor and an assignment operator, to correctly handle the resources allocated.