Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   help in writing a function to retrieve last item in an unsorted list (http://www.go4expert.com/forums/help-writing-function-retrieve-item-t15157/)

elsa87 17Nov2008 18:18

help in writing a function to retrieve last item in an unsorted list
 
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

shabbir 17Nov2008 18:21

Re: help in writing a function to retrieve last item in an unsorted list
 
Can you define ItemType as well as how the list is passed to the function ReturnLastItem

elsa87 17Nov2008 19:05

Re: help in writing a function to retrieve last item in an unsorted list
 
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?

shabbir 17Nov2008 20:32

Re: help in writing a function to retrieve last item in an unsorted list
 
How do you pass that unsorted list to the function so that it can get the last element.

xpi0t0s 17Nov2008 20:33

Re: help in writing a function to retrieve last item in an unsorted list
 
Is the list held in a global variable?
How is ItemType defined?
Are you on a course?

elsa87 17Nov2008 21:08

Re: help in writing a function to retrieve last item in an unsorted list
 
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

shabbir 17Nov2008 21:31

Re: help in writing a function to retrieve last item in an unsorted list
 
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.

elsa87 17Nov2008 21:34

Re: help in writing a function to retrieve last item in an unsorted list
 
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

elsa87 17Nov2008 21:37

Re: help in writing a function to retrieve last item in an unsorted list
 
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];

                        }
}


xpi0t0s 17Nov2008 21:49

Re: help in writing a function to retrieve last item in an unsorted list
 
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?


All times are GMT +5.5. The time now is 13:19.