Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-31-2005, 06:56 AM   #1 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Linked List Constructor help...

Hello,

I'm trying to write a linked list in C++ and I'm having a bit of trouble. I understand the absolute basics of a list (front/back insertion, destructor) but I'm having a bit of trouble putting my ideas into code. Lets see if I can show what I have so far:

Code:
struct Node
{
    int data;
    Node* nextPtr;
};

class LinkedList
{
public:
    LinkedList() : nodePtr(0) {  }
    ~LinkedList();

private:
    Node* nodePtr;
};

LinkedList::~LinkedList()
{
    Node* tmpPtr;
    while(nodePtr->nextPtr != 0)
    {
        tmpPtr = nodePtr;
        nodePtr = nodePtr->next;
        delete nodePtr;
    }
}
2 questions:

1.) Does my constructor basically do the following?

Code:
Node * bla;
bla->data = 0;
bla->nextPtr = 0;
2.) Am I on basically the right track?

Please try not to "give it all away" so to speak, if necessary try to push me slightly in the right direction. This is my first time trying to write a List class from scratch and I feel like theres lots to learn.

Thanks.
fp_unit is offline   Reply With Quote
Old 01-31-2005, 08:34 AM   #2 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
In my opinion you are heading in the right general direction, but not quite hitting the target.

You should take a careful look at your destructor to see if it is really going to do what you think it will do. Think about what tmpPtr is pointing to what and what you are deleting, and how that might affect your while loop.

Your constructor is not quite right. Remember that a pointer does just that, it points. What you do to the pointer itself does nothing to what it points to (unless you explicitly tinker with what it points to with * or ->). Think about that and then think about what declaring a pointer does. Does delaring a pointer create what it points to?

You are doing good! Keep it up!

-Ted
__________________
while(1) fork();
ender is offline   Reply With Quote
Old 01-31-2005, 09:23 AM   #3 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Thanks, revised code:

Good eye. I made 2 updates (constructor, deletion of tempPtr in destructor) and to me it looks OK. I can't compile this yet or even spend more than 5 minutes working on it ATM because I'm also trying to do some C++ homework right now. I should be done in an hour or so, and if my constructor/destructor look good then I'm going to write the appendHead and appendTail member functions.

Code:
struct Node
{
    int data;
    Node* nextPtr;
};

class LinkedList
{
public:
    LinkedList() : nodePtr->nextPtr(0) {  }
    ~LinkedList();

private:
    Node* nodePtr;
};

LinkedList::~LinkedList()
{
    Node* tmpPtr;
    while(nodePtr->nextPtr != 0)
    {
        tmpPtr = nodePtr;
        nodePtr = nodePtr->nextPtr;
        delete tmpPtr;
    }
}
Does that look better? I basically made a typo in the destructor the first time, if my destructor's right then that's what I was trying to do all along... I gotta watch out for those typing errors. And can I use the arrow operator in the member initializer list of the constructor like I'm doing? I don't really think that any data should be filled in to the first node, I'm thinking right now that I will use that as a head node - a pointer to the first element in the List, instead of it being the first node in the List.

Thanks for your help.

Last edited by fp_unit; 01-31-2005 at 01:01 PM.
fp_unit is offline   Reply With Quote
Old 01-31-2005, 10:36 AM   #4 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
Yes, the destructor looks ok now.

As for your constructor, you will not be able to use an initialization list in this case. Remember, the pointer just points. You have not set what it will be pointing to yet. Therefore you can't set the values to what it points to either. I hope I'm not giving it away.

Think about why you delete something (tmpPtr). Now think of another keyword that might shed some light on this. And you are correct that it will just 'point' to the head of the list, however this is still a pointer!

Doing great!

-Ted
__________________
while(1) fork();
ender is offline   Reply With Quote
Old 01-31-2005, 10:46 AM   #5 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
OK I had a few errors still, I think I'm bug free now

I got the following code working, too bad I have to go to class now. (I forgot to allocate a new node... duh...)

Code:
#ifndef LIST_H_
#define LIST_H_

#include <iostream>
using namespace std;

struct Node
{
    int data;
    Node* nextPtr;
};

class LinkedList
{
public:
    LinkedList()
    {
        nodePtr = new Node;
        nodePtr->nextPtr = 0;
    }
    ~LinkedList();

private:
    Node* nodePtr;
};

LinkedList::~LinkedList()
{
    while(nodePtr->nextPtr != 0)
    {
        Node* tmpPtr = nodePtr;
        nodePtr = nodePtr->nextPtr;
        delete tmpPtr;
    }
}

#endif
and main
Code:
#include <iostream>
using namespace std;
#include "List.h"

int main()
{
    LinkedList test;
    return 0;
}
I'll probably keep having to modify it as I add features to the class, but thats the goal, is for me to learn lists. I have drawn a few out on paper which I recommend to people just learning, it really helps to visualize the list. Anyways hopefully I'll add some functionality to this class tonite, or maybe even after class today. We'll see. I'll keep you guys updated.
fp_unit is offline   Reply With Quote
Old 01-31-2005, 03:26 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Let's try to be as object oriented as we can be. The struct leaves us with access to its public members. Encapsulate the struct as well. And there is no need to initialize the the nodePointer or the nextPointer:
Code:
class LinkedList
{
public:
  LinkedList();    
  ~LinkedList();
private:
  struct Node
  {
    int data;
    Node* nextPtr;
  };
  Node* nodePtr;
};

//-----------------------------------------

LinkedList::LinkedList() : nodePtr(0)
{}

//------------------------------------------

LinkedList::~LinkedList()
{
    while(nodePtr->nextPtr != 0)
    {
        Node* tmpPtr = nodePtr;
        nodePtr = nodePtr->nextPtr;
        delete tmpPtr;
    }
}

//------------------------------------------}
__________________
Valmont is offline   Reply With Quote
Old 01-31-2005, 04:23 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Another (more professional) option is this:
Code:
#include <iostream>
#include <cstdlib>
#include <stdexcept>

using namespace std;

class Node
{
  public:
    Node() : next_(0)
    {}
    Node( int data, Node* nxt) : data_(data), next_(nxt)
    {}
    const int& get_data() const
    {
      return data_;
    }
    Node* next() const
    {
      return next_;
    }  
    private:
      friend class LinkedList;
      int data_;
      Node* next_;      
};

//--

class LinkedList
{
public:
  LinkedList();    
  ~LinkedList();
  void prepend(int const val);
  int const first() const;
  Node* get_head() const;
private:  
  Node* head_;
};

//-----------------------------------------

LinkedList::LinkedList() : head_(0)
{}

//------------------------------------------

LinkedList::~LinkedList()
{
  if(head_ != 0)
  {
    while(head_->next_ != 0)
    {
      Node* temp = head_;
      head_ = head_->next_;
      delete temp;
    }
    head_ = 0;
  }  
}

//------------------------------------------

void LinkedList::prepend(const int val)
{
  Node* const tmp = new Node(val, head_);
  head_ =  tmp;
}  

//------------------------------------------

const int LinkedList::first() const
{
  if(head_ == 0)
  {
    throw domain_error("ERROR: List is empty");
  }
  return head_->data_;    
}  

//------------------------------------------

Node* LinkedList::get_head() const
{
  return head_;
}  

//------------------------------------------

int main(int argc, char *argv[])
{
  LinkedList L;
  
  L.prepend(5);
  L.prepend(6);
  L.prepend(7);
  
  try
  {
    cout<<L.first()<<endl;
    for( Node* p = L.get_head(); p != 0; p=p->next() )
    {
      cout<<p->get_data()<<endl;
    }
  }
  catch(domain_error& dr)
  {
    cout<<dr.what()<<endl;
  }  

  return 0;
}
__________________

Last edited by Valmont; 01-31-2005 at 05:07 PM.
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with linked list Database toast28 Standard C, C++ 12 01-14-2005 01:03 PM
linked list problem if13121 Standard C, C++ 3 11-12-2004 09:58 AM
Linker errors on my linked list... Danish Standard C, C++ 1 03-02-2003 08:59 PM


All times are GMT -8. The time now is 08:39 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting