Linked List in OOP style?

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

Page 1 of 1

1 Replies - 4254 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

Hello,

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 {
	private:
		int data;
		DLItem *prev, *next;
	public:
		friend class DoublyLinkedList;
};

class DoublyLinkedList {
	private:
		DLObject *head, *tail, *curr;
	public:
		DLList(); //constructor
			   // FUNCTIONS HERE
};




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 online

  • Dreaming Coder
  • member icon

Reputation: 5804
  • View blog
  • Posts: 12,642
  • 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 {
private:
	struct Node {
		T *item;
		Node *prev;
		Node *next;
	};
	Node *head;

public:
	DoubleList();
	~DoubleList();
	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