Stanislav
Stanislav

Reputation: 1

Implementing an array of linked lists

Working with graphs right now, and pretty stuck on creating an array of linked lists (adjacency list), tried few variations, but got this error:

[Error] no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'node*')

class node {
    int data;
    node* next;
    node(int x){
        data = x;
        next = NULL;
    }   
};

void SS_in(){
    int nver;
    node **listik;
    cout << "vvedite kolvo vershin";
    cin >> nver;
    for (int i=0; i<nver;i++){
        cout << "V" << i << ": ";
        cin >> listik[i];
    }
}

Upvotes: 0

Views: 431

Answers (3)

4386427
4386427

Reputation: 44274

This code:

node **listik;
...
...
    cin >> listik[i];

is wrong in two ways.

1) listik is uninitialized and consequently so is listik[i]. In other words - you are trying to read into memory that your program (most likely) doesn't own.

2) Since listik is a "pointer to pointer to node", listik[i] is a "pointer to node". Asking a user to type in a pointer value is meaningless and - lucky you - you got a compile error telling you that.

Now for your real question:

... creating an array of linked lists ...

Remember that a linked list start with a head element which is a "pointer to node". So to get an array of linked lists, you need an array of "pointer to node".

For instance:

node* arr_of_lists[20];

will be an array with 20 "pointer to node". And therefore something you can use as an array of 20 linked list.

Or in a more c++ like style:

std::array<node*, 20> arr_of_lists;

Or with dynamic allocation so that you can control the array size at runtime:

node** arr_of_lists = new node*[desired_size];

When reading data from the user, you'll need two informations:

1) The data value for the node to be added

2) Which of the (e.g. 20) linked lists to add the node to.

Like:

cin >> data;
cin >> list_index;

// Skipping error checks here... but must be added to the real code

// You need a function like this to creat an new node holding
// data and insert the new node to the linked list at index "list_index"
list_insert(arr_of_lists, list_index, data);

Upvotes: 0

Val
Val

Reputation: 221

Only node and graph (listik) are defined.

On this stage the error could be fixed:

#include <iostream>
using namespace std;

class node {
public:
  int data;
  node* next;
  node (int x) {
    data = x;
    next = NULL;
  }
};

void SS_in () {
  int nver;
  std::cout << "vvedite kolvo vershin";
  std::cin >> nver;

  node** listik = new node * [nver];

  int tempData;
  for (int i = 0; i < nver; i++) {
    cout << "V" << i << ": ";
    cin >> tempData;
    listik[i] = new node(tempData);
  }
}

void main () {
  SS_in ();
}

Class adjacencyList (linked list) should be added.

Upvotes: 0

Landstalker
Landstalker

Reputation: 1368

I will intervene only on the compilation error that you have and not on your need to build a linked list because your request is not clear.
You must overload the operator >> for your class before you can use it:

#include <iostream> 
using namespace std; 

class node {
public :
    int data;
    node* next;

    node(int x){
        data = x;
        next = NULL;
    }   

    friend istream & operator >> (istream &in,  node &myNode); 
};

istream & operator >> (istream &in,  node &myNode) 
{ 
    cout << "Enter node data :"<< endl;
    in >> myNode.data; 

    return in; 
} 

int main()
{
    node myNode(2);
    cin >> myNode;

    return 0;
}

Upvotes: 1

Related Questions