Merging Two Linked Lists

Linked list

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 10793 Views - Last Post: 02 December 2009 - 02:06 PM Rate Topic: -----

#1 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Merging Two Linked Lists

Posted 01 December 2009 - 05:34 PM

I have written code for merging two doubly linked list together. For some reason, it is not compiling
#include<iostream>

using namespace std;

#define NULL 0

template <typename T>
class DoublyList;

template <typename T>
class Node
{
  friend class DoublyList<T>;
  public:
	Node()
	  {next = this; prev = this;}
	Node(const T& data, Node<T> *next = NULL, Node<T> *prev)
	{this ->data = data; this ->next = next;
	 this ->prev = prev;}
  private:
	T data;
	Node<T> *next;
	Node<T> *prev;
};

template <typename T>
class DoublyList
{
public:
  DoublyList();
  DoublyList(T someArray[], T arraySize);
  ~DoublyList();
  Node<T> *add(Node<T> *insert, const T& data);
  void mergeList(DoublyList<T>& listA);
  void print();
private:
  Node<T> *header;
  int size;  // number of data nodes in list
};

// Default constructor for creating empty linked list

template<typename T>
DoublyList<T>::DoublyList()
{
  header = new Node<T>();
  size = 0;
}


template<typename T>
DoublyList<T>::DoublyList(T someArray[], T arraySize)
{
  header = new Node<T>();
  size = arrSize;
  for (int i = 0; i < size; i++)
  {
	add(someArray[i], arraySize);
  }
}

// This is a destructor

template<typename T>
DoublyList<T>::~DoublyList()
{
  while (header->next != header)
  {
	Node* next = header ->next;
	header = next;
  }
  delete header;
}

template<typename T>
Node<T> *DoublyList<T>::add(Node<T> *insert, const T& data)
{
  Node<T> *prevPtr;
  Node<T> *newPtr;
  prevPtr = add ->prev;
  newPtr = new Node<T>(data, add, prevPtr);
  size++;
  add ->prev = prevPtr->next = newPtr;
  return newPtr;
}

// This function perform a sorted merge between the calling object list and the passed in listA
template<typename T>
void DoublyList<T>::mergeList(DoublyList<T> &listA)
{
  DoublyList<T> *result = NULL;	// build the answer here
  DoublyList<T> *current = *listA; //iterate over the original list
  DoublyList<T> *next;
  while(current != NULL)
  {
	next = current ->next;
	insert(&result, current);
	current = next;
  }
  *listA = result;
}

// This function print the values of each node in the list. If the list is empty it prints an error message

template<typename T>
void DoublyList<T>::print()
{
  if(header->next = header)
  {
	cout<<"list is empty now"<<endl;
  }
	I am not sure how can I print the value of each node here?
}



Here is the main function

int main()
{
  int array1[] = {2,5,7};
  int array2[] = {1,3,4,6};
  int array1Size = sizeof(array1)/sizeof(int);
  int array2Size = sizeof(array2)/sizeof(int);
  DoublyList<int> list1(array1, array1Size);
  DoublyList<int> list2(array2, arraySize);
  DoublyList<int> list3;
  list1.mergeList(list2);
  list3.print();
}



It should print :

1 2 3 4 5 6 7
list is empty now

Thanks for any help.


MOD EDIT: Edited title to be more descriptive

This post has been edited by KYA: 01 December 2009 - 05:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Merging Two Linked Lists

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Merging Two Linked Lists

Posted 01 December 2009 - 05:36 PM

What are the errors? Make it easier for us to help you.


No one is going to try your code without some inkling of what to look for.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Merging Two Linked Lists

Posted 01 December 2009 - 05:36 PM

A list of your compiler errors would be most helpful.

EDIT: Well there is this:
template<typename T>
void DoublyList<T>::print()
{
  if(header->next = header)
  {
    cout<<"list is empty now"<<endl;
  }
    I am not sure how can I print the value of each node here?
}


Which has a logical flaw. And I do say, if you wrote the rest of this code, implementing this should be pretty damn easy.

This post has been edited by JackOfAllTrades: 01 December 2009 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Re: Merging Two Linked Lists

Posted 01 December 2009 - 06:13 PM

View PostJackOfAllTrades, on 1 Dec, 2009 - 04:36 PM, said:

A list of your compiler errors would be most helpful.

EDIT: Well there is this:
template<typename T>
void DoublyList<T>::print()
{
  if(header->next = header)
  {
    cout<<"list is empty now"<<endl;
  }
    I am not sure how can I print the value of each node here?
}


Which has a logical flaw. And I do say, if you wrote the rest of this code, implementing this should be pretty damn easy.



template<typename T>
void DoublyList<T>::print()
{
  if(header->next == header)
  {
	cout<<"list is empty now"<<endl;
  }
   else
   {
	  I still can't think this part properly.
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Merging Two Linked Lists

Posted 01 December 2009 - 06:23 PM

Again, if you wrote the rest of that code, you should have zero, none, no problem at all figuring out how to traverse your list and print it. None whatsoever, piece of cake.
Was This Post Helpful? 0
  • +
  • -

#6 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Re: Merging Two Linked Lists

Posted 01 December 2009 - 06:35 PM

List of compile errors :

1>c:\users\documents\visual studio 2008\projects\doublylist.h(17) : error C2548: 'Node<T>::Node' : missing default parameter for parameter 3
1> with
1> [
1> T=int
1> ]
1> c:\users\documents\visual studio 2008\projects\doublylist.h(51) : see reference to class template instantiation 'Node<T>' being compiled
1> with
1> [
1> T=int
1> ]
1> c:\users\documents\visual studio 2008\projects\doublylist.h(50) : while compiling class template member function 'DoublyList<T>::DoublyList(T [],T)'
1> with
1> [
1> T=int
1> ]
1> c:\users\documents\visual studio 2008\projects\doublylinkedlistdriver.cpp(10) : see reference to class template instantiation 'DoublyList<T>' being compiled
1> with
1> [
1> T=int
1> ]
1>c:\users\documents\visual studio 2008\projects\doublylist.h(55) : error C2664: 'DoublyList<T>::insert' : cannot convert parameter 1 from 'int' to 'Node<T> *'
1> with
1> [
1> T=int
1> ]
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\documents\visual studio 2008\projects\doublylist.h(64) : error C2440: 'initializing' : cannot convert from 'Node<T> *' to 'Node *'
1> with
1> [
1> T=int
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> c:\users\documents\visual studio 2008\projects\doublylist.h(61) : while compiling class template member function 'DoublyList<T>::~DoublyList(void)'
1> with
1> [
1> T=int
1> ]
1>c:\users\documents\visual studio 2008\projects\doublylist.h(65) : error C2440: '=' : cannot convert from 'Node *' to 'Node<T> *'
1> with
1> [
1> T=int
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\documents\visual studio 2008\projects\doublylist.h(86) : error C2100: illegal indirection
1> c:\users\documents\visual studio 2008\projects\doublylist.h(84) : while compiling class template member function 'void DoublyList<T>::mergeList(DoublyList<T> &)'
1> with
1> [
1> T=int
1> ]
1>c:\users\documents\visual studio 2008\projects\doublylist.h(86) : error C2440: 'initializing' : cannot convert from 'DoublyList<T>' to 'DoublyList<T> *'
1> with
1> [
1> T=int
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\documents\visual studio 2008\projects\doublylist.h(90) : error C2039: 'next' : is not a member of 'DoublyList<T>'
1> with
1> [
1> T=int
1> ]
1>c:\users\documents\visual studio 2008\projects\doublylist.h(91) : error C2664: 'DoublyList<T>::insert' : cannot convert parameter 1 from 'DoublyList<T> **' to 'Node<T> *'
1> with
1> [
1> T=int
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\documents\visual studio 2008\projects\doublylist.h(94) : error C2100: illegal indirection
1>c:\users\documents\visual studio 2008\projects\doublylist.h(94) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'DoublyList<T> *' (or there is no acceptable conversion)
1> with
1> [
1> T=int
1> ]
1> c:\users\documents\visual studio 2008\projects\doublydlist.h(39): could be 'DoublyList<T> &DoublyList<T>::operator =(const DoublyList<T> &)'
1> with
1> [
1> T=int
1> ]
1> while trying to match the argument list '(DoublyList<T>, DoublyList<T> *)'
1> with
1> [
1> T=int
1> ]
Was This Post Helpful? 0
  • +
  • -

#7 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Re: Merging Two Linked Lists

Posted 01 December 2009 - 07:00 PM

View PostCisUgly, on 1 Dec, 2009 - 05:13 PM, said:

View PostJackOfAllTrades, on 1 Dec, 2009 - 04:36 PM, said:

A list of your compiler errors would be most helpful.

EDIT: Well there is this:
template<typename T>
void DoublyList<T>::print()
{
  if(header->next = header)
  {
    cout<<"list is empty now"<<endl;
  }
    I am not sure how can I print the value of each node here?
}


Which has a logical flaw. And I do say, if you wrote the rest of this code, implementing this should be pretty damn easy.



template<typename T>
void DoublyList<T>::print()
{
  if(header->next == header)
  {
	cout<<"list is empty now"<<endl;
  }
   else
   {
	  I still can't think this part properly.
	}
}


template<typename T>
void DoublyList<T>::print()
{
  if(header->next == header)
  {
	cout<<"list is empty now"<<endl;
  }
   else
   {
	  Node *next = header ->next;
		  header = next;
		  cout<<header;
	}
}



JackofAllTrades : I think this will do! phew!!
Was This Post Helpful? 0
  • +
  • -

#8 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Re: Merging Two Linked Lists

Posted 01 December 2009 - 07:23 PM

Can anyone help me out with my compile errors please?
Was This Post Helpful? 0
  • +
  • -

#9 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Re: Merging Two Linked Lists

Posted 01 December 2009 - 08:10 PM

Nobody?
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Merging Two Linked Lists

Posted 01 December 2009 - 08:58 PM

This error
error C2548: 'Node<T>::Node' : missing default parameter for parameter 3
refers to this code
Node(const T& data, Node<T> *next = NULL, Node<T> *prev)


As soon as you declare a default parameter in a method declaration, as you have here with your second parameter, next, every parameter that follows must also have a default value associated with it.

This error
error C2664: 'DoublyList<T>::insert' : cannot convert parameter 1 from 'int' to 'Node<T> *'
seems pretty self-explanatory to me, as does this
 error C2440: 'initializing' : cannot convert from 'Node<T> *' to 'Node *'


But I am not a generic programming/template expert, so I must defer to others on the exact fixes.
Was This Post Helpful? 0
  • +
  • -

#11 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Re: Merging Two Linked Lists

Posted 01 December 2009 - 10:49 PM

Thanks Jack for your input.

I know there are many expert here to point out my compile errors.
Please guys! I have been staring at the monitor for 7 straight hours now with no success. I understand the concept and I dont see any relevant errors in my program.
Was This Post Helpful? 0
  • +
  • -

#12 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Re: Merging Two Linked Lists

Posted 02 December 2009 - 12:29 AM

none?
Was This Post Helpful? 0
  • +
  • -

#13 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Re: Merging Two Linked Lists

Posted 02 December 2009 - 09:02 AM

none?wow
Was This Post Helpful? 0
  • +
  • -

#14 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Merging Two Linked Lists

Posted 02 December 2009 - 09:53 AM

First, its bad from to bump your topic. Second, its worse to repeatably do so.


You haven't provided an updated code listing so how are we supposed to know what you have and haven't fixed?
Was This Post Helpful? 0
  • +
  • -

#15 CisUgly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-December 09

Re: Merging Two Linked Lists

Posted 02 December 2009 - 10:03 AM

KYA,

Nobody has provided me the help so far. Like I said earlier, I am trying to fix it with no success.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2