CarranoChap4 - Carrano - CS 150 Carrano - CS 150 11 Arrays:...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Carrano - CS 150 Carrano - CS 150 11 Arrays: Advantages and Disadvantages Advantages: Random access of elements is facilitated via indices. To find a[i], merely add i*(size of single element) to address of a[0]. This also facilitates sorting and searching. Enumerated list idea is easy to conceptualize. Disadvantages: A specific amount of space must be allocated. What if the program actually requires less space? What if the program actually requires more space? Altering the arrays contents can be time-consuming. Inserting an element inside the array requires shifting all later array elements down to make room for it. Removing an internal array element requires shifting all later array elements up to fill the resulting gap. Carrano - CS 150 Carrano - CS 150 22 Linked Lists Data element Location of next item Data element NULL indicator of last element Data element Location of next item Data element Location of next item Data element Location of next item Data element Location of next item Location of first list item Carrano - CS 150 Carrano - CS 150 33 Linked List Advantages A variable amount of space is allocated. No particular space requirements must be specified. Adequate memory is provided dynamically. Extra memory is not wasted. Altering the linked lists contents is efficient. Inserting an element into the linked list merely requires linking the new node to its successor and linking its predecessor to the new node. Before: After: Carrano - CS 150 Carrano - CS 150 44 Linked List Advantages (Continued) Removing an element from the linked list merely requires linking the nodes predecessor to its successor. Linked List Disadvantages Random access of elements is no longer possible. Due to the lack of indices, the entire list must be traversed when a particular element is sought. Unenumerated list idea can be difficult to conceptualize. After: Before: Carrano - CS 150 Carrano - CS 150 55 A Linked List Class Definition in C++ // Class definition file: linkedList.h // #ifndef LINKED_LIST_H #include <string> using namespace std; typedef string elementType; struct node; typedef node* nodePtr; struct node { elementType item; nodePtr next; }; class LinkedList { public: // Constructors and destructor LinkedList(); LinkedList(const LinkedList &list); ~LinkedList(); // Member functions int size(); bool insert(elementType elt); bool remove(elementType elt); bool retrieve(elementType elt, int &position); elementType& operator [ ] (int position); friend istream& operator >> (istream &sourceFile, LinkedList &list); friend ostream& operator << (ostream &destFile, const LinkedList &list); private: // Data member nodePtr head; // Member function nodePtr getNode(elementType elt); }; #define LINKED_LIST_H #endif Note that the mutual dependence between the definitions of the node struct and the nodePtr type requires that their definitions be handled creatively. Carrano -...
View Full Document

Page1 / 20

CarranoChap4 - Carrano - CS 150 Carrano - CS 150 11 Arrays:...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online