?
Solved

linked list

Posted on 2003-03-19
6
Medium Priority
?
254 Views
Last Modified: 2013-12-14
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
Comment
Question by:wong83
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8172360
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
 
LVL 2

Accepted Solution

by:
Dariuzk earned 100 total points
ID: 8172495
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
 

Author Comment

by:wong83
ID: 8172520
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!

 

Author Comment

by:wong83
ID: 8172541
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8172552
- 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
 

Author Comment

by:wong83
ID: 8172572
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question