所以我的LinkedList
代码的put运算符出现了这个问题。该ADT在一个具有相同值的ADT中被假定为动态数组和链接列表。但是,当我在主对象上运行重载的put运算符时,它会打印出动态数组,但不会打印出链接列表。操作员位于底部。
我的ADT代码如下:
#include "MyArray.h"
#include <iostream>
MyArray::MyArray():size_array(0),size_LinkedList(0),head(NULL),capacity(200){
try{
array = new int[capacity];
} catch(bad_alloc &b) {
cout << "Constructor Exception: " << b.what();
}
}
MyArray::~MyArray(){
size_array = 0;
size_LinkedList = 0;
if(array != NULL){
delete [] array;
}
if(head != NULL){
Node *current = head;
while(head != NULL){
head = current->next;
delete current;
current = head;
}
}
}
MyArray::MyArray(const MyArray& copy) {
size_array = copy.size_array;
capacity = copy.capacity;
if (capacity > 0) {
array = new int[capacity];
for(int i = 0; i < size_array; i++){
array[i] = copy.array[i];
}
} else {
this->array = NULL;
}
size_LinkedList = copy.size_LinkedList;
if(copy.head == NULL) {
head = NULL;
} else {
head = new Node;
head->value = copy.head->value;
Node *newPtr = head;
for(Node *origPtr = copy.head->next; origPtr != NULL; origPtr = origPtr->next) {
newPtr->next = new Node;
newPtr->value = origPtr->value;
newPtr->next = origPtr->next;
}
newPtr->next = NULL;
}
}
MyArray MyArray::operator = (const MyArray& rhs) {
if (rhs.capacity ==0){
if(array != NULL){
delete [] array;
array = NULL;
}
size_array=0;
} else if (capacity == rhs.capacity) {
size_array = rhs.size_array;
for (int i = 0; i < size_array; i++) {
array[i] = rhs.array[i];
}
} else {
if (array != NULL){
delete [] array;
}
capacity = rhs.capacity;
size_array = rhs.size_array;
array = new int[capacity];
for (int i = 0; i < size_array; i++) {
array[i] = rhs.array[i];
}
}
if(rhs.head == NULL){
if(head != NULL) {
Node *current = head;
while(head != NULL){
head = current->next;
delete current;
current = head;
}
}
head = NULL;
size_LinkedList = 0;
} else{
if(head != NULL) {
Node *current = head;
while(head != NULL){
head = current->next;
delete current;
current = head;
}
}
head = new Node;
head->value = rhs.head->value;
Node *newPtr = head;
for(Node *origPtr = rhs.head->next; origPtr != NULL; origPtr = origPtr->next) {
newPtr->next = new Node;
newPtr->value = origPtr->value;
newPtr->next = origPtr->next;
}
newPtr->next = NULL;
head = newPtr;
}
return *this;
}
void MyArray::push_back(int val){
if(head == NULL) {
head = new Node;
head->value = val;
head->next=NULL;
size_LinkedList++;
if(size_array >= capacity) {
capacity = capacity + 1;
int *tmp = new int [capacity];
for (int i=0; i<size_array; i++)
tmp[i] = array[i];
delete [] array;
size_array++;
array = tmp;
int index = size_array-1;
array[index] = val;
} else {
size_array++;
int index = size_array-1;
array[index] = val;
}
} else {
Node *newNode = new Node;
newNode->value = val;
newNode->next = NULL;
Node *temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
size_LinkedList++;
if(size_array >= capacity) {
capacity = capacity + 1;
int *tmp = new int [capacity];
for (int i=0; i<size_array; i++)
tmp[i] = array[i];
delete [] array;
size_array++;
array = tmp;
int index = size_array-1;
array[index] = val;
} else {
size_array++;
int index = size_array-1;
array[index] = val;
}
}
}
void MyArray::pop_back() {
if(head != NULL) {
if(head->next == NULL) {
head = NULL;
} else {
Node *pop = head;
while(pop->next != NULL) {
pop = pop->next;
}
delete pop;
}
int *popArr = new int[capacity-1];
for(int i = 0; i < capacity-1; i++){
popArr[i] = array[i];
}
delete [] array;
array = popArr;
capacity--;
size_array--;
size_LinkedList--;
}
}
ostream& operator << (ostream &out, MyArray a){
MyArray::Node *print = new MyArray::Node;
print = a.head;
out << "Linked List: ";
while(print != NULL) {
int val = print->value;
out << val << ", ";
print = print->next;
}
out << endl << "Dynamic Array: ";
for(int i = 0; i < a.capacity; i++){
if(i != (a.capacity -1)){
out << a.array[i] << ", ";
} else {
out << a.array[i];
}
}
return out;
}
然后这是我的.h文件:
#ifndef MyArray_h
#define MyArray_h
#include <iostream>
using namespace std;
class MyArray {
private:
int *array; // dynamic array
int size_array; // number of elements stored in the dynamic array
int capacity; // the current number of integer spaces allocated to the dynamic array
struct Node {
int value;
Node *next;
};
Node *head; //point to the first node on the linked list
int size_LinkedList; //number of nodes on the linked list
public:
MyArray();
~MyArray();
MyArray(const MyArray& copy);
MyArray operator = (const MyArray& rhs);
void push_back(int val);
void pop_back();
friend ostream& operator << (ostream &out, MyArray a);
};
#endif /* MyArray_h */
然后这是我的main.cpp文件:
#include "MyArray.h"
#include <iostream>
using namespace std;
int main() {
MyArray t = MyArray();
for(int i = 0; i<205; i++){
t.push_back(i);
}
cout << t << endl;
return 0;
}
这是我运行时的输出:
链接列表:204,
动态阵列:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21, 22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46, 47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71, 72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96, 97,98,99,100,101,103,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, 122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146, 147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171, 172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196, 197,198,199,200,201,202,203,204
任何帮助都会有很大帮助!