Here is a basic setup. Observe
LinkedList::ShowItems()
Note that I've created a linked list on-the-fly as an example for this thread. It is certainly not a realistic implementation though.
Code:
#include <iostream>
#include <string>
using namespace std;
struct Node
{
int Item;
Node *Next;
};
class LinkedList
{
public:
void AddItem(int itm);
void ShowItems();
//Dtors, Ctors etc.
public:
LinkedList() : m_nodeStart(0){ }
~LinkedList();
private:
Node* m_nodeStart;
};
void LinkedList::AddItem(int itm)
{
Node* p = m_nodeStart;
m_nodeStart = new Node;
m_nodeStart->Next = p;
m_nodeStart->Item = itm;
}
void LinkedList::ShowItems()
{
for(Node* p = m_nodeStart; p != 0; p = p->Next)
cout<<p->Item<<" ";
cout<<endl;
}
LinkedList::~LinkedList()
{
Node* p;
while(m_nodeStart != 0)
{
p = m_nodeStart;
m_nodeStart = m_nodeStart->Next;
delete p;
}
}
int main ()
{
LinkedList MyList;
MyList.AddItem(15);
MyList.AddItem(30);
MyList.AddItem(45);
MyList.ShowItems();
return 0;
}