Jump to content
  • Advertisement
Sign in to follow this  
00dave

Modifying a segment in a linked list?

This topic is 4762 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!