-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListNode.cpp
66 lines (61 loc) · 1.39 KB
/
ListNode.cpp
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
#include "ListNode.h"
#include <iostream>
#include <cassert>
namespace luzinsan
{
ListNode* ListNode::BeginList{nullptr};
ListNode*& ListNode::getBeginList() { return BeginList; }
ListNode*& ListNode::getNext() { return l_next; }
ListNode::ListNode() : l_info{ 0 }, l_next{nullptr}{}
ListNode* ListNode::InsertNode(ListNode*& p, int i)
{
ListNode* q = new ListNode;
assert(q && "Память не выделилась!!!");
q->l_info = i;
if (!p) // если список ещё не заполнен ни одним элементом
p = q;
else
{
q->l_next = p->l_next;
p->l_next = q;
}
return this;
}
int ListNode::DeleteNode(ListNode* p)
{
ListNode* q = p->l_next;
int val = q->l_info;
p->l_next = q->l_next;
delete q;
return val;
}
ListNode* ListNode::PrintList(ListNode* p)
{
std::cout << "Список:\n";
do
{
std::cout << p->l_info << ' ';
p = p->l_next;
} while (p != BeginList);
std::cout << "\n";
return this;
}
ListNode* ListNode::Rationing(ListNode* p)
{
do
{
if (p->l_info > 0)
p->l_info -= p->l_next->l_info;
else p->l_info += p->l_next->l_info;
p = p->l_next;
} while (p != BeginList);
return this;
}
ListNode* ListNode::Dispose()
{
while (BeginList != BeginList->l_next)
DeleteNode(BeginList);
delete[] BeginList;
return this;
}
}