数据结构与算法分析_C++语言描述(第2版)Larry_Nyhoff__.ppt
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《数据结构与算法分析_C++语言描述(第2版)Larry_Nyhoff__.ppt》由会员分享,可在线阅读,更多相关《数据结构与算法分析_C++语言描述(第2版)Larry_Nyhoff__.ppt(46页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Main IndexContents1Main IndexContentsCLASS list ConstructorsCLASS list ConstructorsCLASS list OperationsCLASS list OperationsApplication:A List PalindromeApplication:A List Palindrome iteratoriterator Operations Operations Constant Constant IteratorsIterators Declaring and Using Declaring and Using
2、IteratorsIteratorsInserting an element into a listInserting an element into a listRemoving an element from a listRemoving an element from a listThe Function The Function writeListwriteList()()The Function find_last_of()The Function find_last_of()The Sequential Search of a ListThe Sequential Search o
3、f a ListSplicing two listsSplicing two listsOrdered listsOrdered listsRemoving DuplicatesRemoving Duplicates Summary SlidesSummary Slides Chapter 11 The List Container and The List Container and IteratorsIterators1Main IndexContentsShifting blocks of elements to Shifting blocks of elements to insert
4、 or delete a vector iteminsert or delete a vector item2Main IndexContents3Main IndexContentsModel of a list object with links Model of a list object with links to next and previous elementto next and previous element3Main IndexContentsSample listSample list4Main IndexContents5Main IndexContentsThe L
5、ist APIThe List APIlThe list API documents the member function prototype as well as pre-and postconditions.provides three constructors to declare a list object.5Main IndexContents6Main IndexContentsCLASS listConstructorslist();Create an empty list.This is the default constructor.list(int n,const T&v
6、alue=T();Create a list with n elements,each having a specified value.If the value argument is omitted,the elements are filled with the default value for type T.Type T must have a default constructor,and the default value of type T is specified by the notation T().list(T*first,T*last);Initialize the
7、list,using the address range first,last).6Main IndexContents7Main IndexContentsCLASS listOperationsT&back();Return the value of the item at the rear of the list.Precondition:The list must contain at least one element.bool empty()const;Return true if the list is empty,false otherwise.T&front();Return
8、 the value of the item at the front of the list.Precondition:The list must contain at least one element.7Main IndexContents8Main IndexContentsCLASS listOperationsvoid push_back(const T&value);Add a value at the rear of the list.Postcondition:The list has a new element at the rear,and its size increa
9、ses by 1.void pop_back();Remove the item at the rear of the list.Precondition:The list is not empty.Postcondition:The list has a new element at the rear or is empty.8Main IndexContents9Main IndexContentsCLASS listOperationsvoid push_front(const T&value);Add a value at the front of the list.Postcondi
10、tion:The list has a new element at the front,and its size increases by 1.void pop_front();Remove the item at the front of the list.Precondition:The list is not empty.Postcondition:The list has a new element at the front or is empty.int size()const;Return the number of elements in the list.9Main Inde
11、xContentsApplication:A List Palindrome(P247)Application:A List Palindrome(P247)#include#include#include#include/for isalpha()and tolower()using namespace std;/check whether the values in alist read the same forward/and backwards.if so,return true;otherwise return falsetemplate bool isPalindrome(cons
12、t list&alist);10Main IndexContentsint main()string str;list charList;/empty list of charactersint i;char ch;/prompt the user to enter a string that may include blankscout Enter the string:;getline(cin,str,n);/copy all of the non-blank letters as lowercase characters/to the list charListfor(i=0;i str
13、.length();i+)ch=stri;if(isalpha(ch)charList.push_back(tolower(ch);11Main IndexContents/call isPalindrome()and use the return value to designate/whether the string is or is not a palindromeif(isPalindrome(charList)cout str is a palindrome endl;elsecout str is not a palindrome endl;return 0;12Main Ind
14、exContentsFunction Function isPalindromeisPalindrometemplate bool isPalindrome(const list&alist)list copyList;copyList=alist;while(copyList.size()1)if(copyList.front()!=copyList.back()return false;copyList.pop_front();copyList.pop_back();return true;13Main IndexContents/*Run 1:Enter the string:A man
15、,a plan,a canal,PanamaA man,a plan,a canal,Panama is a palindromeRun 2:Enter the string:Go hang a salami,Im a lasagna hogGo hang a salami,Im a lasagna hog is a palindromeRun 3:Enter the string:palindromepalindrome is not a palindrome*/14Main IndexContents15Main IndexContentsCLASS list:iteratorOperat
16、ions*:(反引用运算符)Accesses the value of the item currently pointed to by the iterator.*iteriter;+:Moves the iterator to the next item in the list.iteriter+;+;-:Moves the iterator to the previous item in the list.iteriter-;-;=:Takes two iterators as operands and returns truewhen they both point at the sa
17、me item in the list.iter1=iter2iter1=iter2!=:Returns true when the two iterators do not point at the same item in the list.iter1!=iter2iter1!=iter215Main IndexContentsDeclaring and Using Declaring and Using IteratorsIterators The list class includes iterator as a nested class within its declaration.
18、The declaration of an iterator object must include the class name“iterator”along with the scope operator expression“list:”list:iterator iter;16Main IndexContents17Main IndexContentsCLASS listOperationsiterator begin();Returns an iterator that references the first position(front)of the list.If the li
19、st is empty,the iterator value end()is returned.const_iterator begin();Returns a const_iterator that points to the first position(front)of a constant list.If the list is empty,the const_iterator value end()is returned.17Main IndexContents18Main IndexContentsCLASS listOperationsiterator end();Returns
20、 an iterator that signifies a location immediately out of the range of actual elements.A program must not dereference the value of end()with the*operator.const_iterator end();Returns a const_iterator that signifies a location immediately out of the range of actual elements in a constant list.A progr
21、am must not dereference the value of end()with the*operator.18Main IndexContents list:iterator listIter;list intList;listIter=intList.begin();listIter=intList.end();begin()end()ExampleExampleList iterators move through the list in a ciircular fashion.19Main IndexContentsvoid main()int arr=1,3,3,4,5;
22、int arrSize=sizeof(arr)/sizeof(int);list intList(arr,arr+arrSize);list:iterator iter;iter=find_last_of(intList,3);if(iter!=intList.end()cout *iter “;iter+;cout *iter endl;Searching For The last occurrence of 3 and 7 20Main IndexContents iter=find_last_of(intList,7);if(iter!=intList.end()cout “7 is i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 算法 分析 _C 语言 描述 Larry_Nyhoff_
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内