-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
133 lines (112 loc) · 2.29 KB
/
list.cpp
File metadata and controls
133 lines (112 loc) · 2.29 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
//
// Example of use list
//
// Week 3, lecture 3.7
//
#include <iostream>
using namespace std;
typedef struct list_element
{
list_element(int n = 0, list_element* ptr = 0):
data(n), next(ptr) {};
int data;
list_element *next;
} list_element;
class list
{
public:
list():head(0),cursor(0) { cout << "default constructor\n"; }
// list(list_element *h = 0, list_element *c = 0):head(h),cursor(c)
// { cout << "default constructor\n"; }
list(const int *arr, int sz); // move elements from an array
~list(); // destructor
void prepend(int n); // add an element before head
void add(int n); // add an element at the end of the list
int get_element() { return cursor->data;}
void advance() { cursor = cursor->next; }
void print();
private:
list_element *head;
list_element *cursor;
};
// constructor : move elements from an array
list::list(const int array[], int sz)
{
head = cursor = 0;
// populate the list element using add()
for(int i = 0; i < sz; ++i) {
add(array[i]);
}
}
// destructor
list::~list()
{
for(cursor = head; cursor != 0;) {
cursor = head->next;
delete head;
head = cursor;
}
}
void list::prepend(int n)
{
// swap head with current head
if(head) {
head = cursor = new list_element(n, head);
}
// if empty list, make current element as head
else
head = new list_element(n, head);
}
void list::add(int n)
{
if(head) {
cursor = head;
while(cursor) {
// end of the list
if(cursor->next == NULL) {
cursor->next = new list_element(n);
break;
}
// advance to the next element
else
advance();
}
}
// no head, make head with current element
else {
head = new list_element(n);
}
}
void list::print()
{
if(head)
{
cursor = head;
while(cursor) {
cout << get_element() << " ";
advance();
}
}
else
cout << "Empty list!";
cout << endl;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
list myList(arr,5);
myList.add(6);
myList.prepend(0);
myList.print();
list a, b;
cout << "first print\n";
a.print();
a.prepend(9); a.prepend(8);
cout << "list a" << endl;
a.print();
for (int i = 0; i < 40; i++)
b.prepend(i*i);
cout << "lisb b" << endl;
b.print();
return 0;
}