4 -1 Chapter 4 Queues and Lists. 4 -2 Queue First-in first-out (FIFO) First come first serve (FCFS) 2 ends: Data are inserted to one end (rear) and removed.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

EENG212 Algorithms and Data Structures
DATA STRUCTURES USING C++ Chapter 5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Ceng-112 Data Structures I Chapter 5 Queues.
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
Homework - Chapter 03 Solution.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Queues and Lists Alexander G. Chefranov CMPE-231 Spring
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
8.1 何謂高度平衡二元搜尋樹 8.2 高度平衡二元搜尋樹的加入 8.3 高度平衡二元搜尋樹的刪除
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
: Tight words ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: : Tight Words 解題者:鐘緯駿、林一帆 解題日期: 2006 年 03 月 14 日 題意: 給定數字 k 與 n (0 ≦ k.
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
: Problem A : MiniMice ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11411: Problem A : MiniMice 解題者:李重儀 解題日期: 2008 年 9 月 3 日 題意:簡單的說,題目中每一隻老鼠有一個編號.
資料結構實習-一 參數傳遞.
: Problem G e-Coins ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10306: Problem G e-Coins 解題者:陳瀅文 解題日期: 2006 年 5 月 2 日 題意:給定一個正整數 S (0
: A-Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10930: A-Sequence 解題者:陳盈村 解題日期: 2008 年 5 月 30 日 題意: A-Sequence 需符合以下的條件, 1 ≤ a.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 10 m-way 搜尋樹與B-Tree
Extreme Discrete Summation ★★★★☆ 題組: Contest Archive with Online Judge 題號: Extreme Discrete Summation 解題者:蔡宗翰 解題日期: 2008 年 10 月 13 日.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
中序轉後序 藉由由左向右掃瞄中序運算式產生後序運算式,遇到 運算元就直接輸出,遇到運算符號則先存入堆疊,將 優先權較高者輸出。 範例: a + b * c TokenStack [0] [1] [2] topoutput aa ++0a b+0ab *+ *1ab c+ *1abc eosabc*+
資料結構實習-六.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
質數 (Prime) 相關問題 Introducer: Hsing-Yen Ann Date: 2009/05/13.
Data Structures Using C++ 2E
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Linked List Spring 2013Programming and Data Structure1.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Queues EENG212 Algorithm And Data Structures. DEFINITION OF QUEUE A Queue is an ordered collection of items from which items may be deleted at one end.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
5 -1 Chapter 5 Trees Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
第二章 线性表 第二节 线性链表 5. 线性表的链式表示 顺序表的优点是可以随机选取表中元素 缺点是插入删除操作复杂。 用指针将互不相连的内存结点串成的 线性表叫线性链表。 结点 node 由一个数据元素域,一个或几个 指针域组成。单链表的结点只有一个指针域。
Data Structures -3 rd exam- 授課教師 : 李錫智 教授. 1.[10] Please answer the following questions about queue: [ 2] What is the response of the statement: “(new.
Group 2 Block 胡貴蓉 Project 2 JMVC code tracing.
LINKED LISTS.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
STACKS & QUEUES for CLASS XII ( C++).
Queues Chapter 4.
Data Structure #1: The Array
CC 215 Data Structures Queue ADT
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Algorithms and Data Structures
Chapter 16-2 Linked Structures
EENG 212 ALGORITHMS And DATA STRUCTURES
6.1.3 Graph representation.
Presentation transcript:

4 -1 Chapter 4 Queues and Lists

4 -2 Queue First-in first-out (FIFO) First come first serve (FCFS) 2 ends: Data are inserted to one end (rear) and removed from the other end (front).

4 -3 Linear queue ( 和課本稍不同 ) A B A initialinsert(A)insert(B) rear front rear front rear front B remove rear front C B Insert(C) rear front E D someday rear front insert(F): overflow, but there are still some empty locations in the lower part.

4 -4 Abstract data type--queue abstract typedef > QUEUE(eltype); abstract empty(que) QUEUE(eltype) que; postcondition empty == (len(que) == 0); abstract eltype remove(que) QUEUE(eltype) que; precondition empty(que) == FALSE; postcondition remove == first(que’); que == sub(que’, 1, len(que’) –1); abstract insert(que, elt) QUEUE(eltype) que; eltype elt; postcondition que == que’ + ;

4 -5 Circular queue E 3D 2C 1 0 4E 3D 2C 1 0F front rear initial empty queue 之條件 : front = rear que.items que.rear = 4 que.front = 1 que.rear = 0 (a)(b)

4 -6 Full circular queue 4E 3D 2C 1G 0F que.items (c) que.front = que.rear = 1 此時分不清是 full 或是 empty 故若有 n 個記憶容量, 則只用其中 n-1 個

4 -7 #define MAXQUEUE 100 struct queue{ int items[MAXQUEUE]; int front, rear; }; struct queue que; // initialization que.front = que.rear = MAXQUEUE-1; Implementation of circular queues

4 -8 int empty(struct queue *pq) { return ((pq->front == pq->rear) ? TRUE : FALSE); } /* end empty */ Checking if empty

4 -9 int remove(struct queue *pq) { if (empty(pq)){ printf(“queue underflow”); exit(1); } /* end if */ if (pq->front == MAXQUEUE –1) pq->front = 0; else (pq->front)++; return (pq->items[pq->front]); } /* end remove */ Removing the front node

4 -10 void insert(struct queue *pq, int x) { /* make room for new element */ if (pq->rear == MAXQUEUE –1) pq->rear = 0; else (pq->rear)++; /* check for overflow */ if (pq->rear == pq->front){ printf(“queue overflow”); exit(1); } /* end if */ pq->items[pq->rear] = x; return; } /* end insert */ Appending a node

4 -11 Priority queue We can insert new elements to a priority queue quickly. Ascending priority queue: easy to remove the smallest item Descending priority queue : easy to remove the largest item 最好使用 heap 做為 priority queue. 每個 stack 或 queue 個別使用固定的 array, 浪 費空間 多個 stack 或 queue 共用一個 array, 較節省空 間

4 -12 Linear linked list DABC 561 information next address(pointer) 以 -1 代表結尾 (null pointer) list = 2 header, external pointer null list node info next **

4 -13 Operation on the first node A BCnull list (1) adding ‘F’ to the front of the list ABCnull list F p = getnode(); info(p) = ‘F’; next(p) = list; list = p; (2) removing the first node of the list BCnull list **

4 -14 A BCnull s (top) Linked implementation of stacks push(s, x): similar to adding an element to the front **

4 -15 x = pop(s): similar to removing the first node if (empty(s)) { printf(“stack underflow”); exit(1); } else{ p = s; s = next(p); x = info(p); freenode(p); } /* end if */ p = list; list = next(p); x = info(p); freenode(p);

4 -16 Available lists p = getnode(): if (avail == null){ rintf(“overflow”); exit(1); } p = avail; avail = next(avail); freenode(p): next(p) = avail; avail = p; null avail...

4 -17 Linked implementation of queues empty queue: front = rear = null x = remove(que): if (empty(que)){ printf(“queue underflow”); exit(1); } p = que.front; x = info(p); que.front = next(p); if (que.front == null) que.rear = null; freenode(p); return(x); B CDnull front A rear front

4 -18 p rear x Appending a node p = getnode(); info(p) = x; next(p) = null; if (que.rear == null) // 此時 front 也是 null que.front = p; else next(que.rear) = p; que.rear = p;

4 -19 The linked list as a data structure insafter(p, x): Insert x into a list after a node p. A BC AB C x ** q p

4 -20 delafter(p, x): Delete the node following p and assign its contents to x. A BC p ABC p q **

4 -21 Examples of list operations (1) Delete all occurrences of the number 4: q = null; p = list; while (p != null){ if (info(p) == 4) if (q = null){ /* remove first node of the list */ x = pop(list); p = list; } else{ /* delete the node after q and move up p */ p = next(p); delafter(q, x); } /* end if */

4 -22 Examples of list operations (2) else{ /* continue traversing the list */ q = p; p = next(p); } /* end if */ } /* end while */

4 -23 q = null; for (p = list; p != null && x > info(p); p = next(p)) q = p; /* at this point, a node containing */ /* x must be inserted */ if (q == null) /* insert x at the */ /*head of the list */ push(list, x); else insafter(q, x); q X=8 p 前面較小 後面較大 Insertion of a sorted list qp

4 -24 Header nodes ( 不是一般 node, 而是特殊用途的 node) 2 ACnull (1) list 此 list 有 2 個 element (2) list A746 B841K321null machine A746 是由 parts B841, K321 所組成 (3) list ACnull 如果 information field 可以存放 pointer, 則可以做為 queue 之 rear.

4 -25 Array implementation of lists (1) e.g. an array of nodes containing 4 linked lists list4 = list2 = list3 = list1 = info next

4 -26 Initialization of the available list #define NUMNODES 500 struct nodetype{ int info, next; }; struct nodetype node[NUMNODES]; initialization of the available list: avail = 0; for (i = 0; i < NUMNODES-1; i++) node[i].next = i+1; node[NUMNODES-1].next = -1;

4 -27 Allocating a node from the available list int getnode(void) { int p; if (avail == -1){ printf("overflow\n"); exit(1); } p = avail; avail = node[avail].next; return(p); } /* end getnode */

4 -28 Returning a node to the available list void freenode(int p) { node[p].next = avail; avail = p; return; } /* end freenode */

4 -29 insafter(p, x) Insert an item x into a list after a node p void insafter(int p, int x) { int q; if (p == -1){ printf("void insertion\n"); return; } q = getnode(); node[q].info = x; node[q].next = node[p].next; node[p].next = q; return; } /* end insafter */ x p q

4 -30 delafter(p, px) Delete the node following p and store its value in x (*px). void delafter(int p, int *px) { int q; if ((p == -1) || (node[p].next == -1)){ printf("void deletion\n"); return; } q = node[p].next; *px = node[q].info; node[p].next = node[q].next; freenode(q); return; } /* end delafter */ A BC p q

4 -31 Allocating and freeing dynamic variables int *p, *q; int x; p = (int *)malloc(sizeof(int)); *p = 3; q = p; printf("%d %d \n", *p, *q); x = 7; 3 PqPq 77 PqPq x (a)(b) output: **

4 -32 *q = x; printf("%d %d \n", *p, *q); p = (int *) malloc(sizeof(int)); *p = 5; printf("%d %d \n", *p, *q); 77 p q x 577 p q x (c)(d) output: **

4 -33 p = (int *)malloc(sizeof(int)); *p = 5; q = (int *)malloc(sizeof(int)); *q = 8; free(p); p = q; q = (int *)malloc(sizeof(int)); *q = 6; printf("%d %d \n", *p, *q); output: 58 p q (a) 8 pq (b) 8 p (c) q 68 qp (d) **

4 -34 e.g. p = (int *)malloc(sizeof(int)); *p = 3; p = (int *)malloc(sizeof(int)); *p = 7; The first copy of *p is lost since its address was not saved.

4 -35 Linked lists with dynamic variables struct node{ int info; struct node *next; }; typedef struct node *NODEPTR; NODEPTR getnode(void) { NODEPTR p; p = (NODEPTR)malloc(sizeof(struct node)); return(p); } void freenode(NODEPTR p) { free(p); }

4 -36 getnode and freenode can be simply replaced by: NODEPTR p; p = (NODEPTR)malloc(sizeof(struct node)); and free(p); Memory allocation in C

4 -37 insafter(p, x) Insert an item x into a list after a node p void insafter(NODEPTR p, int x) { NODEPTR q; if (p == NULL){ printf("void insertion\n"); exit(1); } q = getnode(); q->info = x; q->next = p->next; p->next = q; } /* end insafter */ x p q

4 -38 delafter(p, px) Delete the node following p and store its value in x (*px) void delafter(NODEPTR p, int *px) { NODEPTR q; if ((p == NULL) || (p->next == NULL)){ printf("void deletion\n"); exit(1); } q = p->next; *px = q->info; p->next = q->next; freenode(q); } /* end delafter */ p q

4 -39 A queue represented as a linear list in C Array Implementation struct queue{ int front, rear; }; struct queue que; int empty(struct queue *pq) { return((pq->front == -1) ? TRUE : FALSE); } /* end empty */ Dynamic Implementation struct queue{ NODEPTR front, rear; }; struct queue que; int empty(struct queue *pq) { return((pq->front == NULL) ? TRUE : FALSE); } /* end empty */ null rear front

4 -40 empty queue : front = rear = null insert(q, x): rear x p void insert(struct queue *pq, int x) { int p; p = getnode(); node[p].info = x; node[p].next = -1; if (pq->rear == -1) pq->front = p; else node[pq->rear].next = p; pq->rear =p; } /* end insert */ Appending a node

4 -41 void insert(struct queue *pq, int x) { NODEPTR p; p = getnode(); p->info = x; p->next = NULL; if (pq->rear == NULL) pq->front = p; else (pq->rear->next = p; pq->rear =p; } /* end insert */

4 -42 remove(q): front int remove(struct queue *pq) { int p, x; if (empty(pq)){ printf("queue underflow\n"); exit(1); } p = pq->front; x = node[p].info; pq->front = node[p].next; if (pq->front == -1) pq->rear = -1; freenode(p); return(x); } /* end remove */ int remove(struct queue *pq) { NODEPTR p; int x; if (empty(pq)){ printf("queue underflow\n"); exit(1); } p = pq->front; x = p->info; pq->front = p->next; if (pq->front == NULL) pq->rear = NULL; freenode(p); return(x); } /* end remove */ Removing the front node

4 -43 Insertion of a sorted list void place(NODEPTR *plist, int x) { NODEPTR p, q; q = NULL; for (p = *plist; p!= NULL && x > p->info; p = p->next) q = p; if (q == NULL) /* insert x at the head of the list */ push(plist, x); else insafter(q, x); } /* end place */ q P pq

4 -44 Dynamic and array implementation of lists disadvantage of dynamic: need more time to allocate and free storage advantage of dynamic: storage is allocated when needed no data type constraint need not compute address array: 與 dynamic 相反

4 -45 Circular lists list Last Node First Node The external pointer should point to the last node of a circular list.

4 -46 Representing a stack as a circular list A BC stack int empty(NODEPTR *pstack) { return((*pstack == NULL) ? TRUE : FALSE); } /* end empty */

4 -47 void push(NODEPTR *pstack, int x) { NODEPTR p; p = getnode(); p->info = x; if (empty(pstack) == TRUE) *pstack = p; else p->next = (*pstack)->next; (*pstack)->next = p; } /* end push */ A BCDABC stack push(S, 'D') stack Stack pushing and poping

4 -48 A BC BC pop(s) stack int pop(NODEPTR *pstack){ int x; NODEPTR p; if (empty(pstack) == TRUE){ printf("stack underflow\n"); exit(1); } /* end if */ p = (*pstack)->next; x = p->info; if (p == *pstack) /* only one node on the stack */ *pstack = NULL; else (*pstack)->next = p->next; freenode(p); return(x); } /* end pop */

4 -49 Representing a queue as a circular list A BC BC A BC D rear front rear frontrear front q insert(q, 'D') remove(q): same as pop in a stack q q

4 -50 void insert(NODEPTR *pq, int x) { NODEPTR p; p = getnode(); p->info = x; if (empty(pq) == TRUE) *pq = p; else p->next = (*pq)->next; (*pq)->next = p; *pq = p; return; } insert(&q, x) is equivalent to: push(&q, x); q = q->next; Node insertion in the circular list

4 -51 Delafter void delafter(NODEPTR p, int *px) { NODEPTR q; if ((p == NULL) || (p == p->next)){ /* the list is empty or contains only a single node*/ printf("void deletion\n"); return; } /* end if */ q = p->next; *px = q->info; p->next = q->next; freenode(q); return; } /* end delafter */... pq

4 -52 Concatenate 2 circular lists A BCD..... ABCD list1 list2

4 -53 void concat(NODEPTR *plist1, NODEPTR *plist2) { NODEPTR p; if (*plist2 == NULL) return; if (*plist1 == NULL){ *plist1 = *plist2; return; } p = (*plist1)->next; (*plist1)->next = (*plist2)->next; (*plist2)->next = p; *plist1 = *plist2; return; } /* end concat */

4 -54 null Doubly linked lists null A linear doubly linked list. A circular doubly linked list without a header. A circular doubly linked list with a header. Header node

4 -55 Implementation with C struct node{ int info; struct node *left, *right; }; typedef struct node *NODEPTR; left(right(p)) = p = right(left(p))

4 -56 Node deletion in a doubly linked circular list void delete(NODEPTR p, int *px) { NODEPTR q, r; if (p == NULL){ printf("void deletion\n"); return; } /* end if */ *px = p->info; q = p->left; r = p->right; q->right = r; r->left = q; freenode(p); return; } /* end delete */ A BC q p r

4 -57 Node insertion to the right of node p void insertright(NODEPTR p, int x) { NODEPTR q, r; if (p == NULL){ printf("void insertion\n"); return; } /* end if */ q = getnode(); q->info = x; r = p->right; r->left = q; q->right = r; q->left = p; p->right = q; return; } /* end insertright */ A B D C q rp

4 -58 Addition of long integers with doubly linked lists The integer Header 有 3 個 node 且為負數 Header The integer Header The integer 0

4 -59 How to add 2 long integers ? (1) 決定正負號 (2) 加法運算 **

4 -60 決定正負號 (1) int compabs(NODEPTR p, NODEPTR q) { NODEPTR r, s; /* compare the counts */ if (abs(p->info) > abs(q->info)) return(1); if (abs(p->info) info)) return(-1); /* the counts are equal */ r = p->left; s = q->left;

4 -61 決定正負號 (2) /* traverse the list from the most significant digits */ while (r != p){ if (r->info > s->info) return(1); if (r->info info) return(-1); r = r->left; s = s->left; } /* end while */ /* the absolute value are equal */ return(0); } /* end compabs */

4 -62 加法運算 NODEPTR addiff(NODEPTR p, NODEPTR q) { int count; NODEPTR pptr, qptr, r, s, zeroptr; long int hunthou = L; long int borrow, diff; int zeroflag; /* initialize variables */ count = 0; borrow = 0; zeroflag = FALSE; /* generate a header node for the sum */ r = getnode(); r->left = r; r->right = r; /* traverse the two lists */ pptr = p->right; qptr = q->right; while (qptr != q){ diff = pptr->info - borrow -qptr->info;

4 -63 if (diff >= 0) borrow = 0; else{ diff = diff + hunthou; borrow = 1; } /* end if */ /* generate a new node and insert it */ /* to the left of header in sum */ insertleft(r, diff); count += 1; /* test for zero node */ if (diff == 0){ if (zeroflag == FALSE) zeroptr = r->left; zeroflag = TRUE; }else zeroflag = FALSE; pptr = pptr->right; qptr = qptr->right; } /* end while */ /* traverse the remainder of the p list */ while (pptr != p){ diff = pptr->info - borrow;

4 -64 if (diff >= 0) borrow = 0; else{ diff = diff + hunthou; borrow = 1; } /* end if */ insertleft(r, diff); count += 1; if (diff == 0){ if (zeroflag == FALSE) zeroptr = r->left; zeroflag = TRUE; }else zeroflag = FALSE; pptr = pptr->right; } /* end while */ if (zeroflag == TRUE) /* delete leading zeros */ while (zeroptr != r){ s = zeroptr; zeroptr = zeroptr->right; delete(s, &diff); count -= 1; } /* end if...while */

4 -65 /* insert count and sign into the header */ if (p->info > 0) r->info = count; else r->info = -count; return(r); } /* end addiff */ 主程式 : NODEPTR addint(NODEPTR p, NODEPTR q) { /* check if integers are of like sign */ if (p->info * q->info > 0) return(addsame(p, q)); /* check which has a larger absolute value */ if (compabs(p, q) > 0) return(addiff(p, q)); else return(addiff(q, p)); } /* end addint */

4 -66 Linked lists in C++ class List{ protected: struct node{ int info; struct node *next; } typedef struct node *NODEPTR; NODEPTR listptr; // the pointer to the first node // of the list public: List(); ~List(); int emptylist(); void push(int newvalue); void insertafter(int oldvalue, int newvalue); int pop(); void delete(int oldvalue); }

4 -67 List::List(){ listptr = 0; } List::~List(){ NODEPTR p, q; if (emptylist()) return 0; for (p=listptr, q=p->next; p!=0; p=q, q=p->next) delete p; } int List:: emptylist(){ return(listptr == 0); } List::push(int newvalue){ NODEPTR p; p = new node; p->info = newvalue; p->next = listptr; listptr = p; }

4 -68 List::insertafter(int oldvalue, int newvalue){ NODEPTR p, q; for (p=listptr; p!=0 && p->info!=oldvalue; p=p->next) ; if (p == 0); error(“ERROR: value sought is not on the list.”); q = new node; q->info = newvalue; q->next = p->next; p->next = q; } int List::pop(){ NODEPTR p; int x; if (emptylist()) error(“ERROR: the list is empty.”); p = listptr; listptr = p->next; x = p->info; delete p; return x; }

4 -69 List::delete(int oldvalue){ NODEPTR p, q; for (q=0, p=listptr; p!=0 && p->info!=oldvalue; q=p, p=p->next) ; if (p == 0) error(“ERROR: value sought is not on the list.”); if (q == 0) listptr = p->next; else q->next = p->next; delete p; }