-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1b.cpp
136 lines (116 loc) · 2.57 KB
/
1b.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
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
#include <iostream>
#include <vector>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
int index;
Node();
~Node();
};
Node::Node() : data{INT32_MAX}, next{nullptr}, prev{nullptr}, index{INT32_MAX} { }
Node::~Node(){
data = 0;
index = 0;
}
class LinkedList {
int _size; //the number of nodes in the list
Node* _head; //the address of the first node
Node* _tail; //the address of the last node
public:
LinkedList(); //constructor
~LinkedList(); //destructor
int size() const; //gives the number of nodes
bool empty() const; //if the list is empty or not
Node* head() const; //gives the address of the first node
Node* tail() const;
void append(int data);
void clear(); //deletes all elements in the list
};
LinkedList::LinkedList()
: _size(0), _head(nullptr), _tail(nullptr) { }
LinkedList::~LinkedList() {
clear();
}
Node* LinkedList::head() const{
return _head;
}
Node* LinkedList::tail() const{
return _tail;
}
int LinkedList::size() const{
return _size;
}
bool LinkedList::empty() const{
return _head == nullptr;
}
void LinkedList::clear(){
Node* curr = _head;
while(_head != nullptr){
curr = _head;
_head = _head -> next;
delete curr;
}
_size = 0;
_head = nullptr;
_tail = nullptr;
return;
}
void LinkedList::append(int data){
Node* new_node = new Node();
new_node -> data = data;
new_node -> index = _size - 1;
new_node -> next = nullptr;
new_node -> prev = nullptr;
if(empty()){
_head = new_node;
_tail = new_node;
_size = 1;
new_node -> index = 0;
return;
}
_tail -> next = new_node;
new_node -> prev = _tail;
_tail = new_node;
_size++;
}
void printList(LinkedList& list) {
Node* curr = list.head();
std::cout << curr -> data << " ";
curr = curr -> next;
while (curr != nullptr) {
std::cout << curr -> data << " ";
curr = curr -> next;
}
}
int main() {
int* pointer = new int[300];
for(int i = 0; i < 150; i++){
pointer[i] = 150 + i*2;
}
for(int i = 150; i >= 0; i--){
pointer[i+150] = 449 - i*2;
}
for(int i = 0; i < 300; i ++){
std::cout << pointer[i] << " ";
} std::cout << std::endl;
vector<int> v;
for(int i = 150; i < 449; i+=2){
v.push_back(i);
}
for(int i = 449; i > 150; i-=2){
v.push_back(i);
}
for(int i = 0; i < v.size(); i++){
std::cout << v.at(i) << " ";
} std::cout << std::endl;
LinkedList list;
for(int i = 150; i < 449; i+=2){
list.append(i);
}
for(int i = 449; i > 150; i-=2){
list.append(i);
}
printList(list);
}