Microsoft PowerPoint - Lecture3.ppt

Similar documents
数据结构 Data Structure

ebook39-5

ebook39-6

Microsoft PowerPoint - Model Checking a Lazy Concurrent List-Based Set Algorithm.ppt [Compatibility Mode]

Strings

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Open topic Bellman-Ford算法与负环

概述

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1

Fuzzy Highlight.ppt

untitled

C++ 程式設計

Microsoft PowerPoint - string_kruse [兼容模式]

Microsoft Word - CX VMCO 3 easy step v1.doc

TX-NR3030_BAS_Cs_ indd

FY.DOC

<4D F736F F D20B2F8A74AA4AF5FA578C657A175BCC6A6ECB6D7AC79A176BB50A46AB3B0A175A454BAF4A658A440A176AC46B5A6A641B1B4>

<4D F736F F D C4EAC0EDB9A4C0E04142BCB6D4C4B6C1C5D0B6CFC0FDCCE2BEABD1A15F325F2E646F63>

國立中山大學學位論文典藏.PDF

Chapter12 Derived Classes

新版 明解C++入門編

VASP应用运行优化

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

Agenda 大 纲 FIFO data-structures 先 进 先 出 ( FIFO) 数 据 结 构 (= First In First Out) Heap data-structures 堆 结 构 Non-static methods 非 静 态 方 法 (= object metho

Microsoft Word - 物件導向編程精要.doc

WinMDI 28

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

全国计算机技术与软件专业技术资格(水平)考试

软件测试(TA07)第一学期考试


Chapter 9: Objects and Classes

Microsoft Word - ch04三校.doc

Microsoft Word - 01.DOC

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

K301Q-D VRT中英文说明书141009

untitled

数据结构 Data Structure

c_cpp

提纲 1 2 OS Examples for 3

Microsoft Word - 11.doc

Microsoft PowerPoint - L17_Inheritance_v4.pptx

穨control.PDF

Microsoft Word - Learn Objective-C.doc

提问袁小兵:

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

A Community Guide to Environmental Health

中国科学技术大学学位论文模板示例文档

<4D F736F F D205F FB942A5CEA668B443C5E9BB73A740B5D8A4E5B8C9A552B1D0A7F75FA6BFB1A4ACFC2E646F63>

高中英文科教師甄試心得

Explain each of the following terms. (12%) (a) O(n 2 ) (b) protected in C++ language (c) sparse matrix 7. Write

概述

Microsoft Word - 第四組心得.doc

coverage2.ppt

C o n t e n t s Acceptance Allow Love Apologize Archangel Metatron Archangel Michael Ask for

96 7 () 124

第1章 簡介

演算法導入、ソート、データ構造、ハッシュ

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

ENGG1410-F Tutorial 6

2

Journal of Hangzhou Normal University Humanities and Social Sciences No. 6 Nov ! / % & 2 PP P. 9

Chn 116 Neh.d.01.nis

2.3 链表

Microsoft Word - template.doc

<4D F736F F D20B5DAC8FDB7BDBE57C9CFD6A7B8B6D6AEB7A8C2C98696EE7DCCBDBEBF2E646F63>

CC213

Microsoft Word - data_mid1611_and_sol.docx

Microsoft PowerPoint - STU_EC_Ch08.ppt

声 明 本 人 郑 重 声 明 : 此 处 所 提 交 的 硕 士 学 位 论 文 基 于 等 级 工 鉴 定 的 远 程 考 试 系 统 客 户 端 开 发 与 实 现, 是 本 人 在 中 国 科 学 技 术 大 学 攻 读 硕 士 学 位 期 间, 在 导 师 指 导 下 进 行 的 研 究

Microsoft Word doc

四川省普通高等学校

27 :OPC 45 [4] (Automation Interface Standard), (Costom Interface Standard), OPC 2,,, VB Delphi OPC, OPC C++, OPC OPC OPC, [1] 1 OPC 1.1 OPC OPC(OLE f

ebook140-9

, 7, Windows,,,, : ,,,, ;,, ( CIP) /,,. : ;, ( 21 ) ISBN : -. TP CIP ( 2005) 1

國立中山大學學位論文典藏.PDF

LEETCODE leetcode.com 一 个 在 线 编 程 网 站, 收 集 了 IT 公 司 的 面 试 题, 包 括 算 法, 数 据 库 和 shell 算 法 题 支 持 多 种 语 言, 包 括 C, C++, Java, Python 等 2015 年 3 月 份 加 入 了 R

untitled

mvc

untitled

Logitech Wireless Combo MK45 English

Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies to all d

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

從篤加有二「區」談當代平埔文化復振現相

ebook14-4

PowerPoint Presentation

Windows XP

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

Microsoft Word - 11月電子報1130.doc

untitled

國 立 政 治 大 學 教 育 學 系 2016 新 生 入 學 手 冊 目 錄 表 11 國 立 政 治 大 學 教 育 學 系 博 士 班 資 格 考 試 抵 免 申 請 表 論 文 題 目 申 報 暨 指 導 教 授 表 12 國 立 政 治 大 學 碩 博 士 班 論

穨6街舞對抗中正紀念堂_林伯勳張金鶚_.PDF

Microsoft Word - 口試本封面.doc

BC04 Module_antenna__ doc

UTI (Urinary Tract Infection) - Traditional Chinese

東莞工商總會劉百樂中學

C/C++ 语言 - 循环

Linked Lists

Transcription:

Chap 4. Links, Stacks and Queue 1

Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: <a 0, a 1,, a n-1 > What operations should we implement? 2

List Implementation Concepts Our list implementation will support the concept of a current position. We will do this by defining the list in terms of left and right partitions. Either or both partitions may be empty. Partitions are separated by the fence. <20, 23 12, 15> 3

List ADT template <class Elem> class List { public: virtual void clear() = 0; virtual bool insert(const Elem&) = 0; virtual bool append(const Elem&) = 0; virtual bool remove(elem&) = 0; virtual void setstart() = 0; virtual void setend() = 0; virtual void prev() = 0; virtual void next() = 0; 4

List ADT (cont) virtual int leftlength() const = 0; virtual int rightlength() const = 0; virtual bool setpos(int pos) = 0; virtual bool getvalue(elem&) const = 0; virtual void print() const = 0; }; 5

List ADT Examples List: <12 32, 15> MyList.insert(99); Result: <12 99, 32, 15> Iterate through the whole list: for (MyList.setStart(); MyList.getValue(it); MyList.next()) DoSomething(it); 6

List Find Function // Return true iff K is in list bool find(list<int>& L, int K) { int it; for (L.setStart(); L.getValue(it); L.next()) if (K == it) return true; // Found it return false; // Not found } 7

Array-Based List Insert 8

Array-Based List Class (1) template <class Elem> // Array-based list class AList : public List<Elem> { private: int maxsize; // Maximum size of list int listsize; // Actual elem count int fence; // Position of fence Elem* listarray; // Array holding list public: AList(int size=defaultlistsize) { maxsize = size; listsize = fence = 0; listarray = new Elem[maxSize]; } 9

Array-Based List Class (2) ~AList() { delete [] listarray; } void clear() { delete [] listarray; listsize = fence = 0; listarray = new Elem[maxSize]; } void setstart() { fence = 0; } void setend() { fence = listsize; } void prev() { if (fence!= 0) fence--; } void next() { if (fence <= listsize) fence++; } int leftlength() const { return fence; } int rightlength() const { return listsize - fence; } 10

Array-Based List Class (3) bool setpos(int pos) { if ((pos >= 0) && (pos <= listsize)) fence = pos; return (pos >= 0) && (pos <= listsize); } bool getvalue(elem& it) const { if (rightlength() == 0) return false; else { it = listarray[fence]; return true; } } 11

Insert // Insert at front of right partition template <class Elem> bool AList<Elem>::insert(const Elem& item) { if (listsize == maxsize) return false; for(int i=listsize; i>fence; i--) // Shift Elems up to make room listarray[i] = listarray[i-1]; listarray[fence] = item; listsize++; // Increment list size return true; } 12

Append // Append Elem to end of the list template <class Elem> bool AList<Elem>::append(const Elem& item) { if (listsize == maxsize) return false; listarray[listsize++] = item; return true; } 13

Remove // Remove and return first Elem in right // partition template <class Elem> bool AList<Elem>::remove(Elem& it) { if (rightlength() == 0) return false; it = listarray[fence]; // Copy Elem for(int i=fence; i<listsize-1; i++) // Shift them down listarray[i] = listarray[i+1]; listsize--; // Decrement size return true; } 14

Link Class Dynamic allocation of new list elements. // Singly-linked list node template <class Elem> class Link { public: Elem element; // Value for this node Link *next; // Pointer to next node Link(const Elem& elemval, Link* nextval =NULL) { element = elemval; next = nextval; } Link(Link* nextval =NULL) { next = nextval; } }; 15

Linked List Position (1) 16

Linked List Position (2) 17

Linked List Class (1) / Linked list implementation template <class Elem> class LList: public List<Elem> { private: Link<Elem>* head; // Point to list header Link<Elem>* tail; // Pointer to last Elem Link<Elem>* fence;// Last element on left int leftcnt; // Size of left int rightcnt; // Size of right void init() { // Intialization routine fence = tail = head = new Link<Elem>; leftcnt = rightcnt = 0; } 18

Linked List Class (2) void removeall() { // Return link nodes to free store while(head!= NULL) { fence = head; head = head->next; delete fence; } } public: LList(int size=defaultlistsize) { init(); } ~LList() { removeall(); } // Destructor void clear() { removeall(); init(); } 19

Linked List Class (3) void setstart() { fence = head; rightcnt += leftcnt; leftcnt = 0; } void setend() { fence = tail; leftcnt += rightcnt; rightcnt = 0; } void next() { // Don't move fence if right empty if (fence!= tail) { fence = fence->next; rightcnt--; leftcnt++; } } int leftlength() const { return leftcnt; } int rightlength() const { return rightcnt; } bool getvalue(elem& it) const { if(rightlength() == 0) return false; it = fence->next->element; return true; } 20

Insertion 21

Insert/Append // Insert at front of right partition template <class Elem> bool LList<Elem>::insert(const Elem& item) { fence->next = new Link<Elem>(item, fence->next); if (tail == fence) tail = fence->next; rightcnt++; return true;} // Append Elem to end of the list template <class Elem> bool LList<Elem>::append(const Elem& item) { tail = tail->next = new Link<Elem>(item, NULL); rightcnt++; return true;} 22

Removal 23

Remove // Remove and return first Elem in right // partition template <class Elem> bool LList<Elem>::remove(Elem& it) { if (fence->next == NULL) return false; it = fence->next->element; // Remember val // Remember link node Link<Elem>* ltemp = fence->next; fence->next = ltemp->next; // Remove if (tail == ltemp) // Reset tail tail = fence; delete ltemp; // Reclaim space rightcnt--; return true; } 24

Prev // Move fence one step left; // no change if left is empty template <class Elem> void LList<Elem>::prev() { Link<Elem>* temp = head; if (fence == head) return; // No prev Elem while (temp->next!=fence) temp=temp->next; fence = temp; leftcnt--; rightcnt++; } 25

Setpos // Set the size of left partition to pos template <class Elem> bool LList<Elem>::setPos(int pos) { if ((pos < 0) (pos > rightcnt+leftcnt)) return false; fence = head; for(int i=0; i<pos; i++) fence = fence->next; return true; } 26

Comparison of Implementations Array-Based Lists: Insertion and deletion are (n). Prev and direct access are (1). Array must be allocated in advance. No overhead if all array positions are full. Linked Lists: Insertion and deletion are (1). Prev and direct access are (n). Space grows with number of elements. Every element requires overhead. 27

Space Comparison Break-even point: DE = n(p + E); n = DE P + E E: Space for data value. P: Space for pointer. D: Number of elements in array. 28

Freelists System new and delete are slow. // Singly-linked list node with freelist template <class Elem> class Link { private: static Link<Elem>* freelist; // Head public: Elem element; // Value for this node Link* next; // Point to next node Link(const Elem& elemval, Link* nextval =NULL) { element = elemval; next = nextval; } Link(Link* nextval =NULL) {next=nextval;} void* operator new(size_t); // Overload void operator delete(void*); // Overload }; 29

Freelists (2) template <class Elem> Link<Elem>* Link<Elem>::freelist = NULL; template <class Elem> // Overload for new void* Link<Elem>::operator new(size_t) { if (freelist == NULL) return ::new Link; Link<Elem>* temp = freelist; // Reuse freelist = freelist->next; return temp; // Return the link } template <class Elem> // Overload delete void Link<Elem>::operator delete(void* ptr){ ((Link<Elem>*)ptr)->next = freelist; freelist = (Link<Elem>*)ptr; } 30

Doubly Linked Lists Simplify insertion and deletion: Add a prev pointer. // Doubly-linked list link node template <class Elem> class Link { public: Elem element; // Value for this node Link *next; // Pointer to next node Link *prev; // Pointer to previous node Link(const Elem& e, Link* prevp =NULL, Link* nextp =NULL) { element=e; prev=prevp; next=nextp; } Link(Link* prevp =NULL, Link* nextp =NULL) { prev = prevp; next = nextp; } }; 31

Doubly Linked Lists 32

Doubly Linked Insert 33

Doubly Linked Insert // Insert at front of right partition template <class Elem> bool LList<Elem>::insert(const Elem& item) { fence->next = new Link<Elem>(item, fence, fence->next); if (fence->next->next!= NULL) fence->next->next->prev = fence->next; if (tail == fence) // Appending new Elem tail = fence->next; // so set tail rightcnt++; // Added to right return true; } 34

Doubly Linked Remove 35

Doubly Linked Remove // Remove, return first Elem in right part template <class Elem> bool LList<Elem>::remove(Elem& it) { if (fence->next == NULL) return false; it = fence->next->element; Link<Elem>* ltemp = fence->next; if (ltemp->next!= NULL) ltemp->next->prev = fence; else tail = fence; // Reset tail fence->next = ltemp->next; // Remove delete ltemp; // Reclaim space rightcnt--; // Removed from right return true; } 36

Dictionary Often want to insert records, delete records, search for records. Required concepts: Search key: Describe what we are looking for Key comparison Equality: sequential search Relative order: sorting Record comparison 37

Comparator Class How do we generalize comparison? Use ==, <=, >=: Disastrous Overload ==, <=, >=: Disastrous Define a function with a standard name Implied obligation Breaks down with multiple key fields/indices for same object Pass in a function Explicit obligation Function parameter Template parameter 38

Comparator Example class intintcompare { public: static bool lt(int x, int y) { return x < y; } static bool eq(int x, int y) { return x == y; } static bool gt(int x, int y) { return x > y; } }; 39

Comparator Example (2) class PayRoll { public: int ID; char* name; }; class IDCompare { public: static bool lt(payroll& x, Payroll& y) { return x.id < y.id; } }; class NameCompare { public: static bool lt(payroll& x, Payroll& y) { return strcmp(x.name, y.name) < 0; } }; 40

Dictionary ADT // The Dictionary abstract class. template <class Key, class Elem, class KEComp, class EEComp> class Dictionary { public: virtual void clear() = 0; virtual bool insert(const Elem&) = 0; virtual bool remove(const Key&, Elem&) = 0; virtual bool removeany(elem&) = 0; virtual bool find(const Key&, Elem&) const = 0; virtual int size() = 0; }; 41

Unsorted List Dictionary template <class Key, class Elem, class KEComp, class EEComp> class UALdict : public Dictionary<Key,Elem,KEComp,EEComp> { private: AList<Elem>* list; public: bool remove(const Key& K, Elem& e) { for(list->setstart(); list->getvalue(e); list->next()) if (KEComp::eq(K, e)) { list->remove(e); return true; } return false; } }; 42

Stacks LIFO: Last In, First Out. Restricted form of list: Insert and remove only at front of list. Notation: Insert: PUSH Remove: POP The accessible element is called TOP. 43

Stack ADT // Stack abtract class template <class Elem> class Stack { public: // Reinitialize the stack virtual void clear() = 0; // Push an element onto the top of the stack. virtual bool push(const Elem&) = 0; // Remove the element at the top of the stack. virtual bool pop(elem&) = 0; // Get a copy of the top element in the stack virtual bool topvalue(elem&) const = 0; // Return the number of elements in the stack. virtual int length() const = 0; }; 44

Array-Based Stack // Array-based stack implementation private: int size; // Maximum size of stack int top; // Index for top element Elem *listarray; // Array holding elements Issues: Which end is the top? Where does top point to? What is the cost of the operations? 45

Linked Stack // Linked stack implementation private: Link<Elem>* top; // Pointer to first elem int size; // Count number of elems What is the cost of the operations? How do space requirements compare to the array-based stack implementation? 46

examples of stack 47

栈的应用举例 1: 数制转换十进制数 N 和其他 d 进制数的转换是计算 机实现计算的基本问题, 其解决方法很多, 其中一个简单算法基于下列原理 : 迭代 N = (N div d) d + N mod d ( 其中 :div 为整除运算,mod 为求余运算 ) 例如 :(1348)10 = (2504)8, 其运算过程如下 : N N div 8 N mod 8 1348 168 4 168 21 0 21 2 5 2 0 2 48

49

伪代码表示 : void conversion () { InitStack( ); // 构造空栈 cin >> N; // 输入一个十进制数 while(n) { Push(N % 8); // " 余数 " 入栈 N = N / 8; // 非零 " 商 " 继续运算 } // while while (!StackEmpty) { // 和 " 求余 " 所得相逆的顺序输出八进制的各位数 Pop(e); cout << e; } // while } // conversion 50

栈的应用举例 2: 括号匹配的检验 处理的括号包括 :{ } [ ] ( ) 正确的格式 : { [ ] ( ) } 或 [ ( { } [ ] ) ] 不正确的格式 : [ ( ] ) 或 { [ ( ] } ) 或 ( ( ) ] ) 则检验括号是否匹配的方法可用 期待的急迫程度 这个概念来描述 51

例如 : 考虑下列括号序列 : [ ( [ ] { } ) ] 1 2 3 4 5 6 7 8 分析可能出现的不匹配的情况 : 到来的右括号并非是所 期待 的 ; 到来的是 不速之客 ; 直到结束, 也没有到来所 期待 的右括号 52

算法的设计思想 : 1) 凡出现左括号, 则进栈 ; 2) 凡出现右括号, 首先检查栈是否空若栈空, 则表明该 右括号 多余, 否则和栈顶元素比较, 若相匹配, 则 左括号出栈, 否则表明不匹配 3) 表达式检验结束时, 若栈空, 则表明表达式中匹配正确, 否则表明 左括号 有余 53

代码演示 s3_2/alg.h VC 调试方法 54

栈的应用举例 3: 迷宫求解算法 迷宫求解问题 : 求迷宫中从入口到出口的所有路径是一个经典的程序设计问题 由于计算机解迷宫时, 通常用的是 穷举求解 的方法, 即从入口出发, 顺某一方向向前探索, 若能走通, 则继续往前走 ; 否则沿原路退回, 换一个方向再继续探索, 直至所有可能的通路都探索到为止 为了保证在任何位置上都能沿原路退回, 显然需要用一个后进先出的结构来保存从入口到当前位置的路径 因此, 在求迷宫通路的算法中应用 栈 也就是自然而然 55 的事了

迷宫的表示 56

解迷宫 57

Queues FIFO: First in, First Out Restricted form of list: Insert at one end, remove from the other. Notation: Insert: Enqueue Delete: Dequeue First element: Front Last element: Rear 58

Queue Implementation (1) 59

Queue Implementation (2) 60

循环队列 (Circular Queue) 存放队列的数组被当作首尾相接的表 队头 队尾指针加 1 时从 maxsize -1 直接进 到 0,, 可用取模 ( 余数 ) 运算实现 队头指针加 1: front=(front+1) % maxsize 队尾指针加 1: rear = (rear+1) % maxsize 队列初始化 :front: = rear = 0 61

5 4 6 7 front rear 3 2 空队列 0 1 5 4 6 7 3 2 A 进队 A front 0 1 rear 5 4 rear 6 7 队空条件 :rear = = front C 3 B 2 A 0 1 B, C 进队 front 6 7 6 7 6 7 5 0 5 0 5 0 4 rear C 3 B 2 A 退队 A 1 front 4 rear C 3 B 2 B 退队 1 front 4 rear front C 3 2 C 退队 1 62

5 4 6 7 front rear 3 2 空队列 0 1 5 4 6 7 3 2 A 进队 A front 0 1 rear 5 4 rear 6 7 队满条件 :rear = = front C 3 B 2 A 0 1 B, C 进队 front 6 7 5 0 4 rear C 3 B 2 A 退队 A 1 front 5 4 rear 6 7 C 3 B 2 B 退队 0 1 front 5 4 6 7 E F G H D I C J 3 2 0 1 rear front D,E,F,G,H,I,J 进队 63

解决 : 设置计数器 count,, 记录队列中的元素个数 设置标志表明当前队列的空满情况 在数组中始终留空一个单元不用 即 front 所指的单元始终不用 队列初始化 :front: = rear = 0 队空条件 :front: = = rear 队满条件 :(rear+1)%maxsize: = = front( ( 当队尾再加 1 追上队头时, 队列满 ) 64

5 4 5 4 6 7 front rear 3 2 空队列 6 7 0 1 E F G H D I C 3 2 0 5 4 rear front D,E,F,G,H,I 进队 6 7 C B rear 3 2 front rear B 退队 0 1 5 4 front 6 7 队空 C 3 2 C 退队 队空条件 :rear = = front 队满队满条件 : 1 (rear+1)%maxsize= =front 65 0 1