forked from ucsb-cs24-w26/STARTER-lab01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintlist.cpp
More file actions
159 lines (135 loc) · 3.12 KB
/
Copy pathintlist.cpp
File metadata and controls
159 lines (135 loc) · 3.12 KB
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
// intlist.cpp
// Implements class IntList
// Harsha Malaviya 1/11/26
#include "intlist.h"
#include <iostream>
using std::cout;
// copy constructor
IntList::IntList(const IntList& source) {
head = nullptr;
tail = nullptr;
*this = source;
//IMPLEMENT THIS
}
// destructor deletes all nodes
IntList::~IntList() {
Node* curr = head;
while (curr) {
Node* next = curr->next;
delete curr;
curr = next;
}
head = nullptr;
tail = nullptr;
}
// return sum of values in list
int IntList::sum() const {
int sum = 0;
Node* curr = head;
while(curr){
sum+=curr->info;
curr = curr ->next;
}
return sum;
}
// returns true if value is in the list; false if not
bool IntList::contains(int value) const {
Node* curr = head;
while(curr){
if(curr->info == value){ return true;}
curr = curr ->next;
}
return false;
}
// returns maximum value in list, or 0 if empty list
int IntList::max() const {
if(!head) return 0;
int max = head->info;
Node* curr = head;
while(curr){
if(curr->info>max) max = curr->info;
curr = curr ->next;
}
return max;
}
// returns average (arithmetic mean) of all values, or
// 0 if list is empty
double IntList::average() const {
if(!head) return 0;
int num = this->count();
int sum = this->sum();
return static_cast<double>(sum) / num;
}
// inserts value as new node at beginning of list
void IntList::push_front(int value) {
Node* new_node = new Node();
new_node->info = value;
if(!head){
head = new_node;
tail = new_node;
}else if(!head->next){
new_node->next = head;
head = new_node;
} else {
new_node->next = head;
head = new_node;
}
}
// append value at end of list
void IntList::push_back(int value) {
Node *new_node = new Node();
new_node->info = value;
if (tail){
tail->next = new_node;
tail = new_node;
} else {
head = new_node;
tail = new_node;
}
}
// return count of values
int IntList::count() const {
if(!head) return 0;
int i = 0;
Node* curr = head;
while(curr){
i++;
curr = curr ->next;
}
//IMPLEMENT THIS
return i;
}
//Assignment operator should copy the list from the source
//to this list, deleting/replacing any existing nodes
IntList& IntList::operator=(const IntList& source){
if (this == &source) return *this;
Node* curr = head;
while (curr) {
Node* next = curr->next;
delete curr;
curr = next;
}
head = nullptr;
tail = nullptr;
curr = source.head;
while (curr) {
push_back(curr->info);
curr = curr->next;
}
return *this;
}
// constructor sets up empty list
IntList::IntList():head(nullptr),tail(nullptr){ }
// DO NOT CHANGE ANYTHING BELOW (READ IT THOUGH)
// print values enclose in [], separated by spaces
void IntList::print() const {
Node *n = head;
cout << '[';
while (n) {
cout << n->info;
if (n->next)
cout << " ";
n = n->next;
}
cout << ']';
}