數據結構----順序表與單鏈表(JAVA)

下面為學習順序表和單鏈表的一些基本操作函數:

 1 public class SeqList<T> extends Object {
 2     protected int n;
 3     protected Object[] element;
 4 
 5     public SeqList(int length) {
 6         this.element = new Object[length];
 7         this.n = 0;
 8     }
 9 
10     public SeqList() {
11         this(64);
12     }
13 
14     public SeqList(T[] values) {
15         this(values.length);
16         for (int i = 0; i < values.length; i++) {
17             this.element[i] = values[i];
18         }
19         this.n = element.length;
20 
21     }
22 
23     public boolean isEmpty() {
24         if (this.n == 0)
25             return true;
26         return false;
27     }
28 
29     public int size() {
30         return this.n;
31     }
32 
33     public T get(int i) {
34         if (i >= 0 && i < this.n) {
35             return (T) this.element[i];
36         }
37         return null;
38     }
39 
40     public void set(int i, T x) {
41         if (x == null)
42             throw new NullPointerException("x==null");
43         if (i >= 0 && i < this.n)
44             this.element[i] = x;
45         else
46             throw new java.lang.IndexOutOfBoundsException(i + "");
47     }
48 
49     public int insert(int i, T x) { // 插入x元素
50         if (x == null)
51             throw new NullPointerException("x==null");
52         if (i < 0)
53             i = 0;
54         if (i > this.n)
55             i = this.n;
56         Object[] source = this.element;
57         if (this.n == element.length) {
58             this.element = new Object[source.length * 2]; // 申請一個2倍大的數組
59             for (int j = 0; j < i; j++) {
60                 this.element[j] = source[j];
61 
62             }
63         }
64         for (int j = this.n - 1; j >= i; j--) {
65             element[j + 1] = source[j];
66 
67         }
68 
69         this.element[i] = x;
70         this.n++;
71         return i;
72     }
73 
74     public int insert(T x) { // 表尾插入x元素,成員方法重載
75         return this.insert(this.n, x);
76     }
77 
78     public T remove(int i) { // 刪除數組下標為i的元素
79         if (this.n >= 0 && i >= 0 && i < this.n) {
80             T old = (T) this.element[i];
81             for (int j = i; j < this.n - 1; j++) {
82                 element[j] = element[j + 1];
83 
84             }
85             this.element[this.n - 1] = null;
86             this.n--;
87             return old;
88         }
89         return null;
90 
91     }
92 
93     public void clear() { // 刪除所有元素
94         this.n = 0;
95     }
  1 public class Node<T> { // 結點類
  2     public T data;
  3     public Node<T> next;
  4 
  5     public Node(T data, Node<T> next) {
  6         this.data = data;
  7         this.next = next;
  8     }
  9 
 10     public Node() {
 11         this(null, null);
 12     }
 13 
 14 }
 15 public class SinglyList<T> extends Object {  //帶頭結點的單鏈表類
 16     public Node<T> head;
 17 
 18     public SinglyList() {
 19         this.head = new Node<T>();
 20     }
 21 
 22     public SinglyList(T[] values) {
 23         this();
 24         Node<T> rear = this.head;
 25         for (int i = 0; i < values.length; i++) {
 26             rear.next = new Node<T>(values[i], null);
 27             rear = rear.next;
 28         }
 29     }
 30 
 31     public boolean isEmpty() {
 32         return this.head.next == null;
 33     }
 34 
 35     public T get(int i) {
 36         Node<T> p = this.head.next;
 37         for (int j = 0; p != null && j < i; j++) {
 38             p = p.next;
 39         }
 40         return (i >= 0 && p != null) ? p.data : null;
 41     }
 42 
 43     public void set(int i, T x) {
 44         if (i < 0 || i > size())
 45             throw new IndexOutOfBoundsException(i + "");
 46         if (x == null)
 47             throw new NullPointerException("x==null");
 48         Node<T> p = this.head.next;
 49         for (int j = 0; p != null && j < i; j++) {
 50             p = p.next;
 51         }
 52         p.data = x;
 53 
 54     }
 55 
 56     public int size() {
 57         int len = 0;
 58         Node<T> p = this.head.next;
 59         if (p == null)
 60             return -1;
 61         while (p != null) {
 62             len++;
 63             p = p.next;
 64 
 65         }
 66         return len;
 67     }
 68 
 69     public Node<T> insert(int i, T x) {
 70         if (x == null)
 71             throw new NullPointerException("x==null");
 72         Node<T> front = this.head;
 73         for (int j = 0; front.next != null && j < i; j++) {
 74             front = front.next;
 75         }
 76         front.next = new Node<T>(x, front.next);
 77         return front.next;
 78 
 79     }
 80 
 81     public Node<T> insert(T t) {
 82         return insert(Integer.MAX_VALUE, t);
 83     }
 84 
 85     public T remove(int i) {
 86         Node<T> front = this.head;
 87         for (int j = 0; front.next != null && j < i; j++) {
 88             front = front.next;
 89         }
 90         if (i >= 0 && front.next != null) {
 91             T old = front.next.data;
 92             front.next = front.next.next;
 93             return old;
 94         }
 95         return null;
 96 
 97     }
 98 
 99     public void clear() {
100         this.head.next = null;
101     }

?

轉載于:https://www.cnblogs.com/wang118/p/7207887.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/253976.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/253976.shtml
英文地址,請注明出處:http://en.pswp.cn/news/253976.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SSH初體驗系列--Hibernate--1--環境配置及demo

最近在學hibernate,常見的教程都是搭配mysql,因為公司本地電腦用的是pg,所以就嘗試著做個pg的小demo. 自己也是邊學邊寫&#xff0c;只當是加深印象.話不多說&#xff0c;直接開始; 一) 準備工作; 1) 本地安裝postgresql ,這個不多說&#xff0c;自己去網上下載; 注: 本次使用的…

Qt學習:QAction系列詳解

一、QAction類詳解 【詳細描述】 QAction類提供了抽象的用戶界面action&#xff0c;這些action可以被放置在窗口部件中。 應用程序可以通過菜單&#xff0c;工具欄按鈕以及鍵盤快捷鍵來調用通用的命令。由于用戶期望每個命令都能以相同的方式執行&#xff0c;而不管命令所使用的…

H.264優秀特征

一、主要特性 1、H.264/AVC相對以前的編碼方法&#xff0c;以MPEG-2為例&#xff0c;在圖像內容預測方面提高編碼效率&#xff0c;改善圖像質量的主要特點如下&#xff1a; ● 可變塊大小運動補償&#xff1a; 選擇運動補償大小和形狀比以前的標準更靈活&#xff0c;最小的…

Linux 文件系統 EXT4 的前世今生

在先前關于Linux文件系統的文章中&#xff0c;我寫了一份說明書去介紹Linux文件系統&#xff0c;里面有一些高級的概念&#xff0c;比如說&#xff0c;一切都是文件。我很想去深入地討論更多EXT文件系統的特性的信息。所以&#xff0c;首先讓我們來回答這個問題&#xff1a;什么…

windows 添加開始菜單

C:\Users\用戶名&#xff08;為你設置的電腦名稱&#xff09;\AppData\Roaming\Microsoft\Windows\Start Menu C:\ProgramData\Microsoft\Windows\Start Menu 注&#xff1a;默認狀態下AppData和ProgramData文件夾為隱藏狀態&#xff0c;所以要查看需要先顯示隱藏的文件。 具體…

awesome-go:很全的go語言資源合集

awesome-go:一個很全的go語言框架&#xff0c;庫&#xff0c;軟件合集 前面發過關于awsone-python, awsone django&#xff0c; flask。最近在學習golang&#xff0c;所以找到awsone-go 非常贊的go語言 Audio & 音樂類安全認證 & OAuthCUI數據庫數據庫驅動日期時間Emai…

zabbix監控系列(5)之通過trap模式監控網絡設備

轉載于:https://www.cnblogs.com/liaojiafa/p/7216749.html

struts2框架下的一個簡單的ajax例子

舉個例子 jsp頁面&#xff1a; <% page language"java" import"java.util.*" pageEncoding"utf-8"%> <% String path request.getContextPath(); String basePath request.getScheme()"://"request.getServerName()":…

C語言的指針初始化特別注意一點

void func2(int *value) { *value 2; /// value為空指針&#xff0c;不能被取值&#xff0c;所以*value是錯誤的 } void func1() { int *p 0;//此處相當于PNULL func2(p); } / void func2(int *value) { *value 2; /// 正確} void func1() { int a0; int *p &…

小程序—九宮格心形拼圖

說明 前幾天在朋友圈看到好幾次這種圖片。 這種圖片&#xff0c;是用九張圖片拼成的一個心形。 感覺很有趣&#xff0c;就上網查了查怎么做&#xff0c;大部分的說法就是用美圖秀秀的拼圖功能來做&#xff0c; 在微信小程序中也有專門做心形拼圖的小程序&#xff0c;我都試了試…

第二部分:志愿錄取標準

第二部分&#xff1a;志愿錄取標準 零、概況一、傳統志愿錄取過程二、平行志愿錄取過程三、17年志愿錄取過程 零、概況自1977年&#xff0c;恢復高考以來&#xff0c;高考錄取標準&#xff0c;作為公平線&#xff0c;都是相當透明的。這部分分享&#xff0c;以錄取標準&#xf…

100. Same Tree

Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 遞歸遍歷左子樹和右子樹 /*** Definition for a binary tree node.* struct T…

關于RTP時間戳及多媒體通信同步的問題/H264關于RTP協議的實現

http://www.rosoo.net/a/201101/10776.html http://hi.baidu.com/fairygardenjoy/blog/item/e56c5cca95829e37b600c88e.html H264關于RTP協議的實現:http://www.rosoo.net/a/201108/14896.html RTP協議包頭的格式&#xff1a; 10~16 Bit為PT域&#xff0c;指的就是負載類型…

程序員懂點經濟學-股票投資

2019獨角獸企業重金招聘Python工程師標準>>> ▍寫在前面 前面有文章 關于程序員如何賺點小錢 講過 合理的投資理財&#xff0c;可以了解一下. 再次建議&#xff0c;不要將全身家當投入股市&#xff0c;建議投入10~30%就好了. (不要拿輸不起的錢來炒股&#xff0c;比…

徹底弄懂響應式設計中的em和rem

前一陣子在響應式開發中遇到了em和rem的問題&#xff0c;也上網搜過一些文章&#xff0c;篇幅很長&#xff0c;也沒有仔細看&#xff0c;今天來總結一下。 rem是指&#xff1a;根元素&#xff08;root element&#xff0c;html&#xff09;的字體大小&#xff0c; em是指&#…

JAVA字符串

字符串 1. 字符串 1.1 字符串概述和特點 java.lang.String類代表字符串。 API當中說&#xff1a;Java 程序中的所有字符串字面值&#xff08;如 "abc" &#xff09;都作為此類的實例實現。 其實就是說&#xff1a;程序當中所有的雙引號字符串&#xff0c;都是String類…

21分鐘 MySQL 入門教程

轉自 21分鐘 MySQL 入門教程 一、MySQL的相關概念介紹二、Windows下MySQL的配置配置步驟MySQL服務的啟動、停止與卸載三、MySQL腳本的基本組成四、MySQL中的數據類型五、使用MySQL數據庫登錄到MySQL創建一個數據庫選擇所要操作的數據庫創建數據庫表六、操作MySQL數據庫向表中插…

node-sass報錯解決方法

node-sass報錯解決方法 node-sass報錯解決方法 在Vue.js中&#xff0c;每一個vue文件都是一個組件&#xff0c;在.vue文件中可以將模板&#xff0c;腳本&#xff0c;樣式寫在一起&#xff0c;便于組織整個組件。在使用template&#xff0c;script時&#xff0c;編寫css樣式時&a…

微軟人工智能愿景:根植于研發 寄望于“對話”

過去25年來&#xff0c;微軟公司持續投入人工智能的發展愿景。現在&#xff0c;借助全新發布的聊天機器人Zo、Cortana Decices SDK和智能套件、以及擴展智能工具&#xff0c;這一愿景即將成為現實。12月13日&#xff0c;在舊金山的一次小聚會上&#xff0c;微軟全球執行副總裁、…