Linked List in OOP style?

Looking for info on how to effectively construct a linked list in OOP

Page 1 of 1

1 Replies - 8140 Views - Last Post: 06 September 2008 - 04:16 PM Rate Topic: -----

#1 kapax  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 65
  • Joined: 02-July 08

Linked List in OOP style?

Posted 06 September 2008 - 01:20 PM


I have decided to make a small collection of functions to work with doubly linked lists in OOP style.

My first idea which I still have for now is to make one class, which would be a linked list itself and another class, which would be the items in that linked list.

To make it more clear, here is some code:

class DLItem {
		int data;
		DLItem *prev, *next;
		friend class DoublyLinkedList;

class DoublyLinkedList {
		DLObject *head, *tail, *curr;
		DLList(); //constructor

Note that the code above is just a draft and might change dramatically and I just wrote it to make you my idea a little more clear.

What I want to ask for is how to realize my idea so that one class would be a template of a linked list itself and another - of an item in that list.

If you think that's ineffective, feel free to tell me and I accept any offers.

This post has been edited by kapax: 06 September 2008 - 01:41 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Linked List in OOP style?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 7160
  • View blog
  • Posts: 14,925
  • Joined: 16-October 07

Re: Linked List in OOP style?

Posted 06 September 2008 - 04:16 PM

Look into C++ templates. Note, you don't need curr or tail. There's no tail in a circular list, but if you really want it, node->prev is usually enough.

This should get you going:
template <typename T>
class DoubleList {
	struct Node {
		T *item;
		Node *prev;
		Node *next;
	Node *head;

	int size() const;
	int find(T *) const;
	void add(T *);
	void remove(T *);
	void get(unsigned index);
	void clear();

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1