Okay, so I am trying to create a "linked list" and I'm running into trouble and I'm a little confused on how I am supposed to implement "nodes".
A collection of nodes that point to each other is considered a linked list, right? I'm trying to create a function that accepts an array along with it's size, and i'm supposed to take each element, and be able to sort it as I put it into a list, then put it back into the array ordered. How am I even supposed to do this? I know that a node holds a certain element, and a pointer to the next node. Is there already a structure in the STL, or do I have to make it from scratch?
Can anybody help me get started? (aside or addition to what I've already got). I also don't know how to go from elements of an array to elements of a node and how to sort them in order. I'm working with ints right now and im hoping to move to templates. Help would be much appreciated! Thanks :)
usingnamespace std;
/***********************************************
* NODE
***********************************************/
struct Node
{
int data;
Node * pNext;
};
/***********************************************
* INSERTION SORT
* Sort the items in the array
**********************************************/
//template <class T>
void sortInsertion(int array[], int num)
{
// Make sure we can list the elements unsorted
cout << "Testing sortInsertion()...\n\n";
cout << "Unsorted:\n";
for (int i = 0; i < num; i++)
cout << "num "<< i + 1 << ": " << array[i] << endl;
cout << endl;
cout << "Sorted:\n";
// Okay, make the head of the list...
Node *pHead;
// Loop through each element and insert it into the list, depending on order
for (int i = 0; i < num; i++);
// Loop through each node, and transfer each element into the array.
for(const Node *p = pHead; p; p = p -> pNext)
{
int i = 0;
array[i] = p -> data;
i++;
}
}