help in writing a function to retrieve last item in an unsorted list

elsa87's Avatar, Join Date: Oct 2008
Go4Expert Member
hi everyone..
please help me write a function which will return the last element in an unsorted list.
the prototype of the function should be
ItemType ReturnLastItem();

we should assume that the list is not empty ans it shouldnt change after the program is executed..
pleeeeeeeeease help me
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Can you define ItemType as well as how the list is passed to the function ReturnLastItem
0
elsa87's Avatar, Join Date: Oct 2008
Go4Expert Member
Im sorry i dont know what you mean..
all i know that i have an unsorted list and i should retrieve the last item in it..
can u plz help?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
How do you pass that unsorted list to the function so that it can get the last element.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Is the list held in a global variable?
How is ItemType defined?
Are you on a course?
0
elsa87's Avatar, Join Date: Oct 2008
Go4Expert Member
actually yes..i'm taking this course and i dont really feel like im learning anything in it..
the whole question says that we should add a member function ReturnLastItem () to the UnsortedType ADT that returns the last element in the list. The function has the following prototype:
ItemType ReturnLastItem();
Preconditions: List is initialized and not empty.
Postconditions: List is unchanged.
this is all i know..and i have no idea how it should be done..
we should point to the last item and then retrieve it but i dont know how to point to that item if i dont know the size of the list??
plz help me out
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I got it and its a member function but then giving you the code would not help but you should make a try and then its very straight forward.
0
elsa87's Avatar, Join Date: Oct 2008
Go4Expert Member
omg..plz help me out..after u give me the code i will be able to understand how it's done..but with nothing i will never understand a single thing
0
elsa87's Avatar, Join Date: Oct 2008
Go4Expert Member
Code:
void UnsortedType::RetrieveItem(ItemType& item, bool& found){
	bool moreToSearch;
	int location=0;
	found = false;
	moreToSearch = (location < length);
	while (moreToSearch && !found)
	{

                            found = true;
                	item = info[location];

			}
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Don't be so feeble; have a go. You've been given the prototype so you should be able to get the first line (??? UnsortedType::???????) and the last statement in the function (a return statement).

The code you've posted seems to have something missing - a mechanism to step to the next item in the list and an if statement that should go before "found=true". What is this code?

On the line that sets moreToSearch, there is a reference to a variable called "length". You said you don't know the length of the list. So what is length, if it's not the length of the list?

Is info[] where the individual entries in the list are stored?