|  | |  |
04-12-2003, 03:58 PM
|
#1 (permalink)
| | Registered User
Join Date: Apr 2003
Posts: 4
| What do these Methods do? Take a look please 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;
} |
| |
04-14-2003, 10:00 PM
|
#2 (permalink)
| | LOAD "*",8,1
Join Date: Feb 2003 Location: la.ca.us
Posts: 254
| the only people who name their functions "Method1", "Method2", etc. are idiots and teachers/professors.
no, we will not do your homework for you. |
| |
04-15-2003, 11:45 AM
|
#3 (permalink)
| | Registered User
Join Date: Apr 2003
Posts: 2
| LMAO!!!!! :th: |
| |
04-15-2003, 02:27 PM
|
#4 (permalink)
| | Registered User
Join Date: Feb 2003
Posts: 34
| Any idiot off the street could figure those out (although they may have trouble reading it without code tags).
As stated previosuly... no, we will not do your homework for you. |
| |
04-15-2003, 03:06 PM
|
#5 (permalink)
| | Newbie
Join Date: Jun 2002 Location: Denmark
Posts: 1,726
| hmmm.. I actualy started reading them, then I realised I couldn't keep up with where I was at, without the proper indentation and just left it as it was.... btw, what value is capacity and new_capacity initialised as??
I think I have that problem noticed in one of my C++ books as an exersize for the reader... |
| |
04-15-2003, 03:59 PM
|
#6 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,532
| hehe, i was gonna put the code tags in, but i saw that the original post wasn't indented .. oh well.
__________________ Mike |
| |
04-15-2003, 07:11 PM
|
#7 (permalink)
| | Registered User
Join Date: Apr 2003
Posts: 4
| 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;
} |
| |
04-15-2003, 07:13 PM
|
#8 (permalink)
| | Registered User
Join Date: Apr 2003
Posts: 4
| 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?? |
| |
04-15-2003, 09:06 PM
|
#9 (permalink)
| | LOAD "*",8,1
Join Date: Feb 2003 Location: la.ca.us
Posts: 254
| you're right about everything except about the word "integers". this template can work on whatever datatype you declare it as. |
| |
04-15-2003, 10:07 PM
|
#10 (permalink)
| | Registered User
Join Date: Apr 2003
Posts: 4
| oh yea
When Method 5 is called, it allocates more memory. |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 03:01 PM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |