#include <iostream>
using namespace std;
#include "ArrayVector.h"
#include "LinkedList.h"
//a slow (= bad) method for displaying a linked list.
//the [] operator is O(n), not O(1)
void DisplayLL_Slow(LinkedList<int> &LL)
{
int i;
for(i=0;i<LL.Length();i++) //for each element,
cout << LL[i] << ' '; //display this element
cout << endl;
}
//a better method for displaying a linked list by parsing through every entry
void DisplayLL_Fast(LinkedList<int> &LL)
{
LL.Reset(); //reset the current pointer to the beginning
while(!LL.AtEnd()) //if we aren't at the end,
{
cout << LL.GetCurrent() << ' '; //display the current element
LL.GoNext(); //go to the next element
}
cout << endl;
}
int main()
{
int i;
LinkedList<int> LL; //create a new linked list of integers
for(i=0;i<4;i++)
LL.Push(i*i); //add 4 entries to the linked list
DisplayLL_Slow(LL);
DisplayLL_Fast(LL); //display the linked list using the two methods above
LL.Pop(i); //get the most recently pushed element (3*3 = 9)
cout << i << endl; //display it
LL.Push(10); //insert 10 instead of 9
DisplayLL_Fast(LL); //display the current contents
Vector<int> Vec;
LL.Dump(Vec); //dump the linked list data into a vector (a good idea when you don't intend
//to resize the data any more.)
int *LLData;
LLData = LL.Dump(); //the same as dumping into a Vector, except it dumps into a memory-leaked pointer.
for(i=0;i<Vec.Length();i++)
cout << Vec[i] << ' ';
cout << endl; //display the current contents of the Vector. Note that dumping reverses the data.
for(i=0;i<LL.Length();i++)
cout << LLData[i] << ' ';
cout << endl; //display the current contents of the leaked pointer.
delete[] LLData; //we have to delete the pointer, but not the Vector. the Vector will free its memory
//when it leaves scope.
return 0;
}