Class List

please help me finish the function
List::OrderInsert(T item)
to insert data into list sequentially
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// List.h
#pragma once

template <class T>
class List
{
private:

	class Node
	{
	public:
		T info;
		Node *prev, *next;
	public:
		Node(T info): info(info) { prev = next = NULL; }
	} *head, *tail;

	int count;

private:
	void Prepend(Node *p)
	{
		if (this->head == NULL)
		{
			this->tail = p;
		}
		else
		{
			p->next = this->head;
			this->head->prev = p;
		}
		this->head = p;
		count++;
	}

	void Append(Node *p)
	{
		if (this->tail == NULL)
		{
			this->head = p;
		}
		else
		{
			p->prev = tail;
			this->tail->next = p;
		}
		this->tail = p;
		count++;
	}

	void InsertBefore(Node *q, Node *p)
	{
		p->prev = q->prev;
		p->next = q;
		q->prev = p;
		count++;
	}

	void InsertAfter(Node *q, Node *p)
	{
		p->next = q->next;
		p->prev = q;
		q->next = p;
		count++;
	}

	void Remove(Node *p)
	{
		Node *next = p->next;
		Node *prev = p->prev;

		if (p == this->head) this->head = next;
		if (p == this->tail) this->tail = prev;
		delete p;

		if (next != NULL) next->prev = prev;
		if (prev != NULL) prev->next = next;
		count--;
	}

public:
	List() : count(0) { head = tail = NULL; }
	~List()
	{
		this->Clear();
	}

public:
	bool IsEmpty() { return this->head == NULL; }
	void Clear()
	{
		Node *p = this->head;
		while (p != NULL)
		{
			this->head = p->next;
			delete p;
			p = this->head;
		}
		count = 0;
	}

public:
        void OrderInsert(T item)
          {
          ///////////// Insert code here
           ///////////// to insert data into the List sequentially
          }
	void PushBegin(T item) { Prepend(new Node(item)); }
	
	void PushEnd(T item) { Append(new Node(item)); }
	
	T PopBegin()
	{
		T info = this->head->info;
		Remove(head);
		return info;
	}

	T PopEnd()
	{
		T info = this->tail->info;
		Remove(tail);
		return info;
	}

	void InsertAt(T info, int index)
	{
		Node *p = new Node(info);

		if (index == 0 || q == NULL)
			Prepend(p);
		else
		{
			if (index >= this->count)
				Append(p);
			else
			{
				Node *q = this->head;
				while (index > 0)
				{
					index--;
					q = q->next;
				}
				InsertBefore(q, p);
			}
		}
	}

	void RemoveAt(int index)
	{
		if (index == 0)
			Remove(head);
		else
		{
			if (index >= this->count)
				Remove(tail);
			else
			{
				Node *q = this->head;
				while (index > 0)
				{
					index--;
					q = q->next;
				}
				Remove(q);
			}
		}
	}
};
Last edited on
pseudocode:
node n = head;
while value of node n is less than value of item, go to next node;
insert a new node with value of item before n;//note: this line is not in a loop 

it would be a bit more difficult if tis wasn't a doubly linked list.
Last edited on
thank "hamsterman"
are you a professional programmer ?
i see you can answer any the quetions.
Topic archived. No new replies allowed.