[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

question on list

<list> is based on pointers..Lets say i have some list with 10 objects. I need to delete the 7th object. My problem is that i dont quite well understand how the list is implemented in the compiler. In order to delete the 7th object through pointers, i need to get the link to the first Node..And thats my problem. If i had to create the list.h by myself it would be easy for me to handle the link since i created. However, i am not sure how to do this when using <list> of STL..
Thanks in advance
Carl
0
carlNYC
Asked:
carlNYC
  • 3
  • 3
  • 2
  • +1
1 Solution
 
AxterCommented:
Use the erase member function.

If you want to erase the 7th item, you could use the following method.

if (mylist.size() >= 7) mylist.erase(mylist.begin() + 7 -1);
0
 
carlNYCAuthor Commented:
I was thinking about it but then i realized that my prof.  wants me to do break the link of the 7th object , then to link the 6th to 8 th..I am pretty sure that i can do it, my only problem is how to create a link pointing to the head of the list..If i get to the head, i can manipulate whatever i want next..
0
 
designedmindCommented:
calling the mylist.erase does just that. the implimentation details of a list object are hidden from you on purpose: "for your own good" as your parents may have said to you. in fact, the implimentation of the stl <list> is arbitrary, it may in fact be implimented by a dynamically resizing array, it might be implimented as a binary tree! the point is, you should only consider the public interface that <list> provides and not worry about the implimentation details.

when you use the erase function, it automagically "heals" itself.

another way to effectively erase the 7th item would be to pop all of the objects from the list and as you go, enqueue (listname.push_back(object)) them into a new list, leaving out the 7th item. then, replace the old one with the new one, delete the new one. or you could do this much much easier with the stl's copy function,

 copy( listname,listname.begin()+7,listname.end());
//move all of the ones after the 7th down one position
listname.erase(listname.end()-1);
//erase the last one, remember listname.end() points to
//the object after the last one in the list

using the public interfaces, you cant change the underlying structure itself.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
carlNYCAuthor Commented:
I see. In my opinion, i would go in that way. However, sometimes, its not up to me. Alright, can you give some time cus i think i should better off ask my prof. how he whants it. But any way.. how coild i see the imlpementation together with the function definitions of list. I was wondering if list has an object Node(struct or class) which sets the pointer to the object of my type, then i could use this object and do whatever my prof. wants..browsing,deleting, adding, swapping, etc..Honsetsly, i am stil not sure what my prof. wants but as soon as i find out i will have better idea.
0
 
SalteCommented:
1. If this is homework we can only offer limited help.

2. If this is homework, chances are that you cannot use std::list but are supposed to implement your own list class. Exactly because you are supposed to learn how it is done.

If you are told to use std::list that is another matter, in this case they probably want you to learn to use the STL and in that case you simply use the std::list::erase function as already mentioned.

The erase function of list does exactly what you think it should do. The std::list is a template and you can therefore read the whole source code. All template functions are provided with source code so you can search in the list include file and find the list::erase function and see for yourself what it does or doesn't do.

Alf
0
 
designedmindCommented:
in microsoft visual c++ 6, right click on the <list> and then click on open file.

the implimentation may change from compiler to compiler or eaven from version to version

0
 
SalteCommented:
Yes, the implementation may change but that is largely irrelevant here.

I suggested to check the source just to get an idea of what is going on and how list does what it do.

It may change from one implementation or version to another but it won't change that much, the external interface is largely the same and that it is implemented using a list is probably also a good bet, otherwise it wouldn't be a list data type :-)

However, the layout of the node etc may differ, but that is not relevant here I think.

Alf
0
 
carlNYCAuthor Commented:
Ok , i am getting really confused and 100% because of my prof. Once, he said that i dont need to create list.h ,next time he saysthat i dont have to use the <list> member functions.  am really confused about what he wants.Here is the begginig of the task..-
Build a pointer-based ADT called ExtremeList that is a templated extension to the List STL.
As a hint he says-Write a templated class called ExtremeList which is expands the functionality of List.
I told him that for me it would be easier to do my own list.h instead of not knowing how at least to start.What would you suggest me to do in this case? Thats all i need-which way to go..
Thanks
Carl
0
 
designedmindCommented:
as a fellow college kid (frosh at drexel), i know your pain all too well.

if you derrive from the stl's list, you have access to its protected members *yay* this is your "foot in the door" if you will

open up the <list> file

it says template<...> class list{
protected:

and then it lists the protected members there. since this is for an assignment, i wont show you the code to do it, but basicly, you are going to use the protecte _Nodeptr _Ptr as your "head" node and get in that way...

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now