• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

linked list

I'm trying to create an array of linked list .
which looks like this
arr[0] = 1 -> 2 -> NULL;
meaning in arr[0] i have "data 1" pointing to "data 2";
How do i insert into the array. my trouble is in understanding the insertion
of a linked list into the array.
Thank you
#include <iostream.h>

class Node {
  int dat;
  Node* next;
 public:
  Node(int );
  int disp() { return dat; }
};

Node *head, *tail;

Node::Node(int n)
{
  for(i=0;i<=n;i++)
   {if(head = NULL)
    {head = nlist;} //I'm assuming i have to get the nlist but i do not know how.
    else
    {tail->next = nlist;}
tail = nlist;
nlist->next = NULL;
cout << tail->data;
}
     
int main() {
  int n;
  cin >> n;
  Node** nlist;
  nlist = new Node*[n];
  for(int i(0); i < n; ++i) {
     nlist[i] = new Node(i);
       }
}
0
wong83
Asked:
wong83
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
Try:

Node :: Node ( int n )
{
  head = new Node ;
  head -> dat = n ;
  head -> next = NULL ;
  tail = head ;

} // end of constructor ()

It will make a single node and in your main () function, all the elements of nlist will be linked-lists having one node each.

This was just to help you how to get started.

Mayank.
0
 
DariuzkCommented:
I wrote an example for you,
I think you shuld use structure inside your
class as data member...
If this is not what you meant, then sorry for post...

#include <iostream>
#include <stdlib.h>

struct node{
    int  number;
    node *next;
};

class List {
    node *data;
public:
    List(int item);
    ~List();
    void add(int item);
    void printout();
private:
    List();//we don't won this to be
           //implemented by compiler
};

List::List(int item)
{
//add first node
   node *newnode = new node;
   newnode->number = item;
   newnode->next = NULL;
   data = newnode;
}

List::~List(){
    node *i;
    node *temp=this->data;
    for(i=this->data->next; i->next != NULL; i = i->next){
        delete temp;
        temp = i;
    }  
}

void List::add(int item){
    //get to the end of list  
    node *i;
    for(i=this->data; i->next != NULL; i = i->next);
    //insert new item
    node *newnode = new node;
    newnode->number = item;
    newnode->next = NULL;
    i->next = newnode;
}

void List::printout(){
    node *i=this->data;
    for(i=this->data; i != NULL; i=i->next)
        cout << i->number << endl;
}
   
int main(){
   
    List *list=new List(0);
    for(int i=1; i<=10; i++)
      list->add(i);
     
    list->printout();
   
    delete list;
   
    system("PAUSE");
    return 0;
}
0
 
wong83Author Commented:
Near the statement
head = new Node;
 no appropriate default constructor available is the error given . I have tried head->dat = n;
But i'm getting an error at run time.
Maybe something else is wrong i have to re-check a lot of things. Thank you .
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
wong83Author Commented:
Near the statement
head = new Node;
 no appropriate default constructor available is the error given . I have tried head->dat = n;
But i'm getting an error at run time.
Maybe something else is wrong i have to re-check a lot of things. Thank you .
0
 
n_fortynineCommented:
- you can supply your Node class with a default constructor
Node() : next(NULL) {}

- i'd rather make Node a struct, just to make it easier on the accessing from List.

I actually wrote a small program based on your original question about how to create and maintain a linked list...

#include <iostream.h>

struct Node {
   int dat;
   Node* next;
   Node() : next(NULL) {}
   Node(int n) : dat(n), next(NULL) {}
};

void insertInto(Node*& head, int value) {
   if(head) {
      Node* ptr = head;
      while(ptr->next)
         ptr = ptr->next;
      ptr->next = new Node(value);
      ptr = NULL;
   }
   else {
      head = new Node(value);
   }
}

void disp(Node* head) {
   for(Node* ptr = head; ptr; ptr = ptr->next)
      cout << ptr->dat << "\n";
}

int main() {
   int n = 5;
   Node** nlist;

   nlist = new Node*[n];
   for(int i(0); i < n; nlist[i++] = NULL);
   insertInto(nlist[3], 5);
   insertInto(nlist[3], 10);
   disp(nlist[3]);
}

This print out 5 \n 10 which is the 4th list in the array of lists. Hope this makes you understand it. Once you've done nlist = new Node*[n]; and set them to NULL (just for being safe) you can effectively using them as head pointers for your LLs. =) If you want to look more into how to get object-oriented then maybe can check out Dariuzk's List class.
0
 
wong83Author Commented:
Sorry for posting my doubts twice.
Dariuzk the problem as you have mentioned was with my data structure. Thank you very much for such detail. You really got me on the first step itself :-).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now