4 Replies - 1035 Views - Last Post: 03 July 2011 - 06:39 AM Rate Topic: -----

#1 boyhailong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-January 11

how to write mergeList in a linked List C++ class writing by array

Posted 02 July 2011 - 08:52 PM

i just write a class below :
#include<iostream>
using namespace std;
const int DefaultSize = 100;
class List
{
public:
	List();
	List(size_t size);
	~List();
	bool insert(const int);
	void ClearList();
	bool IsEmpty();
	size_t ListLen();
	bool GetElem(size_t , int );
	void append(size_t,int);//在第i个位置插入数值
	void Display();
	void remove(size_t);
private:
	size_t MaxSize;
	size_t list_len;
	int *element;
};
List::List()
{
	MaxSize = DefaultSize;
	list_len = 0;
	element = new int[MaxSize];
}
List::List(size_t size)
{
	MaxSize = size;
	list_len = 0;
	element = new int[MaxSize];
}
List::~List()
{
	delete []element;
}
bool List::insert(const int a)
{
	if (list_len == MaxSize)
		return false;
	element[list_len++] = a;
	return true;
}
bool List::IsEmpty()
{
	return list_len == 0;
}
bool List::GetElem(size_t size, int e)
{
	if(size > list_len)
		return false;
	e = element[size];
	return true;
}
void List::Display()
{
	if(IsEmpty())
		cout << "List is empty" << endl;
	else
		for (size_t i = 0; i < list_len; ++i)
			cout << element[i] << " ";
}
size_t List::ListLen()
{
	return list_len;
}
void List::append(size_t i, int num)
{
	if(i < 0 || i > MaxSize)
	{
		cout << "append invalid" << endl;
	}
    if (i == list_len)
    {
        this->insert(num);
    } 
    else
    {
		for(size_t j = list_len-1; j != i-2; --j)
		{
			if(j == list_len-1)
				this->insert(element[j]);
			else
                element[j+1] = element[j];
		}
		element[i-1] = num;
    }
}
void List::remove(size_t i)
{
	if(i < 0 || i > list_len)
	{
		cout << "remove invalid" << endl;
	}
	else
	{
		for(size_t j = i; j != list_len+1; ++j)
		{
				element[j-1] = element[j];
		}
		--list_len;
	}
}
void List::merge(List &L)
{
  if(L.IsEmpty())
	  return;
  size_t size_L = L.ListLen();
  if (list_len+size_L > MaxSize)
  {

  }
}
}

but when i wan to write
void List::merge(List &L)
{
  if(L.IsEmpty())
	  return;
  size_t size_L = L.ListLen();
  if (list_len+size_L > MaxSize)
  {

  }
}

how can i go ahead ?can anyone help ?

Is This A Good Question/Topic? 0
  • +

Replies To: how to write mergeList in a linked List C++ class writing by array

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: how to write mergeList in a linked List C++ class writing by array

Posted 02 July 2011 - 08:55 PM

What have you tried, and what doesn't work?
Was This Post Helpful? 0
  • +
  • -

#3 boyhailong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-January 11

Re: how to write mergeList in a linked List C++ class writing by array

Posted 02 July 2011 - 08:59 PM

View PostPlasticineGuy, on 02 July 2011 - 08:55 PM, said:

What have you tried, and what doesn't work?

because the list_len+size_L > MaxSize
so i have to build a new array to store the two array ,but how to realize it in code ?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: how to write mergeList in a linked List C++ class writing by array

Posted 03 July 2011 - 05:21 AM

It's called LOGIC™. If you actually wrote this code, then you should have no trouble accomplishing this.

You need to create a new array of the correct new size. Copy the existing list into new array, then add the new list into the new array, delete the existing array and set the member pointer to point to the new array. Don't forget to reset the size member variable.
Was This Post Helpful? 0
  • +
  • -

#5 boyhailong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-January 11

Re: how to write mergeList in a linked List C++ class writing by array

Posted 03 July 2011 - 06:39 AM

View PostJackOfAllTrades, on 03 July 2011 - 05:21 AM, said:

It's called LOGIC™. If you actually wrote this code, then you should have no trouble accomplishing this.

You need to create a new array of the correct new size. Copy the existing list into new array, then add the new list into the new array, delete the existing array and set the member pointer to point to the new array. Don't forget to reset the size member variable.

thank you ,i have corrected it !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1