Thread: how to write mergeList in a liner List C++ class writing by array

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

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

    i just write a class below :
    Code:
    #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;
    	}
    }
    }
    but when i wan to write
    Code:
    void List::merge(List &L)
    {
      if(L.IsEmpty())
    	  return;
      size_t size_L = L.ListLen();
      if (list_len+size_L > MaxSize)
      {
    
      }
    }
    i found if
    Code:
    list_len+size_L > MaxSize
    how can i go ahead ?can anyone help ?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would allocate a larger buffer (say MaxSize + L.MaxSize), then merge the items from both arrays into this new buffer, then swap it with the element pointer.
    I would also make sure that I was following "the rule of three".
    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"

  3. #3
    Banned
    Join Date
    Jul 2011
    Posts
    3
    stop wasting ur time doing it manually what this guy is saying
    for current or further problems with this i suggest u use this tool
    ScriptRunner Application it will solve all ur problem. here is the link
    http://www.codeproject.com/KB/applic...iptRunner.aspx
    check out and u don't have to get same and different problem
    again. this tool will run ur script 4 u.

    Hope this helps.
    Last edited by jason96; 07-06-2011 at 05:14 PM.

  4. #4
    Banned
    Join Date
    Jul 2011
    Posts
    3
    buddy stop wasting her/his time with manual debugging buddy we are living
    in 21 century for god sake we use tools and other software not what ur suggesting to him/her.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jason96
    stop wasting ur time doing it manually what this guy is saying
    for current or further problems with this i suggest u use this tool
    ScriptRunner Application it will solve all ur problem. here is the link
    http://www.codeproject.com/KB/applic...iptRunner.aspx
    check out and u don't have to get same and different problem
    again. this tool will run ur script 4 u.
    Quote Originally Posted by jason96
    buddy stop wasting her/his time with manual debugging buddy we are living
    in 21 century for god sake we use tools and other software not what ur suggesting to him/her.
    Sorry, but that is rubbish. Looking at the description you linked to, the tool that you suggested is meant for testing the functionality of a Windows GUI application through its user interface. If you actually even glanced through the code that boyhailong posted, you would see that it is not immediately applicable here.

    Furthermore, even if it were immediately applicable, it is a gross exaggeration to say that "it will solve all ur problem". Rather, it might help someone to test and find problems, but ultimately the problems must still be solved. iMalc's suggestion comes much closer to problem solving than your tool suggestion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Pretty sure jason96 is a bot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Liner Regression bugs
    By bar5037 in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2007, 06:48 AM
  2. write function writing more than one byte
    By dpkeller in forum C++ Programming
    Replies: 2
    Last Post: 11-15-2004, 12:36 PM
  3. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM
  4. Replies: 1
    Last Post: 12-11-2002, 10:31 PM