Help with Linked List / Array
Nov 17, 2011 at 1:22pm UTC
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:
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
#include "DSA_list.h"
#include <new>
#include <iostream>
#ifndef DSA_ARRAY_H
#define DSA_ARRAY_H
using namespace std;
class DSA_array {
public :
int array_size;
DSA_list *newlist;
DSA_array(int size);
~DSA_array();
int get_assigned_quantity();
bool update_array(int x,int y);
void print_array();
};
#endif
DSA_array.cpp:
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
#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:
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
#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:
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
#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;}
}
Nov 17, 2011 at 1:34pm UTC
Posting the errors might help, rather than everyone having to go through every single line of your code.
Nov 17, 2011 at 1:48pm UTC
I apologize ..
Basically i get all kinds of errors but it seems all are connected to the fact that DSA_array.cpp would not recognize the existence of the list class.
for example if i try and compile DSA_array.cpp one of the errors i get is:
"DSA_array.cpp `find_index' has not been declared "
other reoccurring errors are:
"DSA_array.cpp request for member of non-aggregate type before '(' token"
"DSA_array.cpp expected primary-expression before "int" "
"DSA_array.cpp a function-definition is not allowed here before '{' token "
Topic archived. No new replies allowed.