-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathintset.cpp
More file actions
149 lines (127 loc) · 2.69 KB
/
Copy pathintset.cpp
File metadata and controls
149 lines (127 loc) · 2.69 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
#include <iostream>
#include <assert.h>
#include "intset.h"
using namespace std;
Intset::Intset()
{
this->size = 0;
this->head = NULL;
}
Intset::~Intset()
{
Node* head = this->head;
while(head){
Node* del = head;
head = head->next;
delete del;
}
}
/* Return true if key is in the set */
bool Intset::find(int key)
{
Node* head = this->head;
int i = 1;
while(head){
if (head->data == key) return true;
head = head->next;
i++;
}
return false;
// int low = 0, high = size-1, mid;
/* Binary search for key... */
// while (low <= high) {
// mid = (low + high) / 2;
// if (key == A[mid]) return true;
// if (key > A[mid]) low = mid+1;
// else high = mid-1;
// }
// return false;
}
/* Inserts a new key. It is an error if key is already in the set. */
void Intset::insert(int key)
{
assert (!find(key));
Node* node = new Node();
node->data = key;
if (this-> size == 0) {
node->next = this->head;
this->head = node;
this->size++;
} else if (this->head->data >= key) {
node->next = this->head;
this->head = node;
this->size++;
} else {
Node* head = this->head;
while(head->next && head->next->data < key) {
head = head->next;
}
node->next = head->next;
head->next = node;
this->size++;
}
// int i;
//
// assert (!find(key));
//
// /* Enlarge (by 2x) the underlying array if it isn't large enough... */
// if (size == allocated) {
// allocated *= 2;
// int *newA = new int[allocated];
// for (i=0; i<size; i++) newA[i] = A[i];
// delete[] A;
// A = newA;
// }
//
// /* Shift up elements in array to make room for new element... */
// size++;
// i = size-1;
// while (i>0 && A[i-1]>key) {
// A[i] = A[i-1];
// i--;
// }
// A[i] = key;
}
/* Removes a key. It is an error if key isn't in the set */
void Intset::remove(int key)
{
assert (find(key));
if (this->head->data == key){
Node* del = this->head;
this->head = this->head->next;
delete del;
} else{
for (Node* head = this->head; head->next; head = head->next) {
if ((head->next)->data == key) {
Node* del = head->next;
head->next = del->next;
delete del;
break;
}
}
}
// int i;
//
// assert (find(key));
//
// /* Shift down elements in array to plug gap left by deleted element... */
// for (i=size-1; key!=A[i]; i--);
// while (i<size-1) {
// A[i] = A[i+1];
// i++;
// }
// size--;
}
void Intset::print(void)
{
Node* head = this->head;
int i = 1;
while(head){
cout << head->data << "\n";
head = head->next;
i++;
}
// int i;
// for (i=0; i<size; i++)
// cout << A[i] << "\n";
}