Hello,
I've received an homework assignment to create an array via linked list.
All went well until the compilation part where i get continuous errors.
Seems most of the problem is that certain classes (DSA_array) doesn't recognize the existence of other classes and their member functions (DSA_list).
Here's my code , hope you can help:
DSA_array.h:
#include "DSA_list.h"
#include <new>
#include <iostream>
using namespace std;
DSA_list_node::DSA_list_node(){
next = NULL;
}
DSA_list::DSA_list(){
head = NULL;
}
int DSA_list::get_list_size(){
DSA_list_node *current = head;
int counter=0;
while (current->next != NULL) {
counter++;
current = current->next;
}
return counter;
}
DSA_list_node* DSA_list::find(DSA_list_node* x){
int i;
DSA_list_node *current = head;
DSA_list_node *answer = NULL;
for(i=0; i < this->get_list_size() ; i++) {
if (x->data==current->data) { answer = current;}
}
return answer;
}
DSA_list_node* DSA_list::find_index(int index) {
DSA_list_node *current = head;
while(current->next != NULL && current->index != index) {
current = current->next; }
return current;
}
bool DSA_list::insert(DSA_list_node* x) {
if (this->find(x) == NULL) {
DSA_list_node *current=head;
while(current->next != NULL) {
current = current->next; }
current->next = x;
return 1; }
else { return 0;}
}
DSA_array.cpp:
#include "DSA_list.h"
#include "DSA_array.h"
#include <new>
#include <iostream>
using namespace std;
DSA_array::DSA_array(int size){
DSA_list *newlist = new (std::nothrow) DSA_list();
int array_size = size;
}
DSA_array::~DSA_array(){
int i;
for (i=0; i < array_size; i++) {
if (*newlist.find_index(i) != NULL) {
delete *newlist.find_index(i);
}
}
int DSA_array::get_assigned_quantity() {
return *newlist.get_list_size();
}
bool DSA_array::update_array(int x, int y) {
if (x > array_size) { return 0; }
else {
if (*newlist.find_index(x) == NULL) {
DSA_list_node *newnode = new (std::nothrow) DSA_list_node();
*newnode.data = y;
*newnode.index = x;
*newlist.insert(*newnode); }
else { *newlist.find_index(x).index = y;
}
return 1;
}
void DSA_array::print_array(){
int i;
for (i=0; i < array_size; i++) {
if (*newlist.find_index(i) != NULL) {
cout << i << "," << *newlist.find_index(i).data << " "; }
}
}
DSA_list.h:
#ifndef DSA_LIST_H
#define DSA_LIST_H
using namespace std;
class DSA_list_node{
public:
int data;
int index;
DSA_list_node* next;
DSA_list_node();
~DSA_list_node();
};
class DSA_list {
public:
DSA_list_node *head;
DSA_list();
~DSA_list();
int get_list_size();
DSA_list_node* find(DSA_list_node *x);
bool insert(DSA_list_node *x);
DSA_list_node* find_index(int index);
};
#endif
DSA_list.cpp:
#include "DSA_list.h"
#include <new>
#include <iostream>
using namespace std;
DSA_list_node::DSA_list_node(){
next = NULL;
}
DSA_list::DSA_list(){
head = NULL;
}
int DSA_list::get_list_size(){
DSA_list_node *current = head;
int counter=0;
while (current->next != NULL) {
counter++;
current = current->next;
}
return counter;
}
DSA_list_node* DSA_list::find(DSA_list_node* x){
int i;
DSA_list_node *current = head;
DSA_list_node *answer = NULL;
for(i=0; i < this->get_list_size() ; i++) {
if (x->data==current->data) { answer = current;}
}
return answer;
}
DSA_list_node* DSA_list::find_index(int index) {
DSA_list_node *current = head;
while(current->next != NULL && current->index != index) {
current = current->next; }
return current;
}
bool DSA_list::insert(DSA_list_node* x) {
if (this->find(x) == NULL) {
DSA_list_node *current=head;
while(current->next != NULL) {
current = current->next; }
current->next = x;
return 1; }
else { return 0;}
}
Thank you !