Академический Документы
Профессиональный Документы
Культура Документы
6/21/2012 4:39 AM
Ch 4 - Linked Lists
Value v. Reference
Stack
... 100 96 92 ... Heap ... 12 8 4 ... 10 x: 10 xptr: 0x100 yptr: 0x12
6/21/2012 4:39 AM
Ch 4 - Linked Lists
Review : Pointers
Data types are classified as
Value: variable holds actual value Reference: variable holds a reference (address) to the actual value
delete p1;
p1 = NULL;
6/21/2012 4:39 AM
Ch 4 - Linked Lists
Review : Pointers
Dynamic memory is allocated on the heap using new
Must be deallocated, using delete
int *p = new int [100]; p[0] = 1; delete [] p; // p refers to an array of ints // first element is assigned the value 1 // delete the allocated array
struct Box { int len, width; }; Box *b = new Box; b->len = 10; delete b; b = NULL;
// struct definition
6/21/2012 4:39 AM
Ch 4 - Linked Lists
Review : Pointers
Multi-dimensional pointers
int** mat = new int* [3]; // 3 element array of pointers to int
for (int i = 0; i < 3; i++) { mat[i] = new int[4]; } // allocate an array of 4 ints
6/21/2012 4:39 AM
Ch 4 - Linked Lists
Delete
head = head->next; before->next = after; // remove first node // remove other node
// delete memory
Insert
head = new Node(item, head); // insert at the front
// insert elsewhere
6/21/2012 4:39 AM
Ch 4 - Linked Lists
Delete
before->next = after; // remove a node
// delete memory
Insert
before->next = new Node(item, after); // insert a node
6/21/2012 4:39 AM
Ch 4 - Linked Lists
Node* CreateNode(const ItemType& item, Node* next) { return (new Node(item, next)); } // address if successful, NULL if not
6/21/2012 4:39 AM
Ch 4 - Linked Lists
6/21/2012 4:39 AM
Ch 4 - Linked Lists
6/21/2012 4:39 AM
Ch 4 - Linked Lists
10
while (copyHead != NULL) { tail = tail->next = new Node(copyHead->item, NULL); copyHead = copyHead->next; } }
6/21/2012 4:39 AM
Ch 4 - Linked Lists
11
C++ Exceptions
Error handling mechanism Catch and handle the exception (Use try...catch statement)
try { // statements that may cause an error } catch (ExceptionClass identifier) { // statements to handle the error } // one per handled exception
6/21/2012 4:39 AM
Ch 4 - Linked Lists
12
C++ Exceptions
/** @file ListIndexOutOfRangeException.h */ #include <stdexcept> #include <string> using namespace std; class ListIndexOutOfRangeException : public out_of_range { public: ListIndexOutOfRangeException(const string& message = "") : out_of_range(message.c_str()) {} }; /** @file ListException.h */ #include <stdexcept> #include <string> using namespace std; class ListException : public logic_error { public: ListException(const string& message = "") : logic_error(message.c_str()) {} };
6/21/2012 4:39 AM
Ch 4 - Linked Lists
13
C++ Exceptions
/** @file ListAexcept.h */ #include "ListException.h" #include "ListIndexOutOfRangeException.h" const int MAX_LIST = maximum-size-of-list; typedef desired-type-of-list-item ListItemType;
/** ADT list - Array-based implementation with exceptions */ class List { public: List(); /** @throw None. */ bool isEmpty() const; /** @throw None. */ int getLength() const; /** @throw ListIndexOutOfRangeException If index < 1 or index > getLength() + 1 * * @throw ListException If newItem cannot be placed in the list because the * array is full */ void insert(int index, const ListItemType& newItem) throw (ListIndexOutOfRangeException, ListException);
6/21/2012 4:39 AM
Ch 4 - Linked Lists
14
C++ Exceptions
/** @throw ListIndexOutOfRangeException If index < 1 or index > getLength(). */ void remove(int index) throw (ListIndexOutOfRangeException); /** @throw ListIndexOutOfRangeException If index < 1 or index > getLength(). */ void retrieve(int index, ListItemType& dataitem) const throw (ListIndexOutOfRangeException); private: /** array of list items */ ListItemType items[MAX_LIST]; /** number of items in list */ int size; int translate(int index) const; };
6/21/2012 4:39 AM
Ch 4 - Linked Lists
15
C++ Exceptions
void List::insert(int index, const ListItemType& newItem) throw (ListIndexOutOfRangeException) { if (size >= MAX_LIST) throw ListException("ListException: List full on insert"); if (index >= 1 && index <= size + 1) { } else { throw ListIndexOutOfRangeException( "ListIndexOutOfRangeException: Bad index on insert"); } }
6/21/2012 4:39 AM
Ch 4 - Linked Lists
16
// Node definition struct Node { ItemType item; NodePtr next; // constructor to initialize struct Node(const ItemType& item, const Node* next) : item(item), next(next) {} };
6/21/2012 4:39 AM
Ch 4 - Linked Lists
17
// default // copy
6/21/2012 4:39 AM
Ch 4 - Linked Lists
18
6/21/2012 4:39 AM
Ch 4 - Linked Lists
19
C++ STL
Support for predefined ADTs
Iterators, Algorithms, Containers
Containers
objects that hold other objects depend heavily on the class template construct
Iterators
way to cycle through the objects of a container use the * operator to access object are usually bidirectional (++, --)
Algorithms
act on the containers
6/21/2012 4:39 AM Ch 4 - Linked Lists 20
C++ STL
refer to p226 for more examples
list<int> myList; list<int>::iterator iter; // start at the beginning iter = myList.begin(); // insert five items into the list for (int j = 0; j < 5; j++) { // places j at the front of the list iter = myList.insert(iter, j); } // display the list for (iter = myList.begin(); iter != myList.end(); iter++) { cout << *iter << endl; }
6/21/2012 4:39 AM
Ch 4 - Linked Lists
21