Modifying a segment in a linked list?

Started by
12 comments, last by 00dave 18 years, 4 months ago
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?
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::findusing 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 55 5 5 6 5No element == 75 5 5 5 5


CM
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.
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
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
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
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
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.
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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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.

This topic is closed to new replies.

Advertisement