-
linked lists
i need to create a linked list of characters which i'll be retrieving from a text file. my problem is that i'll need to access this data in the same order that i retrieve it in. i am only used to writing linked lists so that it actually reverses the data using a stack. any ideas on this?
-
"i am only used to writing linked lists so that it actually reverses the data using a stack. any ideas on this?"
Yep. Learn to write 'em so you can access the items in the list in any order. How about having data members of the list like:
Node* pbeginning
Node* pcurrent
Node* pend
-
Create a double linked list (DLL) and traverse it backwards.
-
"i am only used to writing linked lists so that it actually reverses the data using a stack. any ideas on this?"
First lets straighten out your terminology. A stack and a linked list are two seperate data structures. I'll assume you are refering to a linear linked list. What you should use, is a circular linked list.
If you need more information just post, I'm a bit tight on time. :)
-
You could learn how to add a new node at the end of a list instead of the beginning and save yourself a lot of trouble. :)
Code:
// Assuming a node like this
struct node {
int content;
node *next;
node(int c, node *n)
{
content = c;
next = n;
}
};
// Add at the front
current = new node(data, front);
front = current;
// Add at the end
if (front != 0)
{
for (current = front; current != 0; current = current->next)
{
if (current->next == 0)
break; // Found the end
}
current->next = new node(data, 0);
}
else
front = new node(data, 0); // Empty list