1 / 14

2 线性表 (3)

2 线性表 (3). 教学目标. 1. 了解线性结构的特点 2. 掌握顺序表的定义、查找、插入和删除 3. 掌握链表的定义、查找、插入和删除 4. 能够从时间和空间复杂度的角度比较两种存储结构的不同特点及其适用场合 5. 应用线性结构解决基本问题 6. 了解 STL 中的 vector,list 的基本使用方法. 教学内容. 2.1 线性表的定义 2.2 线性表的顺序表示和实现 2.3 线性表的链式表示和实现 2.4 线性表与 STL (标准模板库). 2.4 线性表与 STL (标准模板库). vector 与顺序表 list 与链表.

lynn
Download Presentation

2 线性表 (3)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2 线性表(3)

  2. 教学目标 1.了解线性结构的特点 2.掌握顺序表的定义、查找、插入和删除 3.掌握链表的定义、查找、插入和删除 4.能够从时间和空间复杂度的角度比较两种存储结构的不同特点及其适用场合 5.应用线性结构解决基本问题 6.了解STL中的vector,list的基本使用方法

  3. 教学内容 2.1 线性表的定义 2.2 线性表的顺序表示和实现 2.3 线性表的链式表示和实现 2.4 线性表与STL(标准模板库)

  4. 2.4 线性表与STL (标准模板库) • vector与顺序表 • list与链表

  5. 方法2的时间 复杂度为? vector 举例:合并顺序表 // 方法2 顺序表vb最后被清空 #include <vector> //…… vector<int> va(n), vb(m); //分别为va,vb输入数据 while(vb.empty()==false) { va.push_back(vb.front()); vb.erase(vb.begin()); } cout<<va.size()<<endl; // n+m 方法1的算法时间复杂度为O(n) // 方法1 #include <vector> //…… vector<int> va(n), vb(m); //分别为va,vb输入数据 for(int i=0; i<vb.size(); i++) { va.push_back(vb[i]); } cout<<va.size()<<endl; // n+m

  6. list 举例:合并有序链表 算法时间复杂度依赖于 STL的处理效率,大概为O(n2) #include <list> // 方法1 //…… int a[5] = {19,3,6,7,12}, b[4] = {22,7,19,6}; list<int> La, Lb; list<int> ::iterator it; // 迭代器,本质就是指向链表结点的指针 // 一次性将指定长度的数组元素加入链表 La.insert (La.begin(), a, a + 5); Lb.insert (Lb.begin(), b, b + 4); La.sort(); Lb.sort(); // 链表排序 La.merge (Lb); // 合并两个有序链表; // 输出 for (it = La.begin(); it != La.end(); it++) // 3 6 6 7 7 12 19 19 22 cout << *it << " "; cout << endl;

  7. list 举例:合并有序链表 算法时间复杂度为O(n) list<int> La, Lb; list<int> ::iterator it1,it2; // 迭代器,本质就是指向链表结点的指针 // 分别向2个链表添加若干有序元素 La.insert (La.end(), 3); La.insert (La.end(), 6); La.insert (La.end(), 12); Lb.insert (Lb.end(), 6); Lb.insert (Lb.end(), 7); Lb.insert (Lb.end(), 22); it1=La.begin(); for(it2=Lb.begin(); it2!=Lb.end(); it2++) // 合并有序链表 { while(it1!=La.end() && *it1<*it2) it1++; // 寻找合适的插入位置 La.insert(it1, *it2); } // 输出 for (it1 = La.begin(); it1 != La.end(); it1++) // 3 6 6 7 12 22 cout << *it1 << " "; cout << endl;

  8. 应用举例: 利用链表实现大整数处理 时间复杂度为O(n) int carry=0; // 处理进位 while(it1!=La.end() || it2!=Lb.end()) { int c=carry; if(it1!=La.end()) { c = c + (*it1); it1++; } if(it2!=Lb.end()) { c = c + (*it2); it2++; } carry=c/10; Lc.push_back(c%10); } // 利用链表实现大整数加法运算 // 链表存储顺序: 个位在前,高位在后 // Lc=La+Lb list<int> BigAddList(list<int> La, list<int> Lb) { list<int> Lc; list<int> ::iterator it1,it2; it1=La.begin(); it2=Lb.begin(); // 循环处理代码见右边 // …… if(carry>0) Lc.push_back(carry); return Lc; } // 其实list就是双向链表,完全可以用指针 直接逆序处理,但代码书写稍稍麻烦

  9. // 函数BigAddList调用示例: int main(){ list<int> La, Lb, Lc; list<int> ::iterator it1,it2,it3; // 链表存储顺序: 个位在前,高位在后 3672+872=4544 La.push_front(3); La.push_front(6); La.push_front(7); La.push_front(2); // 2种不同的插入方法,效果相同 Lb.insert (Lb.begin(), 8); Lb.insert (Lb.begin(), 7); Lb.insert (Lb.begin(), 2); Lc=BigAddList(La, Lb); Lc.reverse(); // 逆置链表,仅仅为了方便输出 for (it3 = Lc.begin(); it3 != Lc.end(); it3++) cout << *it3 << " "; cout <<endl; return 0; }

  10. LA LB LC 2 4 2 7 4 7 5 6 8 4 3 it1 it1 it2 it1 it1 it1 it2 it2 it2 carry=0 1

  11. 本课程的知识掌握及考查的要求 1)理解基本概念 2)掌握逻辑结构 3)掌握存储结构,实现各种基本操作 4)利用基本原理和方法,能够对算法进行设计或分析 5)能够选择合适的数据结构和方法进行问题求解

  12. 本章小结 1、线性表的逻辑结构特性是数据元素之间存在着线性关系;在计算机中表示这种关系的两类不同的存储结构是顺序存储结构(顺序表)和链式存储结构(链表)。 2、熟练掌握两类存储结构的描述方法,掌握链表中的头结点、头指针和首元结点的区别。 3、熟练掌握顺序表的查找、插入和删除算法 4、熟练掌握链表的查找、插入和删除算法 5、能够从时间和空间复杂度的角度比较两种存储结构的不同特点及其适用场合

  13. 本章详细要求:

  14. 课后建议: • HLoj 8801-8808,8812-8824 • HDoj 1002, 1715,1753(大整数加法) 大整数加法的效率:vector 优于 string 优于 list

More Related