ImageVerifierCode 换一换
格式:DOCX , 页数:81 ,大小:40.72KB ,
资源ID:8590327      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8590327.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据结构及STL.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

数据结构及STL.docx

1、数据结构及STL数据结构*day09*第一课 堆栈与队列1、数据结构的基本概念(1)逻辑结构1)集合结构(集):结构中的元素除了同属一个集外,没有任何联系2)线性结构(表):结构中的元素具有一对一的前后关系3)树型结构(树):结构中的元素具有一对多的父子关系4)网状结构(图):结构中的元素具有多对多的交叉映射关系(2)物理结构1)顺序结构(数组):结构中的元素存放在一段连续的地址空间中。随机访问方便,空间利用率低,插入删除不方便2)链式结构(链式):结构中的元素存放在彼此独立的地址空间中,每个独立的地址空间叫节点。节点中除了保存数据以外还保存另外一个或几个相关节点的地址。空间利用率高,插入删除

2、方便,随机访问不方便(3)逻辑结构和物理结构的关系 表 树 图顺序 数组 顺序树 复链式 链表 链式树 合2、数据结构的基本运算(1)创建与销毁 (2)插入与删除(3)获取与修改(4)排序与查找3、堆栈(vi static.cpp/lstatic.cpp)(1)基本特征:后进先出(2)基本操作:压入(push),弹出(pop)(3)实现要点:初始化空间,栈顶指针,判空判满【代码:vi stack.cpp】#include using namespace std;/ 基于顺序表的堆栈class Stack public: / 构造过程中分配内存 Stack (size_t size = 10)

3、: m_array (new intsize), m_size (size), m_top (0) / 析构过程中释放内存 Stack (void) if (m_array) delete m_array; m_array = 0; / 压入 void push (int data) if (full () throw OverFlow (); m_arraym_top+ = data; / 弹出 int pop (void) if (empty () throw UnderFlow (); return m_array-m_top; / 判满 bool full (void) const r

4、eturn m_top = m_size; / 判空 bool empty (void) const return ! m_top; private: / 上溢异常 class OverFlow : public exception const char* what (void) const throw () return 堆栈上溢!; ; / 下溢异常 class UnderFlow : public exception const char* what (void) const throw () return 堆栈下溢!; ; int* m_array; / 数组 size_t m_siz

5、e; / 容量 size_t m_top; / 栈顶;void printb (unsigned int numb, int base) Stack stack (256); do stack.push (numb % base); while (numb /= base); while (! stack.empty () int digit = stack.pop (); if (digit 10) cout digit; else cout char (digit - 10 + A); cout endl;int main (void) try Stack stack; for (int

6、i = 0; ! stack.full (); +i) stack.push (i);/ stack.push (0); while (! stack.empty () cout stack.pop () endl;/ stack.pop (); catch (exception& ex) cout ex.what () endl; return -1; cout 输入一个整数: numb; cout 您想看几进制: base; cout 结果:; printb (numb, base); return 0;【代码:vi lstack.cpp】#include using namespace

7、std;/ 基于链式表的堆栈class Stack public: / 构造过程中初始化为空堆栈 Stack (void) : m_top (NULL) / 析构过程中销毁剩余的节点 Stack (void) for (Node* next; m_top; m_top = next) next = m_top-m_next; delete m_top; / 压入 void push (int data) /* Node* node = new Node; node-m_data = data; node-m_next = m_top; m_top = node;*/ m_top = new N

8、ode (data, m_top); / 弹出 int pop (void) if (empty () throw UnjjderFlow (); int data = m_top-m_data; Node* next = m_top-m_next; delete m_top; m_top = next; return data; / 判空 bool empty (void) const return ! m_top; private: / 下溢异常 class UnderFlow : public exception const char* what (void) const throw (

9、) return 堆栈下溢!; ; / 节点 class Node public: Node (int data = 0, Node* next = NULL) : m_data (data), m_next (next) int m_data; / 数据 Node* m_next; / 后指针 ; Node* m_top; / 栈顶;int main (void) try Stack stack; for (int i = 0; i 10; +i) stack.push (i); while (! stack.empty () cout stack.pop () endl;/ stack.p

10、op (); catch (exception& ex) cout ex.what () endl; return -1; return 0;*4、队列(vi q.cpp/q1.cpp)(1)基本特征:先进先出(2)基本操作:压入(push),弹出(pop)(3)实现要点:初始化空间,从前端(front)弹出,从后端(rear)压入,循环使用,判空判满【代码:vi q.cpp】#include #include lstack.husing namespace std;/ 基于堆栈的队列class Queue public: / 压入 void push (int data) m_i.push

11、(data); / 弹出 int pop (void) if (m_o.empty () if (m_i.empty () throw underflow_error(队列下溢!); while (! m_i.empty () m_o.push (m_i.pop (); return m_o.pop (); / 判空 bool empty (void) const return m_i.empty () & m_o.empty (); private: Stack m_i; / 输入栈 Stack m_o; / 输出栈;int main (void) try Queue queue; for

12、(int i = 0; i 10; +i) queue.push (i); cout queue.pop () endl; / 0 cout queue.pop () endl; / 1 cout queue.pop () endl; / 2 cout queue.pop () endl; / 3 cout queue.pop () endl; / 4 queue.push (10); queue.push (11); queue.push (12); while (! queue.empty () cout queue.pop () endl; catch (exception& ex) c

13、out ex.what () endl; return -1; return 0;【代码:vi q1.cpp】#include using namespace std;/ 基于顺序表的队列class Queuepublic: /构造过程中分配内存 Queue(size_t size=5):m_array(new intsize),m_size(size), m_rear(0),m_font(0),m_count(0) Queue(void) if(m_array) delete m_array; m_array=NULL; /压入 void push(int data) if(full() t

14、hrow overflow(); if(m_rear=m_size) m_rear=0; +m_count; m_arraym_rear+=data; /弹出 int pop(void) if(empty() throw underflow(); if(m_font=m_size) m_font=0; -m_count; return m_arraym_font+; /判空 bool empty()const return !m_count; /判满 bool full() return m_count=m_size; private: /上溢异常 class overflow:public

15、exception const char* what(void)const throw() return 队列上溢!; ; /下溢异常 class underflow:public exception const char* what(void)const throw() return 队列下溢!; ; int* m_array; size_t m_size;/容量 size_t m_rear; size_t m_font; size_t m_count;int main() try Queue queue; queue.push(10); queue.push(20); queue.push

16、(30); queue.push(40); queue.push(50); /queue.push(60); coutqueue.pop()endl;/10 queue.push(60); coutqueue.pop()endl;/20 queue.push(70); coutqueue.pop()endl;/30 coutqueue.pop()endl;/40 coutqueue.pop()endl;/50 coutqueue.pop()endl;/60 catch(exception& ex) coutex.what()endl; return -1; return 0;【思考题:如何用堆

17、栈来实现队列?(vi lstatic2.cpp)】#include/用堆栈来实现队列#includelstatic.husing namespace std;class Queuepublic: /压入 void push(int data) m_i.push(data); /弹出 int pop() if(m_o.empty() if(m_i.empty() throw underflow_error(队列下溢!); while(!m_i.empty() m_o.push(m_i.pop(); return m_o.pop(); /判空 bool empty()const return m_

18、i.empty()&m_o.empty(); private: stack m_i;/输入栈 stack m_o;/输出栈;int main() try Queue queue; for(int i=0;i10;+i) queue.push(i); coutqueue.pop()endl;/o coutqueue.pop()endl;/1 coutqueue.pop()endl;/2 coutqueue.pop()endl;/3 coutqueue.pop()endl;/4 coutqueue.pop()endl;/5 catch(exception& ex) coutex.what()end

19、l; 5、链表(1)基本特征:内存中不连续的节点序列,彼此之间通过指针相互关联,前指针(prev)指向前节点,后指针(next)指向后节点(2)基本特征:追加、插入、删除、遍历(3)实现要点:void 讲故事 (void) 从前有座山; 山里有个庙; 庙里有个老和尚; if (老和尚圆寂了) return; 讲故事 ();6、二叉树(vi bstree.cpp)(1)基本特征1)树型结构的最简模型,每个节点最多有两个子节点2)单根,除根节点外每个节点有且只有一个父节点,整棵树只有一个根节点3)递归结构,用递归处理二叉树问题可以简化算法(2)基本操作1)生成2)遍历D-L-R:前序遍历 L-D-

20、R:中序遍历 L-R-D:后序遍历有序二叉树(二叉搜索树):L=D=R(左小右大)50 70 20 60 40 30 10 90 80 50 / /- - 20| 70 / / 10 40 60 90 / / 30 80 / 10中序遍历:10 20 30 40 50 60 70 80 90【代码:vi bstree.cpp】includeusing namespace std;/有序二叉树(搜索二叉树)class Treepublic: /构造函数过程中初始化为空树 Tree(void):m_root(NULL),m_size(0) /析构过程中销毁剩余节点 Tree(void) clear

21、(); /插入数据 void insert(int date) insert(new Node(date),m_root); +m_size; /删除第一个匹配的数据,不存在返回false bool erase(int date) Node*& node=find(date,m_root); if(node) /左子树插入右子树 insert(node-m_left,node-m_right); Node* temp=node; /右提升 node=node-m_right; delete temp; -m_size; return true; return false; /删除所有匹配数据

22、void remove(int date) while(erase(date); /清空树 void clear(void) clear(m_root); m_size=0; /将所有的_old替换为_new void update(int _old,int _new) while(erase(_old) insert(_new); /判断date是否存在 bool find(int date) return find(date,m_root)!=NULL; /中序遍历 void travel() travel(m_root); coutm_date m_date) insert(node,t

23、ree-m_left); else insert(node,tree-m_right); /返回子树tree中值与date相匹配的节点的父节中指向该节点的成员变量的别名 Node*& find(int date,Node*& tree) if(!tree) return tree; else if(date=tree-m_date) return tree; else if(datem_date) return find(date,tree-m_left); else return find(date,tree-m_right); void clear(Node*& tree) if(tree

24、) clear(tree-m_left); clear(tree-m_right); delete tree; tree=NULL; void travel(Node* tree) if(tree) travel(tree-m_left); coutm_datem_right); size_t height(Node* tree) if(tree) size_t lh=height(tree-m_left); size_t rh=height(tree-m_right); return (lhrh?lh:rh)+1; return 0; Node* m_root;/树根 size_t m_size;/大小;int main() Tree tree; tree.insert(50); tree.insert(70); tree.insert(20); tree.insert(60); tree.insert(40); tree.insert(30); tree.insert(10);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1