List - CSS342: Linked Lists Professor: Munehiro Fukuda...

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

View Full Document Right Arrow Icon
CSS342: Linked Lists 1 CSS342: Linked Lists Professor: Munehiro Fukuda
Background image of page 1

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

View Full DocumentRight Arrow Icon
CSS342: Linked Lists 2 Topics Basic concepts of linked lists Implementations Special implementations Applications
Background image of page 2
CSS342: Linked Lists 3 Linked List template<class Object> struct LListNode { Object item; LListNode *next; }; template<class Object> class LList { public: LList( ); LList( const LList &rhs ); ~LList( ); bool isEmpty( ) const; void clear( ); void insert( const Object &x, int index ); // insert a new item right after the index int find( const Object &x ) const; void remove( const Object &x ); const LList &operator=( const LList &rhs ); private: LListNode<Object> *header; LListNode<Object> *findByIndex( int index ) const; LListNode<Object> *findPrevious( const Object &x ) const; }; 20 45 51 76 84 60 Removed item Inserted item NULL X X == 45 header Basic Concepts
Background image of page 3

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

View Full DocumentRight Arrow Icon
CSS342: Linked Lists 4 STL Class list #include <list> list( ); list(size_type num, const T& value); list( const list<T>& anotherList); Constructors. See the text page 221 for more details. Example: list<int> list1; list<string> list2(10, “straightB”); bool empty( ) const; Returns true if the list has no elements. Example: while (list1.empty( ) == false) { …} iterator begin( ); Returns an iterator that references the first element.If there is nothing, it is the same as the value returned by end( ) Example: list<string>::iterator i = list2.begin( ); if ( i != list2.end( ) ) cout << *I << endl; iterator end( ); Return the end marker. Example: see begin( ). size_type size() const; Returns the number of elements. Size_type is an integral type. You can compare it with an integer. Example: while (list1.size( ) > 0) { … } iterator insert(iterator position, const T& value); Inserts an item value immediately before the element specified by the iterator position . Example: list<string>::iterator i = list2.insert (10, “abc”); void remove (const T& value); Removes all items with value from the list. Example: list2.remove(“straightB”); iterator erase( iterator position ); Removes the item pointed to by iterator position . Example: list1.erase( 2 ); void push_back(const T& x); void push_front(const T& x); T& front(); void clear(); void pop_front(); void pop_back(); void reverse(); See your own C++ textbook or http://www.cppreference.com/ Basic Concepts
Background image of page 4
CSS342: Linked Lists 5 STL Class list Example #include <list> #include <iostream> #include <string> using namespace std; int main( ) { list<string> facultyList; list<string>::iterator i = facultyList.begin( ); i = facultyList.insert( i, “berger” ); i = facultyList.insert( i, “cioch” ); i = facultyList.insert( i, “fukuda” ); cout << “#faculty members: “ << facultyList.size( ) << endl; for (i = facultyList.begin( ); i != facultyList.end( ); i++ ) cout <<*i << endl; } Results (= inputs are reversed): #faculty members: 3 fukuda cioch berger Basic Concepts
Background image of page 5

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

View Full DocumentRight Arrow Icon
CSS342: Linked Lists 6 Array-Based List Implementation Easy implementation Fixed list size Necessity to shift data down to an extended space upon an insertion and to shift data up upon a deletion What is big-O for those operations?
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 35

List - CSS342: Linked Lists Professor: Munehiro Fukuda...

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

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