Sign in to follow this  

Modifying a segment in a linked list?

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to figure out how to do it, but all I can get is how to add something to the end.. when I really want to take.. say the 4th segment's struct and change one or all of the variables, keeping the rest of the list intact.. can somebody give me a few pointers on how to do this?

Share this post


Link to post
Share on other sites
Are you refering to std::list, or some other linked list?

If the former, iterators are what you're looking for. A few examples:


#include <list>
#include <iostream>
#include <algorithm> //for std::find
using namespace std;

int main()
{
list<int> l(5, 5);
for(list<int>::iterator i = l.begin(); i != l.end(); ++i)
cout << *i << " ";
cout << endl;

list<int>::iterator itr = l.begin();
advance(itr, 3);
(*itr)++;

for(list<int>::iterator i = l.begin(); i != l.end(); ++i)
cout << *i << " ";
cout << endl;

itr = find(l.begin(), l.end(), 7);
if(itr == l.end()) cout << "No element == 7" << endl;
itr = find(l.begin(), l.end(), 6);
if(itr == l.end()) cout << "No element == 6" << endl;
(*itr) = 5;

for(list<int>::iterator i = l.begin(); i != l.end(); ++i)
cout << *i << " ";
cout << endl;
}

Should output:

5 5 5 5 5
5 5 5 6 5
No element == 7
5 5 5 5 5


CM

Share this post


Link to post
Share on other sites
Hmm, I was refering to a linked list class, which allows you to add data structures.. to me, a more advanced array, that is dynamic. I was told to use this.. logic, I guess, by a poster here a few weeks ago for my snake game.

Share this post


Link to post
Share on other sites
The above post shows the use of the std::list.
If you have to create the list yourself from scratch, you will need to create a node class first of all.

Post the code you have so far, and Ill try to give some useful examples

Share this post


Link to post
Share on other sites
std::list is a linked list class, and it is generic so that it accepts almost any type of data you'd want.

Generally speaking, the only reason to create a custom linked list class is for learning purposes. To that end, see this thread for a good introductory explanation on the subject and a google search that has some useful hits. Good luck

CM

Share this post


Link to post
Share on other sites
If you want to learn how to programm, you should learn linked lists! In fact it is rather simple, just create 2 classes ListItem (for nodes) and List, which hadles all node operations. i.e.

class ListItem
{
public:
char name[32];
ListItem *pNext; // Pointer to next node
};

class List
{
public:
List(void);
void Add( ListItem* p );

private:
ListItem* m_pBegin; // Head of the list

};


Simple method for adding node/item:
void
List::Add( ListItem* pItem, bool atTheEnd )
{
ListItem *p; // Node after which to add new

if( !m_pBegin )
{
m_pBegin = new ListItem( pItem );
return;
}

p = m_pBegin;
while( p->pNext ) // Till current node linked w/ next
{
p = p->pNext; // Goes to next
}

p->pNext = new ListItem( pItem );
}

That's all w/ adding. Of course, in destructor you should delete all items :)


Lekha

Share this post


Link to post
Share on other sites
Here's an example of a Snake game that I did. It uses a linked list class that I put together and should provide an example of what you want to do. Hope it helps. Snake

Share this post


Link to post
Share on other sites
Yeah, I'm learning them for learning purposes... Right now I have a working class with an add, get, and delete function.. just had some trouble with an editing function.

Share this post


Link to post
Share on other sites
Quote:
Original post by 00dave
Yeah, I'm learning them for learning purposes... Right now I have a working class with an add, get, and delete function.. just had some trouble with an editing function.

Post the code for your implementation.

Your get function should allow you to modify the element it returns. Again, an explaination or code snippet would help better determine your problem.

Share this post


Link to post
Share on other sites
here is my get function.

segPos *LinkedList::Get(int pos)
{
segPos *current = head;

for(int i = 0; i < pos && current != NULL; i++)
{
current = current->Next;
}

return current;
}


It doesn't really edit anything, just gets the structure and makes a copy of it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Actualy, it's retuning a pointer to the element you specify, not making a copy.

The difference is important. As it is, you have a pointer to the actual data in your list and can modify it easily with the -> notation. If it was returning a copy, you would have no way to modify data in the list short of taking that copy, modifying that, adding it to the list (perhaps inserting it at an arbitrary point rather than at the end, depending if you need that list in order or not) and deleting the original.

Share this post


Link to post
Share on other sites
No. If you edit what is pointed to by what that function returns (e.g. by 'returnvalue->member = value;'), that change will be reflected within the list data (and thus "seen" by any future accesses to the list). That lets you do what you want, but in C++ it is cleaner to return a reference rather than a pointer. That is what std::list does (hint, hint).

Except that std::list is careful *not* to give direct access to an element by element position (except first and last), because having an easy way to do that (like a .get(), or better-stylistically-but-worse-in-terms-of-what-it-encourages, an operator[]) would make it too easy to unintentionally write slow algorithms using the list. For example, if you wanted to do something with every element in the list, then using .get(i) for each element in a list would waste huge amounts of time, redoing traversal of the list that was already almost done last time through the loop.

Standard library containers also provide for the concept of "iterators", which avoid this problem, and also mesh very neatly with standard library algorithms and functional... widgets. It is worth the effort to learn about all of these. For a std::list, an iterator is basically an object that holds a pointer to a list node, but pretends (by an overload of the dereferencing operator - yes, you can do that) to be a pointer to the data within a list node. It also provides an increment operator, which causes its internal node pointer to 'advance' to the next node. For a std::vector, on the other hand, an iterator may simply be a pointer to an element, since there is a single array allocation being used as storage and we can just point into it. With care, we can write code that doesn't care what the iterator type really is.

Share this post


Link to post
Share on other sites
Thanks alot, I just figured out of a way to accomplish what I needed without having to do this.. but thanks for the info, it will come in handy eventually.

Share this post


Link to post
Share on other sites

This topic is 4394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this