Thread: erroe when write the member function“mergeList” in a linear list

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    19

    erroe when write the member function“mergeList” in a linear list

    Code:
    #include<iostream>
    using namespace std;
    const int DefaultSize = 100;
    class List
    {
    public:
    	List();
    	List(size_t size);
    	List(List &L);
    	~List();
    	bool insert(const int);
    	void ClearList();
    	bool IsEmpty();
    	size_t ListLen();
    	int GetElem(size_t);
    	void append(size_t,int);//在第i个位置插入数值
    	void Display();
    	void remove(size_t);
    	List& merge(List &L);
    	int FindElem(int elm);
    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(List &L)
    {
    	list_len = 0;
    	size_t L_size = L.ListLen();
    	MaxSize = 2*(list_len+L_size);
    	for (size_t i = 0; i != L_size; ++i)
    	{
    		insert(L.GetElem(i));
    	}
    }
    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;
    }
    int List::GetElem(size_t size)
    {
    	if(size > list_len)
    		throw runtime_error("error");
    	return element[size];
    }
    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;
    	}
    }
    List& List::merge(List &L)
    {
    	if(L.IsEmpty())
    		return *this;
    	size_t size = list_len + L.ListLen();
    	List merge_L(MaxSize+L.ListLen());
    	for (size_t i = 0; i != size; ++i)
    	{
    		if (i < list_len)
    		{
    			merge_L.insert(element[i]);
    		} 
    		else
    		{
    			merge_L.insert(L.GetElem(i-list_len));
    		}
    	}
    	return merge_L;
    }
    int List::FindElem(int elm)
    {
    	for (size_t i = 0; i != list_len; ++i)
    	{
    		if (element[i] == elm)
    			return i+1;
    	}
    	return -1;
    }
    int main()
    {
    	List l,m,n;
    	l.insert(11);
    	l.insert(12);
    	m.insert(13);
    	m.insert(0);
    	List Q(l.merge(m));
    	cout << endl;
    }
    when i run it in VS2008,it reads “can't read the memory ‘’0XCCCCC”,i think there is some error in the fuction “merge”,but how the deal with it ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Copy constructor is wrong. You never allocate elements.
    merge is also wrong. You must never return a reference to a local variable. Return by-value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    19
    Quote Originally Posted by Elysia View Post
    Copy constructor is wrong. You never allocate elements.
    merge is also wrong. You must never return a reference to a local variable. Return by-value.
    thank you for your answer. ihave correct it and test correctly.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You shouldn't have started a second thread for this.

    It looks to me like the swap step is missing from that copy and swap I sugested for the Merge, or are you changing the semantics now to return the result and hence be a const function?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-06-2011, 11:27 PM
  2. Linear member growth
    By zacs7 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 06-10-2009, 04:56 PM
  3. deleting nodes in a linear list
    By sudhanshu_nsit in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2006, 02:00 AM
  4. Linear list
    By blackswan in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 10:22 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread