链式栈和队列.doc_第1页
链式栈和队列.doc_第2页
链式栈和队列.doc_第3页
链式栈和队列.doc_第4页
免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论