Thread: What do these Methods do?? Take a look

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    What do these Methods do?? Take a look

    Can anyone help me and tell me what Method1, Method2, Method3, Method4, Method5 do?


    template <class T>
    bool List<T> ::Method1 (const T & value)
    {
    if (used == capacity)
    if(Method5 (capacity + CHUNK) ==false)
    return false;
    if (first == -1)
    first = last = 0;
    else if (first == 0)
    first = capacity -1;
    else
    first --;
    data[first] = value;
    used++;
    return true;
    }

    template <class T>
    bool List<T> ::Method2 (const T & value)
    {
    if (used == capacity)
    if(Method5 (capacity + CHUNK) ==false)
    return false;
    if (last == -1)
    first = last = 0;
    else
    last = (last +1) % capacity;
    data[first] = value;
    used++;
    return true;
    }

    template <class T>
    bool List<T> ::Method3 ( T & value)
    {
    if (used ==0)
    return false;
    value = data[first];
    if(capacity - used >= 1.5 * CHUNK)
    if(Method5 (capacity + CHUNK) ==false)
    return false;
    used--;
    if (used ==0)
    first = last = -1;
    else
    first = (first +1) % capacity;
    return true;
    }

    template <class T>
    bool List<T> ::Method4 ( T & value)
    {
    if (used ==0)
    return false;
    value = data[first];
    if(capacity - used >= 1.5 * CHUNK)
    if(Method5 (capacity + CHUNK) ==false)
    return false;
    used--;
    if (used ==0)
    first = last = -1;
    else if (last ==0)
    last = capacity -1;
    else
    last --;

    return true;
    }



    template <class T>
    bool List<T> ::Method5 (const size_t & new_capacity)
    {
    T * temp = data;
    data = new T[new_capacity];
    if(data == NULL)
    {
    data = temp;
    return false;
    }
    if (used >0)
    {
    int j =first;
    int k = (new_capacity - used) /2;
    first =k;
    last = first + used -1;
    for (int i =0; i <used ; i++)
    {
    data[k] = temp[j];
    j = (j+1) % capacity;
    k++;
    }
    }
    capacity = new_capacity;
    delete []temp;
    return true;
    }

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    We will not do your homework for you. Sorry.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    sorry to say, but its not homework...

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    edit ur post to use code tags and then I might take a look at it..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Code:
    template <class T>
    bool List<T> ::Method1 (const T & value)
    {
    	if (used == capacity)
    			if(Method5 (capacity + CHUNK) ==false)
    				return false;
    	if (first == -1)
    			first = last = 0;
    	else if (first == 0)
    		first = capacity -1;
    	else
    		first --;
    	data[first] = value;
    	used++;
    	return true;
    }
    
    template <class T>
    bool List<T> ::Method2 (const T & value)
    {
    	if (used == capacity)
    			if(Method5 (capacity + CHUNK) ==false)
    				return false;
    	if (last == -1)
    			first = last = 0;
    	else 
    		last = (last +1) % capacity;
    	data[first] = value;
    	used++;
    	return true;
    }
    
    template <class T>
    bool List<T> ::Method3 ( T & value)
    {
    		if (used ==0)
    			return false;
    		value = data[first];
    		if(capacity - used >= 1.5 * CHUNK)
    				if(Method5 (capacity + CHUNK) ==false)
    					return false;
    		used--;
    		if (used ==0)
    			first = last = -1;
    		else
    			first = (first +1) % capacity;
    			return true;
    }
    
    template <class T>
    bool List<T> ::Method4 ( T & value)
    {
    	if (used ==0)
    			return false;
    		value = data[first];
    		if(capacity - used >= 1.5 * CHUNK)
    				if(Method5 (capacity + CHUNK) ==false)
    					return false;
    	used--;
    		if (used ==0)
    			first = last = -1;
    		else if (last ==0)
    			last = capacity -1;
    		else 
    			last --;
    
    		return true;
    }
    
    
    
    template <class T>
    bool List<T> ::Method5 (const size_t & new_capacity)
    {
    	T * temp = data;
    	data = new T[new_capacity];
    	if(data == NULL)
    	{
    		data = temp;
    		return false;
    	}
    	if (used >0)
    	{
    			int j =first;
    			int k = (new_capacity - used) /2;
    			first =k;
    			last = first + used -1;
    			for (int i =0; i <used ; i++)
    			{
    				data[k] = temp[j];
    				j = (j+1) % capacity;
    				k++;
    			}
    	}
    	capacity = new_capacity;
    	delete []temp;
    	return true;
    }

  6. #6
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by gqchynaboy
    sorry to say, but its not homework...
    I find it very hard to believe that any program that was not created for learning purposes would use horrible function names such as Method1, Method2, etc. Let us know where the program came from, if it is not homework.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    we don't mind if it is homework, as long as you show effort and don't ask anyone to write it for you.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    Thumbs up

    thanks thats all i was asking for...

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Method 1 “InsertFirst”
    If there is no space, Method 5 is called to create new space.
    In addition, Method 1 adds integers to the beginning of the list.

    Method 2 “InsertLast”
    If there is no space, Method 5 is called to create new space.
    In addition, Method 2 adds integers to the end beginning of the list.

    Method 3 “RemoveFirst”
    If there is no space, Method 5 is called to create new space.
    In addition, Method 3 removes the integers from the beginning of the list.

    Method 4 “RemoveLast”
    If there is no space, Method 5 is called to create new space.
    In addition, Method 4 removes the last integers of the list

    Method 5 “AddSpace”

    Am i right??
    If you would like to add anything else, that would be very helpful

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well supposedly they are member functions of a templated List class although they do not seem to act like a list. more like a vector or hashtable. Memory stored is contiguous. This means its not a list. Interesting isnt it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    oh yea
    When Method 5 is called, it allocates more memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 02:52 AM
  2. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  3. Turn Based Stradegy System Methods
    By TylerMoyer in forum Game Programming
    Replies: 2
    Last Post: 07-30-2007, 10:45 PM
  4. Lesson #5 - Methods
    By oval in forum C# Programming
    Replies: 1
    Last Post: 05-04-2006, 03:09 PM
  5. Replies: 8
    Last Post: 07-27-2003, 01:52 PM