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
|
//file: linked_list.h//
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include <cstdlib> // Provides size_t
#include <string> // allows usage of the string type
#include "node.h" // Provides node class
using namespace std;
namespace CISP430
{
template <typename Item>
class linked_list
{
public:
/* TYPEDEFS and MEMBER CONSTANTS: */
typedef Item value_type;
/* CONSTRUCTORS and DESTRUCTOR: */
linked_list( );
linked_list(const linked_list& source);
~linked_list( );
/* MODIFICATION MEMBER FUNCTIONS */
/*ADDED:*/
value_type get(size_t position); // get item at position x
void set(size_t position, const value_type& entry); // set item at position x, if past boundaries, just modify at beginning or at end.
void add(const value_type& entry); // add to the end of list
void insert(size_t position, const value_type& entry); // set item at position x, if past boundaries, just insert at beginning or attach at end.
void attach(size_t position, const value_type& entry); // set item at position x, if past boundaries, just insert at beginning or attach at end.
void remove(size_t position);
/*REMOVED:*/
/*void remove_current( );*/
/*void start( );*/
/*void advance( );*/
/*void insert(const value_type& entry);*/
/*void attach(const value_type& entry);*/
/*STAYS THE SAME:*/
void operator=(const linked_list& source);
/* CONSTANT MEMBER FUNCTIONS */
/*STAYS THE SAME:*/
value_type current( ) const;
size_t size( ) const { return many_nodes; }
bool is_item( ) const { return (cursor != NULL); }
private:
/*PRIVATE HELPER FUNCTIONS*/
/*ADDED:*/
void remove_current( );
void start( );
void advance( );
void insert_here(const value_type& entry); // same as insert() from sequence. insert at current position.
void attach_here(const value_type& entry); // same as attach() from sequence. attach at current position.
/*PRIVATE VARIABLES*/
/*STAYS THE SAME:*/
node<Item> *cursor;
node<Item> *precursor;
node<Item> *head_ptr;
node<Item> *tail_ptr;
size_t many_nodes;
};
/*Linked List Tools*/
template <typename Item>
linked_list<Item> list_splice(linked_list<Item> &input, size_t offset, size_t length);
template <typename Item>
linked_list<Item> list_splice(linked_list<Item> &input, size_t offset, size_t length, Item new_item);
template <typename Item>
linked_list<Item> list_splice(linked_list<Item> &input, size_t offset, size_t length, linked_list<Item> new_items);
/*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);
template <typename Item>
typename linked_list<Item>::value_type list_pop(linked_list<Item> list);
}
/* Sample usage:
int main(void) {
linked_list items = new linked_list;
items.attach(10);
items.insert(30);
items.attach(20);
items.insert(40);
// for each item in the list:
for (int i=0; i<items.size(); ++i) {
cout << items.get(i) << endl;
}
items.set(2, 50);
// item at position 2 is now 50.
}
*/
// The implementation of a template class must be included in its header file:
#include "linked_list.template"
#endif
//file: node.template//
// FILE: node.template
// IMPLEMENTS: The functions of the node template class and the
// linked list toolkit (see node2.h for documentation).
//
// NOTE:
// Since node is a template class, this file is included in node2.h.
// Therefore, we should not put any using directives in this file.
//
// INVARIANT for the node class:
// The data of a node is stored in data_field, and the link in link_field.
#include <cassert> // Provides assert
#include <cstdlib> // Provides NULL and size_t
namespace CISP430_A5
{
template <class Item>
void list_clear(node<Item>*& head_ptr)
// Library facilities used: cstdlib
{
while (head_ptr != NULL)
list_head_remove(head_ptr);
}
template <class Item>
void list_copy(
const node<Item>* source_ptr,
node<Item>*& head_ptr,
node<Item>*& tail_ptr
)
// Library facilities used: cstdlib
{
head_ptr = NULL;
tail_ptr = NULL;
// Handle the case of the empty list
if (source_ptr == NULL)
return;
// Make the head node for the newly created list, and put data in it
list_head_insert(head_ptr, source_ptr->data( ));
tail_ptr = head_ptr;
// Copy rest of the nodes one at a time, adding at the tail of new list
source_ptr = source_ptr->link( );
while (source_ptr != NULL)
{
list_insert(tail_ptr, source_ptr->data( ));
tail_ptr = tail_ptr->link( );
source_ptr = source_ptr->link( );
}
}
template <class Item>
void list_head_insert(node<Item>*& head_ptr, const Item& entry)
{
head_ptr = new node<Item>(entry, head_ptr);
}
template <class Item>
void list_head_remove(node<Item>*& head_ptr)
{
node<Item> *remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link( );
delete remove_ptr;
}
template <class Item>
void list_insert(node<Item>* previous_ptr, const Item& entry)
{
node<Item> *insert_ptr;
insert_ptr = new node<Item>(entry, previous_ptr->link( ));
previous_ptr->set_link(insert_ptr);
}
template <class Item>
size_t list_length(const node<Item>* head_ptr)
// Library facilities used: cstdlib
{
const node<Item> *cursor;
std::size_t answer;
answer = 0;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
++answer;
return answer;
}
template <class NodePtr, class SizeType>
NodePtr list_locate(NodePtr head_ptr, SizeType position)
// Library facilities used: cassert, cstdlib
{
NodePtr cursor;
SizeType i;
assert(0 < position);
cursor = head_ptr;
for (i = 1; (i < position) && (cursor != NULL); ++i)
cursor = cursor->link( );
return cursor;
}
template <class Item>
void list_remove(node<Item>* previous_ptr)
{
node<Item> *remove_ptr;
remove_ptr = previous_ptr->link( );
previous_ptr->set_link(remove_ptr->link( ));
delete remove_ptr;
}
template <class NodePtr, class Item>
NodePtr list_search(NodePtr head_ptr, const Item& target)
// Library facilities used: cstdlib
{
NodePtr cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
if (target == cursor->data( ))
return cursor;
return NULL;
}
}
|