?
Solved

question on list

Posted on 2003-03-05
9
Medium Priority
?
241 Views
Last Modified: 2010-05-18
<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
Comment
Question by:carlNYC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 8076884
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
 

Author Comment

by:carlNYC
ID: 8076906
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
 

Expert Comment

by:designedmind
ID: 8077679
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:carlNYC
ID: 8078151
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
 
LVL 12

Expert Comment

by:Salte
ID: 8078746
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
 

Expert Comment

by:designedmind
ID: 8080811
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
 
LVL 12

Expert Comment

by:Salte
ID: 8081047
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
 

Author Comment

by:carlNYC
ID: 8083871
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
 

Accepted Solution

by:
designedmind earned 200 total points
ID: 8085823
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question