File tree 4 files changed +104
-0
lines changed
4 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 1
1
#include " ll.hh"
2
+ #include < cassert>
2
3
3
4
node* head = nullptr ;
4
5
Original file line number Diff line number Diff line change
1
+ #include " ll.hh"
2
+ #include < cassert>
3
+
4
+ node* head = nullptr ;
5
+
6
+ // Adds n to the end of the list whose head is head.
7
+ // Sets n->next and n->prev to the proper values.
8
+ // Assumes that n is a live node and not already a member of the list.
9
+ void append (node* n) {
10
+ (void ) n; // Placeholder to avoid compiler warnings. Replace with your code!
11
+ }
12
+
13
+ // representation checker
14
+ void check_list () {
15
+ node* prev = nullptr ;
16
+ node* trav = head;
17
+ while (trav) {
18
+ assert (trav->prev == prev);
19
+ prev = trav;
20
+ trav = trav->next ;
21
+ }
22
+ }
23
+
24
+ int main () {
25
+ // Code to test 'append'
26
+ check_list ();
27
+
28
+ node N1 = {1 , nullptr , nullptr };
29
+ append (&N1);
30
+ check_list ();
31
+ assert (head != nullptr );
32
+
33
+ node N2 = {2 , nullptr , nullptr };
34
+ append (&N2);
35
+ check_list ();
36
+ assert (head != nullptr );
37
+ }
Original file line number Diff line number Diff line change
1
+ #include " ll.hh"
2
+ #include < cassert>
3
+
4
+ node* head = nullptr ;
5
+
6
+ // Removes n from the list.
7
+ // Assumes that n is a live object and a member of the list.
8
+ // Upon return, n is still a live object, but no longer a member of the list.
9
+ void erase (node* n) {
10
+ (void ) n; // Placeholder to avoid compiler warnings. Replace with your code!
11
+ }
12
+
13
+ // Adds n to the end of the list whose head is head.
14
+ // Sets n->next and n->prev to the proper values.
15
+ // Assumes that n is a live node and not already a member of the list.
16
+ void append (node* n) {
17
+ node* prev = nullptr ;
18
+ node* trav = head;
19
+ while (trav) {
20
+ prev = trav;
21
+ trav = trav->next ;
22
+ }
23
+ n->prev = prev;
24
+ n->next = nullptr ;
25
+ if (prev) {
26
+ prev->next = n;
27
+ } else {
28
+ head = n;
29
+ }
30
+ }
31
+
32
+ // representation checker
33
+ void check_list () {
34
+ node* prev = nullptr ;
35
+ node* trav = head;
36
+ while (trav) {
37
+ assert (trav->prev == prev);
38
+ prev = trav;
39
+ trav = trav->next ;
40
+ }
41
+ }
42
+
43
+ int main () {
44
+ // Code to test 'erase'
45
+ check_list ();
46
+
47
+ node N1 = {1 , nullptr , nullptr };
48
+ node N2 = {2 , nullptr , nullptr };
49
+ node N3 = {3 , nullptr , nullptr };
50
+ append (&N1);
51
+ append (&N2);
52
+ append (&N3);
53
+ check_list ();
54
+
55
+ erase (&N2);
56
+ check_list ();
57
+
58
+ erase (&N3);
59
+ check_list ();
60
+
61
+ erase (&N1);
62
+ check_list ();
63
+
64
+ assert (head == nullptr );
65
+ }
Original file line number Diff line number Diff line change 1
1
#include " ll.hh"
2
+ #include < cassert>
2
3
3
4
node* head = nullptr ;
4
5
You can’t perform that action at this time.
0 commit comments