hi,
i created a linked list. it just adds values to the list called "source" and copies it to another list called "destination", it also deletes the values which is found in the source list and prints the source list again.

link_list.h

#ifndef LINK_LIST_H
#define LINK_LIST_H

class link_list
{

 private:

  class list
  {
  public:
    int data;
    list* next;
    
    list();
    list(int, list*);
    ~list();
  };
  
  list* top;
  
 public:
  
  link_list();
  ~link_list();

  void insert_data(int);
  void copy(const link_list* const);
  void print() const;
  int find(int);
};

#endif

link_list.cc

#include<iostream>
#include "link_list.h"

using namespace std;

link_list::link_list()
{
  top=0;
}

link_list::list::list()
{
  data=0;
  next=0;
}

link_list::list::list(int data_temp,list* next_temp=0)
{
  data=data_temp;
  next=next_temp;
}

void link_list::insert_data(int value)
{
  if(top==0)
    top=new list(value);
  else
    {
      list *temp=top;
      while(!temp->next==0)
	{
	  temp=temp->next;
	}
      temp->next=new list(value);
    }
}

void link_list::copy(const link_list* const source_temp)
{

  list* src_temp=source_temp->top;

  while(src_temp!=0)
    {
      if(this->top==0)
	{
	  this->top=new list(src_temp->data);
	}
      else
	{
	  list *dst_temp=this->top;
	  while(!dst_temp->next==0)
	    {
	      dst_temp=dst_temp->next;
	    }
	  dst_temp->next=new list(src_temp->data);

	}
      src_temp=src_temp->next;
    }
}

void link_list::print() const
{
  list* temp=this->top;
  while(temp!=0)
    {
      cout<<temp->data<<endl;
      temp=temp->next;
    }
}

int link_list::find(int value)
{
  list* temp=this->top;

  if(this->top==0)
    {
      cout<<"the list is empty"<<endl;
      return 0;
    }

  while(temp!=0 && temp->data!=value)
    {
      temp=temp->next;
    }

  if(temp==0)
    {
      cout<<"the value does not exist"<<endl;
      return 0;
    }

  cout<<"the value exists"<<endl;
  list* prev=this->top;
  if(prev==temp)
    {
      this->top=temp->next;
      temp->next=0;
      delete temp;
    }
  else
    {
      while(prev->next!=temp)
	{
	  prev=prev->next;
	}
      prev->next=temp->next;
      temp->next=0;
      
      delete temp;
    }
    
}

link_list::~link_list()
{
  delete top;
}

link_list::list::~list()
{
  delete next;
}

link_list_main.cc

#include<iostream>
#include "link_list.h"

using namespace std;

int main()
{

  link_list source;
  link_list dest;
  int data;

  cout<<"enter the data"<<endl;
  while(cin>>data)
    {
      source.insert_data(data);
    }
  if(cin.eof())
    {
      cout<<"eof"<<endl;
      cin.ignore(256,'\n');
      cin.clear();
    }

  cout<<"the source list"<<endl;
  source.print();

  cout<<"coying to destination list"<<endl;
  dest.copy(&source);

  cout<<"the destination list"<<endl;
  dest.print();

  cout<<"enter the values to be deleted from the source list"<<endl;
  while(cin>>data)
    {
      source.find(data);
    }
  cout<<"the source list"<<endl;
  source.print();

}

My code works fine. i just want to know if there is any improvements i can make in my code or simplify my code..
and is my code correct?

and i am very much worried about my destructors.. are they deleting the objects which are dynamically allocated..?? is there any way i can check it?

Recommended Answers

All 7 Replies

What do you think happens when you execute the following?

{
link_list x;
x.insert(42);
link_list y = x;
}

i think that would call the copy constructor of the link_list... hope i am correct..

i think that would call the copy constructor of the link_list... hope i am correct..

What copy constructor?

i am sorry i did not understand can you be still more clear about what you are talking or asking me about??

and as i told you i am concerned about my destructors.. are they working fine.. i meant have i used them correctly??

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

using namespace std;

#define elem_type int

class Node
{
public:
    elem_type data;
    Node* link;
};

class List
{
private:
    Node* head;
    int list_size;
public:
    List();
    void insert(elem_type, int);
    void insertFirst(elem_type);
    void insertLast(elem_type);
    Node* getNode(int);
    elem_type remove(int);
    int empty();
    int size();
    void display();
};

List::List()
{
    head = new Node();
    head->link = NULL;
    list_size = 0;
}

Node* List::getNode(int index)
{
    if (index > list_size) return NULL;
    Node* temp = head;
    int loc = 0;
    while (head->link != NULL && loc < index) {
        temp = temp->link;
        loc++;
    }
    return temp;
}

int List::empty()
{
    return head->link == NULL;
}

int List::size()
{
    return list_size;
}

void List::insert(elem_type item, int loc)
{
    // case 1: list is empty
    if (empty()) {
        Node* newnode = new Node();
        newnode->data = item;
        newnode->link = NULL;
        head->link = newnode;
    }
    else 
    // case 2: list is not empty
    {
        Node* anode = getNode(loc);
        if (anode == NULL) return;
        Node* newnode = new Node();
        newnode->data = item;
        newnode->link = anode->link;
        anode->link = newnode;
    }
    list_size++;
}

void List::insertFirst(elem_type item)
{
    insert(item, 0);
}

void List::insertLast(elem_type item)
{
    insert(item, list_size - 1);
}

elem_type List::remove(int index)
{
    if (index > list_size) return 0;
    Node* anode = getNode(index);
    Node* temp = anode->link;
    anode->link = anode->link->link;
    elem_type e = temp->data;
    delete temp;
    return e;
}


void List::display()
{
    Node* temp = head->link;
    while (temp != NULL) {
        cout << temp->data << endl;
        temp = temp->link;
    }
}

void main()
{
    List list;

    list.insert(10, 0);
    list.insert(20, 0);
    list.insert(30, 0);

    cout << list.remove(0) << endl;

    list.insertFirst(40);
    list.insertLast(50);

    cout << "LIST ITEMS\n";

    list.display();
}

i am sorry i did not understand can you be still more clear about what you are talking or asking me about??

It's really simple. I asked you what would happen if you try to copy an object of your link_list class, and you said that it would call the copy constructor.

As far as I can tell,l you have not defined a copy constructor for link_list, so I asked you where the copy constructor is that you think it will call.

Do you mean that it will call the copy constructor that the compiler defines for you? If so, what do you think the effect of that compiler-defined copy constructor will be?

Or do you think there is a copy constructor that I have failed to see?

Hint: A member function named copy is not a copy constructor.

yes it would call the copy constructor of link_list.. since i did not define my own link_list copy constructor, the compiler would call the default copy constructor for link_list.. and that would produce a shallow copy for "y".
if i make a shallow copy for "y" that would lead to stray pointers if i delete X first because Y object has dynamically allocated objects..

so i have made the following changes in my program to make a deep copy for my "dest" object..

link_list.h

#ifndef LINK_LIST_H
#define LINK_LIST_H

class link_list
{

 private:

  class list
  {
  public:
    int data;
    list* next;
    
    list();
    list(int, list*);
    ~list();
  };
  
  list* top;
  
 public:
  
  link_list();
  ~link_list();
  link_list(const link_list &);

  void insert_data(int);
  void copy(const link_list* const);
  void print() const;
  int find(int);
};

#endif

link_list.cc

#include<iostream>
#include "link_list.h"

using namespace std;

link_list::link_list()
{
  top=0;
  cout<<"constructor called"<<endl;
}

link_list::list::list()
{
  data=0;
  next=0;
}

link_list::list::list(int data_temp,list* next_temp=0)
{
  data=data_temp;
  next=next_temp;
}

link_list::link_list(const link_list& rhs)
{
  cout<<"copy constructor called"<<endl;
  this->top=0;
  copy(&rhs);
}

void link_list::insert_data(int value)
{
  if(top==0)
    top=new list(value);
  else
    {
      list *temp=top;
      while(!temp->next==0)
	{
	  temp=temp->next;
	}
      temp->next=new list(value);
    }
}

void link_list::copy(const link_list* const source_temp)
{
  
  list* src_temp=source_temp->top;

  while(src_temp!=0)
    {
      if(this->top==0)
	{
	  cout<<"hi"<<endl;
	  this->top=new list(src_temp->data);
	}
      else
	{
	  list *dst_temp=this->top;
	  while(dst_temp->next!=0)
	    {
	      dst_temp=dst_temp->next;
	    }
	  dst_temp->next=new list(src_temp->data);

	}
      src_temp=src_temp->next;
    }
}

void link_list::print() const
{
  list* temp=this->top;
  while(temp!=0)
    {
      cout<<temp->data<<endl;
      temp=temp->next;
    }
}

int link_list::find(int value)
{
  list* temp=this->top;

  if(this->top==0)
    {
      cout<<"the list is empty"<<endl;
      return 0;
    }

  while(temp!=0 && temp->data!=value)
    {
      temp=temp->next;
    }

  if(temp==0)
    {
      cout<<"the value does not exist"<<endl;
      return 0;
    }

  cout<<"the value exists"<<endl;
  list* prev=this->top;
  if(prev==temp)
    {
      this->top=temp->next;
      temp->next=0;
      delete temp;
    }
  else
    {
      while(prev->next!=temp)
	{
	  prev=prev->next;
	}
      prev->next=temp->next;
      temp->next=0;
      
      delete temp;
    }
    
}

link_list::~link_list()
{
  cout<<"main destructor"<<endl;
  delete top;
}

link_list::list::~list()
{
  cout<<"destructor"<<endl;
  cout<<data<<endl;
  delete next;
}

link_list_main.cc

#include<iostream>
#include "link_list.h"

using namespace std;

int main()
{

  link_list source;
  //  link_list dest;
  int data;

  cout<<"enter the data"<<endl;
  while(cin>>data)
    {
      source.insert_data(data);
    }
  if(cin.eof())
    {
      cout<<"eof"<<endl;
      cin.ignore(256,'\n');
      cin.clear();
    }

  cout<<"the source list"<<endl;
  source.print();

  cout<<"coying to destination list"<<endl;
  //  dest.copy(&source);
  link_list dest(source);
 

  cout<<"the destination list"<<endl;
  dest.print();

  cout<<"enter the values to be deleted from the source list"<<endl;
  while(cin>>data)
    {
      source.find(data);
    }
  cout<<"the source list"<<endl;
  source.print();

}

i hope i am correct.. if not pls correct my explanation...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.