View Single Post
Old 07-12-2004, 08:56 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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;
}
__________________
Valmont is offline   Reply With Quote