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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

《数据结构Java版第2版》习题解答.docx

1、数据结构Java版第2版习题解答数据结构(Java版)(第2版)习题解答第1章 Java程序设计基础【习1.1】 实验0.1 哥德巴赫猜想。【习1.2】 实验0.2 杨辉三角形。【习1.3】 实验0.3 金额的中文大写形式。【习1.4】 实验0.4 下标和相等的数字方阵。输出下列方阵(当n=4时)。1 2 6 7 或 1 3 4 103 5 8 13 2 5 9 114 9 12 14 6 8 12 1510 11 15 16 7 13 14 16采用二维数组实现。二维数组中,每一条斜线上各元素下标和相等,如图0.1所示。图1.2 下标和相等的数字方阵算法描述程序如下。public class

2、 Upmat public static void main(String args) int n=4; /阶数 int mat = new intnn; int k=1; /k是自然数,递增变化 boolean up = true; /方向向上 for (int sum=0; sum=0; i-) matisum-i = k+; /k先赋值后自加 else for (int i=0; i=sum; i+) matisum-i = k+; up=!up; /方向求反 for (int sum=n; sum2*n-1; sum+) /右下三角 if (up) for (int j=sum-n+1

3、; jsum-n; j-) matsum-jj = k+; up=!up; for (int i=0; imat.length; i+) /输出二维数组元素 for (int j=0; jmati.length; j+) /i、j是行、列下标 System.out.print( +matij); System.out.println(); 【习1.1】 实验0.5 找出一个二维数组的鞍点【习1.2】 实验0.6 复数类。【习1.3】 实验0.8 图形接口与实现图形接口的类第2章 绪论【习2.1】 实验1.1 判断数组元素是否已按升序排序。程序见例1.4的SortedArray.java。pub

4、lic static boolean isSorted(int table) /判断整数数组是否已按升序排序 /若已排序返回true,否则返回false if (table=null) return false; for (int i=0; itablei+1) return false; return true;public static boolean isSorted(Comparable table) /判断对象数组是否已按升序排序 /若已排序返回true,否则返回false if (table=null) return false; for (int i=0; i0) return

5、false; return true;【习2.2】 实验1.3 用递归算法求两个整数的最大公因数。public class Gcd public static int gcd(int a, int b) /返回a,b的最大公因数,递归方法 if(b=0) return a; if(a0) return gcd(-a, b); if(b0) return gcd(a, -b); return gcd(b, a%b); public static void main(String args) int a=12, b=18, c=24; System.out.println(gcd(+a+,+b+,

6、+c+)=+gcd(gcd(a,b),c); /获得3个整数最大公因数 第3章 线性表【习3.1】 习2-5 图2.19的数据结构声明。table数组元素为单链表,声明如下:SinglyLinkedList table【习3.2】 习2-6 如果在遍历单链表时,将p=p.next语句写成p.next=p,结果会怎样?使p.next指向p结点自己,改变了结点间的链接关系,丢失后继结点,如图2.1所示。图3.2 p.next=p将改变结点间的链接关系【习3.1】 实验2.2 由指定数组中的多个对象构造单链表。在SinglyLinkedList单链表类中,增加构造方法如下。public Singly

7、LinkedList(E element) /由指定数组中的多个对象构造单链表 this.head = null; if (element!=null & element.length0) this.head = new Node(element0); Node rear=this.head; int i=1; while (ielement.length) rear.next = new Node(elementi+); rear = rear.next; 【习3.2】 实验2.2 单链表的查找、包含、删除操作详见8.2.1。单链表的以下查找、包含、删除等操作方法详见8.2.1顺序查找。pu

8、blic Node search(E element, Node start) /从单链表结点start开始顺序查找指定对象public Node search(E element) /若查找到指定对象,则返回结点,否则返回nullpublic boolean contain(E element) /以查找结果判断单链表是否包含指定对象public boolean remove(E element) /移去首次出现的指定对象【习3.3】 实验2.2 单链表的替换操作。在SinglyLinkedList单链表类中,增加替换操作方法如下。public boolean replace(Object

9、obj, E element) /将元素值为obj的结点值替换为element /若替换成功返回true,否则返回false,O(n) if (obj=null | element=null) return false; Node p=this.head; while (p!=null) if (obj.equals(p.data) p.data = element; return true; p = p.next; return false;【习3.4】 实验2.2 首尾相接地连接两条单链表。在SinglyLinkedList单链表类中,增加替换操作方法如下。public void conc

10、at(SinglyLinkedList list) /将指定单链表list链接在当前单链表之后 if (this.head=null) this.head = list.head; else Node p=this.head; while (p.next!=null) p = p.next; p.next = list.head; 【习3.5】 实验2.2 复制单链表。在SinglyLinkedList单链表类中,增加构造方法如下。public SinglyLinkedList(SinglyLinkedList list) /以单链表list构造新的单链表 /复制单链表 this.head =

11、 null; if (list!=null & list.head!=null) this.head = new Node(list.head.data); Node p = list.head.next; Node rear = this.head; while (p!=null) rear.next = new Node(p.data); rear = rear.next; p = p.next; 【习3.6】 实验2.2 单链表构造、复制、比较等操作的递归方法。由指定数组中的多个对象构造单链表的操作也可设计为以下的递归方法:public SinglyLinkedList(E elemen

12、t) /由指定数组中的多个对象构造单链表 this.head = null; if (element!=null) this.head = create(element,0);private Node create(E element, int i) /由指定数组构造单链表,递归方法 Node p=null; if (ielement.length) p = new Node(elementi); p.next = create(element, i+1); return p;单链表的复制操作也可设计为以下的递归方法:public SinglyLinkedList(SinglyLinkedLi

13、st list) /以单链表list构造新的单链表 this.head = copy(list.head);private Node copy(Node p) /复制单链表,递归方法 Node q=null; if (p!=null) q = new Node(p.data); q.next = copy(p.next); return q;比较两条单链表是否相等的操作也可设计为以下的递归方法:public boolean equals(Object obj) /比较两条单链表是否相等 if (obj = this) return true; if (obj instanceof Singly

14、LinkedList) SinglyLinkedList list = (SinglyLinkedList)obj; return equals(this.head, list.head); return false;private boolean equals(Node p, Node q) /比较两条单链表是否相等,递归方法 if (p=null & q=null) return true; if (p!=null & q!=null) return p.data.equals(q.data) & equals(p.next, q.next); return false;【习3.7】 建立

15、按升序排序的单链表(不带头结点)。采用直接插入排序算法将一个结点插入到已排序的单链表中。import dataStructure.linearList.Node; import dataStructure.linearList.SinglyLinkedList; /不带头结点的单链表类public class SortedSinglyLinkedList extends SinglyLinkedList public SortedSinglyLinkedList() super(); public boolean add(E element) /根据指定对象的大小插入在合适位置 if (ele

16、ment=null | !(element instanceof Comparable) return false; /不能插入null或非Comparable对象 Comparable cmp = (Comparable)element; if (this.head=null | pareTo(this.head.data)=0) this.head = new Node(element,this.head); /头插入 else Node front=null, p=this.head; while (p!=null & pareTo(p.data)0) front = p; /front

17、是p的前驱结点 p = p.next; front.next = new Node(element, p); /中间/尾插入 return true; public static void main(String args) SortedSinglyLinkedList list = new SortedSinglyLinkedList(); int n=10; System.out.print(insert: ); for (int i=0; in; i+) int k = (int) (Math.random()*100); /产生随机数 if (list.add(new Integer(

18、k) System.out.print(k+ ); System.out.println(nlist: +list.toString(); 程序多次运行结果如下:insert: 22 48 50 9 71 71 19 67 50 80 list: (9, 19, 22, 48, 50, 50, 67, 71, 71, 80)insert: 42 33 52 89 13 11 50 29 78 34 list: (11, 13, 29, 33, 34, 42, 50, 52, 78, 89)insert: 69 16 99 0 20 68 14 73 90 76 list1: (0, 14, 1

19、6, 20, 68, 69, 73, 76, 90, 99)【习3.8】 实验2.6 带头结点的循环双链表类,实现线性表接口。package dataStructure.linearList;import dataStructure.linearList.DLinkNode; /导入双链表结点类import dataStructure.linearList.LList; /导入线性表接口public class CHDoublyLinkedList implements LList /带头结点的循环双链表类 protected DLinkNode head; /头指针 public CHDou

20、blyLinkedList() /构造空链表 this.head = new DLinkNode(); /创建头结点,值为null this.head.prev = head; this.head.next = head; public boolean isEmpty() /判断双链表是否为空 return head.next=head; /以下算法同循环单链表,与单链表的差别在于,循环条件不同 public int length() /返回双链表长度 int i=0; DLinkNode p=this.head.next; /此句与单链表不同 while (p!=head) /循环条件与单链

21、表不同 i+; p = p.next; return i; public E get(int index) /返回序号为index的对象 if (index=0) int j=0; DLinkNode p=this.head.next; while (p!=head & j=0 & element!=null) int j=0; DLinkNode p=this.head.next; while (p!=head & jindex) j+; p=p.next; if (p!=head) E old = (E)p.data; p.data = element; return old; retur

22、n null; public String toString() String str=(; DLinkNode p = this.head.next; while (p!=head) str += p.data.toString(); p = p.next; if (p!=head) str += , ; return str+); /双链表的插入、删除算法与单链表不同 public boolean add(int index, E element) /插入element对象,插入后对象序号为index /若操作成功返回true,O(n) if (element=null) return f

23、alse; /不能添加空对象(null) int j=0; DLinkNode front = this.head; while (front.next!=head & jindex) /寻找插入位置,若i=0,插入在头结点之后 j+; front = front.next; DLinkNode q = new DLinkNode(element, front, front.next); /插入在front结点之后 front.next.prev = q; front.next = q; return true; public boolean add(E element) /在单链表最后添加对

24、象,O(1) if (element=null) return false; /不能添加空对象(null) DLinkNode q = new DLinkNode(element, head.prev, head); head.prev.next = q; /插入在头结点之前,相当于尾插入 head.prev = q; return true; public E remove(int index) /移除指定位置的对象,O(n) /返回被移除的原对象,指定位置序号错误时返回null E old = null; int j=0; DLinkNode p=this.head.next; while

25、 (p!=head & jindex) /定位到待删除结点 j+; p = p.next; if (p!=head) old = (E)p.data; /操作成功,返回原对象 p.prev.next = p.next; /删除p结点自己 p.next.prev = p.prev; return old; public void clear() /清空线性表 this.head.prev = head; this.head.next = head; /以上实现LList接口 public static void main(String args) int i=0; CHDoublyLinkedL

26、ist list = new CHDoublyLinkedList(); System.out.println(删除第+i+个结点+list.remove(0); System.out.println(list.toString(); for (i=5; i=0; i-) list.add(0, new String(char)(A+i)+); for (i=0; i6; i+) list.add(new String(char)(A+i)+);/ list.add(i, new String(char)(A+i)+); System.out.println(list.toString();

27、System.out.println(删除第+i+个结点+list.remove(i); System.out.println(list.toString(); 程序运行结果如下: 删除第0个结点null()(A, B, C, D, E, F, A, B, C, D, E, F)删除第6个结点A(A, B, C, D, E, F, B, C, D, E, F)【习3.9】 实验2.5 建立按升序排序的循环双链表。package dataStructure.linearList;import dataStructure.linearList.DLinkNode;import dataStructure.

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

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