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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
im sorry, ill post it now, and can you also help me get the output as shown above.
#include <cassert> // Provides assert
#include <cstdlib> // Provides NULL and size_t
namespace CISP430_A5 {
/* CONSTRUCTORS and DESTRUCTOR */
template <typename Item>
linked_list<Item>::linked_list() {
head_ptr = NULL;
tail_ptr = NULL;
cursor = NULL;
precursor = NULL;
many_nodes = 0;
}
template <typename Item>
linked_list<Item>::linked_list( const linked_list& source ) { // copier
int src_size = source.size(); // to replicate cursor position
int many_til_end = 0; // to replicate cursor position
int many_til_mid = 0; // to replicate cursor position
node<Item> *src_cursor = source.cursor; // to replicate cursor position
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
/*replicate the cursor position*/
if (src_cursor != NULL) {
while (src_cursor != NULL) {
++many_til_end;
src_cursor = src_cursor->link();
}
many_til_mid = src_size - many_til_end;
start();
for (int i=0; i<many_til_mid; ++i) {
advance(); // DEPENDS ON VALUE OF many_nodes ABOVE
}
else {
cursor = NULL;
precursor = NULL;
}
}
template <typename Item>
linked_list<Item>::~linked_list() { // Destructor
list_clear(head_ptr);
head_ptr = tail_ptr = cursor = precursor = NULL;
many_nodes = 0;
}
/* MODIFICATION MEMBER FUNCTIONS */
template <typename Item>
void linked_list<Item>::start() {
if (many_nodes > 0) { // if at least one item exists
cursor = head_ptr;
precursor = NULL;
}
}
template <typename Item>
void linked_list<Item>::advance() {
if (is_item()) {
precursor = cursor;
cursor = cursor->link();
if ( cursor == NULL ) {
precursor = NULL;
}
}
}
template <typename Item>
typename linked_list<Item>::value_type linked_list<Item>::get(size_t position) {
for (int i=0; i<=position; ++i) {
if (i==0)
start();
else
advance();
if (i==position) {
return current();
}
}
}
template <typename Item>
void linked_list<Item>::set(size_t position, const value_type& entry) {
if (is_item()) {
insert(position, entry); // insert new item to list
advance();
remove_current(); // remove old item from list.
}
}
template <typename Item>
void linked_list<Item>::add(const value_type& entry) {
cursor = precursor = NULL; // remove cursor
attach_here(entry); // <<-- attaches at end when no cursor.
}
template <typename Item>
void linked_list<Item>::remove(size_t position) {
for (int i=0; i<=position; ++i) {
if (i==0)
start();
else
advance();
if (i==position) {
remove_current();
}
}
}
template <typename Item>
void linked_list<Item>::insert(size_t position, const value_type& entry) {
for (int i=0; i<=position; ++i) {
if (i==0)
start();
else
advance();
if (i==position) {
insert_here(entry);
}
}
}
template <typename Item>
void linked_list<Item>::insert_here(const value_type &entry){
if (!is_item() && !many_nodes) {
cursor = new node<Item>(entry);
head_ptr = cursor;
tail_ptr = cursor;
}
else if (is_item() && cursor == head_ptr) {
cursor = new node<Item>( entry );
cursor->set_link( head_ptr );
head_ptr = cursor;
}
else if (!is_item() && many_nodes) {
cursor = new node<Item>( entry );
cursor->set_link( head_ptr );
head_ptr = cursor;
}
else if (is_item() && cursor != head_ptr) {
cursor = new node<Item>( entry );
cursor->set_link( precursor->link() );
precursor->set_link( cursor );
}
++many_nodes; //increase the node count
}
template <typename Item>
void linked_list<Item>::attach(size_t position, const value_type& entry) {
for (int i=0; i<=position; ++i) {
if (i==0)
start();
else
advance();
if (i==position) {
attach_here(entry);
}
}
}
template <typename Item>
void linked_list<Item>::attach_here(const value_type& entry) {
template <typename Item>
void linked_list<Item>::operator =(const linked_list& source) {
if (this != &source) {
list_clear(head_ptr);
head_ptr = tail_ptr = cursor = precursor = NULL;
many_nodes = 0;
int src_size = source.size(); // to replicate cursor position
int many_til_end = 0; // to replicate cursor position
int many_til_mid = 0; // to replicate cursor position
node<Item> *src_cursor = source.cursor;
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
/*replicate the cursor position*/
if (src_cursor != NULL) {
while (src_cursor != NULL) {
++many_til_end;
src_cursor = src_cursor->link();
}
many_til_mid = src_size - many_til_end;
start(); // put this.cursor at beginning.
for (int i=0; i<many_til_mid; ++i) {
advance();
}
/* after the for loop, the new linked_list's cursor should be at the same position as the source's cursor was.*/
}
else {
cursor = NULL;
precursor = NULL;
}
}
}
/* CONSTANT MEMBER FUNCTIONS */
template <typename Item>
typename linked_list<Item>::value_type linked_list<Item>::current() const {
if ( is_item() ) {
return cursor->data();
}
}
/*LINKED LIST TOOLS*/
template <typename Item>
linked_list<Item> list_splice(linked_list<Item> &input, size_t offset, size_t length) {
/*removes as many items as specified by length from the input
list starting from the zero-based offset. Don't try a length longer
than the number of remaining items (yet). Input gets modified, and the return value
is a new linked list consisting of the that were removed from input.*/
linked_list<Item> spliced;
for (int i=offset; i<offset+length; ++i) {
spliced.add( input.get(i) );
input.remove(i);
}
return spliced;
}
template <typename Item>
linked_list<Item> list_splice(linked_list<Item> &input, size_t offset, size_t length, Item new_item) {
/*removes as many items as specified by length from the input
list starting from the zero-based offset.Input gets modified, and the return value
is a new linked list consisting of the that were removed from input.
Additionally, new_item has been put in place of the items that were removed.*/
input.insert(offset, new_item);
return list_splice(input, offset+1, length);
}
template <typename Item>
linked_list<Item> list_splice(linked_list<Item> &input, size_t offset, size_t length, linked_list<Item> new_items) {
// input.insert(offset, new_item);
return list_splice(input, offset+1, length);
}
/*template prefix not needed here since the second parameter names a specific type for the linked_list*/
string charList_join(const char* glue, linked_list<char> pieces) {
string joined = "";
for (int i=0; i<pieces.size(); ++i) {
if (i==0) { //only for the first piece do we not include the glue because there are one less glues than the total pieces (one glue between each piece).
joined += pieces.get(i); //??? How do you append a char to the string variable ???
}
else {
// joined += glue + pieces.get(i); //??? Does this way work instead of the two line method below? Answer:
joined += glue;
joined += pieces.get(i);
}
}
return joined;
}
template <typename Item>
typename linked_list<Item>::value_type list_pop(linked_list<Item> list) {
return (list_splice(list, list.size()-1, 1)).get(0);
}
}
|