2016-06-06 2 views
1

Ich habe kürzlich mit dynamischer Zuweisung und Warteschlange gespielt. Denken Sie daran, dass ich mit den Datenstrukturen ziemlich neu bin und mir selbst beigebracht habe, indem ich mir YouTube-Tutorials angesehen und einige Tutorials von Websites gelesen habe. Verzeihen Sie meine Ignoranz, wenn ich nicht alles über dieses Thema weiß. Während ich jedoch mit der Implementierung eines Zuweisungsoperators für meine Warteschlange herumspielte, habe ich irgendwo in seinem Code einen Fehler gemacht. Sie sehen, wenn ich versuche, und die Betreiber Zuordnung verwendenZuweisungsoperator, der alles mit Ausnahme des freien Speicherplatzes im Array kopiert

heißt

Queue names2; 
names2 = names; 

der Zuweisungsoperator kopiert alles bereits in der Warteschlange mit Ausnahme der leeren Elemente des Arrays. Implementiere ich meinen Kopierkonstruktor oder meinen Zuweisungsoperator falsch, was dazu führt, dass die leere nicht verwendete Kapazität nicht zur neu erstellten Warteschlange hinzugefügt wird?

main.cpp

#include "Queue.h"; 

int main() 
{ 
    Queue names; 
    names.enqueue("A"); 
    names.enqueue("B"); 
    names.enqueue("C"); 
    names.dequeue(); 
    names.enqueue("D"); 
    names.showFullQueue(); 
    Queue names2; 
    names2 = names; 
    names2.showFullQueue(); 
    names2.enqueue("Tom"); 
    names2.enqueue("Alice"); 
    names2.showFullQueue(); 

    return 0; 
} 

Queue.h

#ifndef Queue_H 
#define Queue_H 

#include <iostream> 
#include <cstring> 
#include <algorithm> 
#include <string> 

using namespace std; 

class Queue { 
public: 
    Queue(); //Defualt costructor 
    Queue(const Queue& source); //Copy constructor 
    ~Queue(); //Destructor 

    Queue& operator=(const Queue& source); //Assignament Operator //NOT WORKING 

    void enqueue(string value); // add item to queue 
    string dequeue(); // remove item from queue 

    void showFullQueue() const; 

    //memory allocation 
    void memory(int currentCapacity); 
private: 
    void shift(); 
    string * arr; 
    const static int front = 0; 
    int back; 
    int capacity; 
    int usedCapacity; 
}; 


#endif // !Queue_H 

Queue.cpp

#include "Queue.h" 

Queue::Queue() 
{ 
    back = -1; 
    capacity = 1; 
    usedCapacity = 0; 
    arr = new string[capacity]; 
} 

Queue::Queue(const Queue & source) 
{ 
    cout << "Invoking copy constructor" << endl; 
    this->back = source.back; 
    this->capacity = source.capacity; 
    this->usedCapacity = source.usedCapacity; 
    arr = new string[source.capacity]; 
    copy(source.arr, source.arr + usedCapacity, this->arr); 
} 

Queue::~Queue() 
{ 
    delete[] arr; 
} 

Queue & Queue::operator=(const Queue & source) 
{ 
    if (this == &source) 
     return *this; 
    cout << "Invoking Assignament operator" << endl; 
    Queue temp(source); 
    swap(temp.back, back); 
    swap(temp.capacity, capacity); 
    swap(temp.usedCapacity, capacity); 
    swap(temp.arr, arr); 

    return *this; 
} 

void Queue::enqueue(string value) 
{ 
    ++back; 
    arr[back] = value; 
    usedCapacity++; 
    memory(usedCapacity); 
} 

string Queue::dequeue() 
{ 
    string returnValue = arr[front]; 
    shift(); 
    usedCapacity--; 
    memory(usedCapacity); 
    return returnValue; 
} 

void Queue::showFullQueue() const 
{ 
    cout << "Front: " << front << endl; 
    cout << "Back: " << back << endl; 
    for (int i = 0; i < capacity; i++) 
    { 
     cout << arr[i] << ", "; 
    } 
    cout << endl; 
} 

void Queue::memory(int currentCapacity) 
{ 
    if (currentCapacity >= capacity) 
    { 
     int newCapacity = (currentCapacity * 3)/2 + 1; 
     string * arr2 = new string[newCapacity]; 
     copy(arr, arr + usedCapacity, arr2); 
     delete[] arr; 
     arr = arr2; 
     capacity = newCapacity; 
    } 
    else 
     cout << "allocation error"; 
} 

void Queue::shift() 
{ 
    for (int i = front; i <= back; i++) 
    { 
     arr[i] = arr[i + 1]; 
    } 
    --back; 
} 

Jede Hilfe wäre sehr sein appreci ated. Danke im Voraus.

+6

Bitte entfernen Sie alle und setzen nur die [MCVE] –

+4

Sie würden sich eine ganze Menge Stress gespeichert, wenn Sie 'std :: vector verwendet ' anstatt 'std :: string *' – WhiZTiM

+0

Ja Ich wollte nur mit dynamischen String-Arrays herumspielen. Ich werde beim nächsten Mal Vektoren verwenden, aber jetzt wollte ich nur wissen, wie man eine Warteschlange implementiert, ohne Vektoren zu verwenden. –

Antwort

1

Nun, ein paar Bugs hier zu beheben ..

  • Ihre Nutzung des Datenelements back und dem statischen Element front scheint mir überflüssig.

  • Ich sehe keine nützliche Verwendung von shift Funktion.

Auch in Ihrer Kopie Zuweisungsoperator ...

Queue & Queue::operator=(const Queue & source) 
{ 
    if (this == &source) 
     return *this; 
    cout << "Invoking Assignament operator" << endl; 
    Queue temp(source); 
    swap(temp.back, back); 
    swap(temp.capacity, capacity); 
    swap(temp.usedCapacity, capacity); //Notice the Bug here...? 
    swap(temp.arr, arr); 

    return *this; 
} 

Nun, hier ist eine aufgeräumt Version ... (beachten Sie ein paar Änderungen wie nicht using namespace std; in global Umfang einer Header-Datei und Verschieben #includes in die Implementierungsdatei ...

Queue.h

#ifndef Queue_H 
#define Queue_H 

#include <string> 

//using namespace std; ...typically a bad idea in header files 

class Queue { 
public: 
    Queue(); //Defualt costructor 
    Queue(const Queue& source); //Copy constructor 
    ~Queue(); //Destructor 

    Queue& operator=(const Queue& source); //Assignment Operator // WORKING :-) 

    void enqueue(std::string value); // add item to queue 
    string dequeue(); // remove item from queue 

    void showFullQueue() const; 

private: 
    std::string* arr; 
    int capacity; 
    int usedCapacity; 

    //memory allocation 
    void memory(int currentCapacity);  
}; 


#endif // !Queue_H 

Warteschlange.cav

#include "Queue.h" 
//You can put the remaining includes here 
#include <iostream> 
#include <algorithm> 

using namespace std; // You can us it in your cpp file... Not a bad idea.. :-) 

Queue::Queue() 
{ 
    capacity = 3; 
    usedCapacity = 0; 
    arr = new string[capacity](); 
} 

Queue::Queue(const Queue & source) 
{ 
    cout << "Invoking copy constructor" << endl; 
    capacity = source.capacity; 
    usedCapacity = source.usedCapacity; 
    arr = new string[source.capacity](); 
    copy(source.arr, source.arr + source.capacity, arr); 
} 

Queue::~Queue() 
{ 
    delete[] arr; 
} 

Queue & Queue::operator=(const Queue & source) 
{ 
    if (this == &source) 
     return *this; 
    cout << "Invoking Assignament operator" << endl; 

    using std::swap; //For ADL: ...redundant here... since you already have using namespace std; 
    Queue temp(source); 
    swap(temp.capacity, capacity); 
    swap(temp.usedCapacity, usedCapacity); 
    swap(temp.arr, arr); 

    return *this; 
} 

void Queue::enqueue(string value) 
{ 
    memory(usedCapacity); 
    arr[usedCapacity] = value; 
    ++usedCapacity; 
} 

string Queue::dequeue() 
{ 
    string returnValue = arr[--usedCapacity]; 
    //memory(usedCapacity); //needless 
    return returnValue; 
} 

void Queue::showFullQueue() const 
{ 

    //This styple prevents printing the last comma on the last item 
    int i = 0; 
    for (; i < usedCapacity-1; i++) 
    { 
     cout << arr[i] << ", "; 
    } 
    cout << arr[i] << endl; 
} 

void Queue::memory(int currentCapacity) 
{ 
    if (currentCapacity >= capacity) 
    { 
     int newCapacity = (currentCapacity * 3)/2 + 1; 
     string* arr2 = new string[newCapacity](); 
     copy(arr, arr + capacity, arr2); 
     delete[] arr; 
     arr = arr2; 
     capacity = newCapacity; 
    } 
} 

Und es gab keine Änderungen an Ihrem main.cpp ... :-) anzeigen Arbeiten here

Von der Ursache, gibt es ein paar weitere Verbesserungen können wir wie

machen
+0

Vielen Dank! Ich verstehe jetzt, was ich falsch gemacht habe. Ich werde sicherlich versuchen, meine Fähigkeiten in den Bereichen, auf die Sie hingewiesen haben, zu verbessern. Nochmals vielen Dank für Ihre enorme Hilfe. –

+0

@RichardSchwartz, du bist willkommen ... Es ist ein harter Job, aber wir alle lernen es ... Also, mehr Fett auf deinen Ellenbogen! – WhiZTiM

0

In Queue & Queue::operator=(const Queue & source) denke ich, dass Sie eine falsche Zuordnung machen.

swap(temp.capacity, capacity); 
swap(temp.usedCapacity, capacity); 

Ich nehme an, es sein sollte:

swap(temp.capacity, capacity); 
swap(temp.usedCapacity, usedCapacity); 
Verwandte Themen