免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 定义: 链式结构:The idea of a linked list is to augment every element of a list structure with a pointer giving the location of the next element in the list.Linked stack: a stack implemented by linked structure.Linked Queue: a queue implemented by linked structure.2、 实现: Linked Stack: *Node.h: *Node.h:#ifndef NODE_H#define NODE_H#include typedef double Node_entry;typedef Node_entry Stack_entry;struct NodeNode_entry entry;Node *next;Node()next = NULL;Node(Node_entry item, Node* add = NULL)entry = item;next = add;#endif*LinkedStack.h:class Stack public:/ Standard Stack methods Stack(); bool empty() const; Error_code push(const Stack_entry &item); Error_code pop(); Error_code top(Stack_entry &item) const;/ Safety features for linked structures Stack(); Stack(const Stack &original); void operator =(const Stack &original);protected: Node *top_node;LinkedStack.cpp:Error_code Stack:push(const Stack_entry &item) Node *new_top = new Node(item, top_node); if (new_top = NULL) return overflow; top_node = new_top; return success;Error_code Stack:pop() Node *old_top = top_node; if (top_node = NULL) return underflow; top_node = old_top-next; delete old_top; return success;Error_code Stack:push(Stack_entry item) Node new_top(item, top_node); top_node = new_top; return success;Stack:Stack() / Destructor while (!empty() pop();void Stack:operator = (const Stack &original) / Overload assignment Node *new_top, *new_copy, *original_node = original.top_node; if (original_node = NULL) new_top = NULL; else / Duplicate the linked nodes new_copy = new_top = new Node(original_node-entry); while (original_node-next != NULL) original_node = original_node-next; new_copy-next = new Node(original_node-entry); new_copy = new_copy-next; while (!empty() / Clean out old Stack entries pop(); top_node = new_top; / and replace them with new entries.Stack:Stack(const Stack &original) / copy constructor Node *new_copy, *original_node = original.top_node; if (original_node = NULL) top_node = NULL; else / Duplicate the linked nodes. top_node = new_copy = new Node(original_node-entry); while (original_node-next != NULL) original_node = original_node-next; new_copy-next = new Node(original_node-entry); new_copy = new_copy-next; Linked Queue: *Node.h:#ifndef NODE_H#define NODE_H#include typedef double Node_entry;typedef Node_entry Queue_entry;struct NodeNode_entry entry;Node *next;Node()next = NULL;Node(Node_entry item, Node* add = NULL)entry = item;next = add;#endif*LinkedQueue.h:#ifndef LINKED_QUEUE_H#define LINKED_QUEUE_Henum Error_codesuccess,overflow,underflow;#include Node.hclass LinkedQueuepublic:LinkedQueue();LinkedQueue(const LinkedQueue &original);LinkedQueue();bool empty() const;Error_code append(const Queue_entry &item);Error_code serve();Error_code retrieve(Queue_entry &item) const;void operator = (const LinkedQueue& original);protected:Node *front,*rear;#endif*LinkedQueue.cpp:#include LinkedQueue.hLinkedQueue:LinkedQueue()front = rear = NULL;LinkedQueue:LinkedQueue(const LinkedQueue &original)Node *new_front,*copy_front = original.front;if(copy_front != NULL)front = new_front = new Node(copy_front-entry);while (copy_front-next != NULL)copy_front = copy_front-next;new_front -next = new Node(copy_front-entry);new_front = new_front-next;rear = new_front;rear-next = NULL;elsefront = rear = NULL;LinkedQueue:LinkedQueue()while (!empty()serve();bool LinkedQueue:empty() constreturn(rear = NULL);Error_code LinkedQueue:append(const Queue_entry &item)Node *new_rear = new Node(item);if(new_rear = NULL)return overflow;else if(rear = NULL)front = rear = new_rear;elserear-next = new_rear;rear = new_rear;return success;Error_code LinkedQueue:retrieve(Queue_entry &item)constif(rear = NULL)return underflow;item = front-entry;return success;Error_code LinkedQueue:serve()if(front = NULL)return underflow;Node *old_front = front;front = old_
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 云南省昆明市西山区民中2026届物理高一第一学期期末质量跟踪监视试题含解析
- 排水板低温弯折性试验记录
- 土的液限(碟式仪法)试验记录
- 医院关于进一步规范各类讲座、论坛、报告会、研讨会审批流程的通知
- 高一数学上学期第一次月考(北师大版2019高效培优-强化卷)(全解全析)
- 浅谈《聊斋志异》中的花妖
- 建筑工程管理中供应链管理的关键问题探讨
- 语言学类论文英语标题信息结构的对比研究
- 略论朱熹的经典诠释思想
- 论文的格式字体要求
- 企业财务制度规范范本合集
- 2025年广东省继续教育公需课《人工智能赋能制造业高质量发展》满分答案
- 学校管理经验介绍材料
- 云南绿色能源产业集团笔试题库
- 管道安全护理课件
- 因私出国(境)管理有关政策的解读课件
- 线性系统理论-郑大钟(第二版)课件
- 音乐课件《鸿雁》(公开课)
- 禾川x3系列伺服说明书
- 拆除工程检验批质量检验记录
- 原发性醛固酮增多症
评论
0/150
提交评论