insertion and deletion into custom "list" type

This is about the list type from PPP2 in Chapter 20.

This is the class and its code as I have it so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef LIST_H_
#define LIST_H_

template<typename Elem>
class Link
{
public:
	Link *prev;
	Link *succ;
	Elem val;
};

template<typename Elem>
class list
	// representation and implementation details
{
public:
	using size_type = unsigned long;
	using value_type = Elem;
	class iterator;
	class const_iterator;

	iterator begin()	// iterator to first element
	{
		return *first;
	}
	iterator end()		// iterator to last element
	{
		return *last;
	}
	const_iterator begin() const
	{
		return *first;
	}
	const_iterator end() const
	{
		return *last;
	}

	size_type size();

	iterator insert(iterator p, const Elem &v);		// insert v into list after p
	iterator erase(iterator p);						// remove p from list

	void push_back(const Elem &v);					// insert v at end
	void push_front(const Elem &v);					// insert v at front
	void pop_front();		// remove the first element
	void pop_back();		// remove the last element

	Elem &front();			// the first element
	Elem &back();			// the last element

private:
	Link<Elem> *first;
	Link<Elem> *last;
};

#endif

// requires Element<Elem>() (ยง19.3.3)
template<typename Elem>
class list<Elem>::iterator
{
	Link<Elem> *curr;	// current link
public:
	iterator(Link<Elem> *p)
		: curr{ p } { }

	iterator &operator++() { curr = curr->succ; return *this; }		// forward
	iterator &operator--() { curr = curr->prev; return *this; }		// backword
	Elem &operator*() { return curr->val; }

	bool operator==(const iterator &b) { return curr == b.curr; }
	bool operator!=(const iterator &b) { return curr != b.curr; }
};

template<typename Elem>
list<Elem>::iterator list<Elem>::insert(list<Elem>::iterator p, const Elem &v)
{
	
}


Whenever I try to reference the iterator p.curr in insert, and I hover curr to see what the compiler will say it is, what I get is
<unknown> list<Elem>::iterator::curr
. Why is that?

That's one problem I'm having. The other is that I can't tell how to define insertion and deletion operations for this (as well as how to define size()).

I get that insertion has to be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if p is pointing to front of list
    make previous pointer of current node point to the new node
    make next pointer of new node point at current node
    make previous pointer of new node point to NULL
    move *first (pointer to head of list) point to the new node
else if p is somewhere in middle of list
    have the next and previous pointers of new node point to both the current node and the one before it
    change previous pointer of current node to point at new node
    change the next pointer of the node pointed by the previous pointer of current node to point to the new node

else if p is pointing to back of list
    make next pointer of current point to new node
    make previous pointer of new node point to current node
    make next pointer of new node point to NULL
    move *last (pointer to back of list) point to new node


But how do I say that in code (if it's correct)? How do I get at a Link from a list and how do I get at a list from a list::iterator? And do I just insert v, or do I have to allocate a new node inside insert and initialize its Link data element to v (how, though?)?

And I might need help on the constructor.

The insertion and deletion, as well as the constructor and destructor, will require dynamic memory allocation or de-allocation as needed, right? Since this container has to own its and manage to its own memory.
Last edited on
Topic archived. No new replies allowed.