Java 模拟单链表

来源:互联网 发布:网络打国际长途 编辑:程序博客网 时间:2024/05/29 18:40

线性表:

线性表(亦作顺序表)是最基本、最简单、也是最常用的一种数据结构。

线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。

线性表的逻辑结构简单,便于实现和操作。

在实际应用中,线性表都是以栈、队列、字符串等特殊线性表的形式来使用的。

线性结构的基本特征为:

1.集合中必存在唯一的一个“第一元素”;

2.集合中必存在唯一的一个 “最后元素” ;

3.除最后一个元素之外,均有 唯一的后继(后件);

4.除第一个元素之外,均有 唯一的前驱(前件)。


链表:linked list

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的

每个数据项都被包含在“链结点”(Link)中。

链结点是一个类的对象,这类可叫做Link。链表中有许多类似的链结点,每个Link中都中包含有一个对下一个链结点引用的字段next。

链表对象本身保存了一个指向第一个链结点的引用first。(若没有first,则无法定位)

链表不能像数组那样(利用下标)直接访问到数据项,而需要用数据间的关系来定位,即访问链结点所引用的下一个链结点,而后再下一个,直至访问到需要的数据

在链头插入和删除的时间复杂度为O(1),因为只需要改变引用的指向即可

而查找、删除指定结点、在指定结点后插入,这些操作都需要平均都需要搜索链表中的一半结点,效率为O(N)。

单链表:

以“结点的序列”表示线性表 称作线性链表(单链表)

是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。(这组存储单元既可以是连续的,也可以是不连续的)

链结点的结构:

┌────┬────┐

│data│next│

└────┴────┘

存放结点值的数据域data;存放结点的引用 的指针域(链域)next

链表通过每个结点的链域将线性表的n个结点按其逻辑顺序链接在一起的。

每个结点只有一个链域的链表称为单链表(Single Linked List) , 一个方向, 只有后继结节的引用

/** * 单链表:头插法后进先出 * 将链表的左边称为链头,右边称为链尾。 * 头插法建单链表是将链表右端看成固定的,链表不断向左延伸而得到的。 * 头插法最先得到的是尾结点 * @author stone */public class SingleLinkedList<T> {private Link<T> first;//首结点public SingleLinkedList() {}public boolean isEmpty() {return first == null;}public void insertFirst(T data) {// 插入 到 链头Link<T> newLink = new Link<T>(data);newLink.next = first; //新结点的next指向上一结点first = newLink;}public Link<T>  deleteFirst() {//删除 链头Link<T> temp = first;first = first.next; //变更首结点,为下一结点return temp;}public Link<T> find(T t) {Link<T> find = first;while (find != null) {if (!find.data.equals(t)) {find = find.next;} else {break;}}return find; }public Link<T> delete(T t) {if (isEmpty()) {return null;} else {if (first.data.equals(t)) {Link<T> temp = first;first = first.next; //变更首结点,为下一结点return temp;}}Link<T> p = first;Link<T> q = first;while (!p.data.equals(t)) {if (p.next == null) {//表示到链尾还没找到return null;} else {q = p;p = p.next;}}q.next = p.next;return p;}public void displayList() {//遍历System.out.println("List (first-->last):");Link<T> current = first;while (current != null) {current.displayLink();current = current.next;}}public void displayListReverse() {//反序遍历Link<T> p = first, q = first.next, t;while (q != null) {//指针反向,遍历的数据顺序向后t = q.next; //no3if (p == first) {// 当为原来的头时,头的.next应该置空p.next = null;}q.next = p;// no3 -> no1  pointer reversep = q; //start is reverseq = t; //no3 start}//上面循环中的if里,把first.next 置空了, 而当q为null不执行循环时,p就为原来的最且一个数据项,反转后把p赋给firstfirst = p; displayList();}class Link<T> {//链结点T data;//数据域Link<T> next; //后继指针,结点链域Link(T data) {this.data = data;}void displayLink() {System.out.println("the data is " + data.toString());}}public static void main(String[] args) {SingleLinkedList<Integer> list = new SingleLinkedList<Integer>();list.insertFirst(33);list.insertFirst(78);list.insertFirst(24);list.insertFirst(22);list.insertFirst(56);list.displayList();list.deleteFirst();list.displayList();System.out.println("find:" + list.find(56));System.out.println("find:" + list.find(33));System.out.println("delete find:" + list.delete(99));System.out.println("delete find:" + list.delete(24));list.displayList();System.out.println("----reverse----");list.displayListReverse();}}
打印

List (first-->last):the data is 56the data is 22the data is 24the data is 78the data is 33List (first-->last):the data is 22the data is 24the data is 78the data is 33find:nullfind:linked_list.SingleLinkedList$Link@4b71bbc9delete find:nulldelete find:linked_list.SingleLinkedList$Link@17dfafd1List (first-->last):the data is 22the data is 78the data is 33----reverse----List (first-->last):the data is 33the data is 78the data is 22
/** * 单链表:尾插法 后进先出 * 若将链表的左端固定,链表不断向右延伸,这种建立链表的方法称为尾插法。 * 尾插法建立链表时,头指针固定不动,故必须设立一个尾部的指针,向链表右边延伸, * 尾插法最先得到的是头结点。 * @author stone */public class SingleLinkedList2<T> {private Link<T> head;//首结点public SingleLinkedList2() {}public boolean isEmpty() {return head == null;}public void insertLast(T data) {//在链尾 插入Link<T> newLink = new Link<T>(data);if (head != null) {Link<T> nextP = head.next;if (nextP == null) {head.next = newLink;} else {Link<T> rear = null;while (nextP != null) {rear = nextP;nextP = nextP.next;}rear.next = newLink;}} else {head = newLink;}}public Link<T>  deleteLast() {//删除 链尾Link<T> p = head;Link<T> q = head;while (p.next != null) {// p的下一个结点不为空,q等于当前的p(即q是上一个,p是下一个) 循环结束时,q等于链尾倒数第二个q = p;p = p.next;}//deleteq.next = null;return p;}public Link<T> find(T t) {Link<T> find = head;while (find != null) {if (!find.data.equals(t)) {find = find.next;} else {break;}}return find; }public Link<T> delete(T t) {if (isEmpty()) {return null;} else {if (head.data.equals(t)) {Link<T> temp = head;head = head.next; //变更首结点,为下一结点return temp;}}Link<T> p = head;Link<T> q = head;while (!p.data.equals(t)) {if (p.next == null) {//表示到链尾还没找到return null;} else {q = p;p = p.next;}}q.next = p.next;return p;}public void displayList() {//遍历System.out.println("List (head-->last):");Link<T> current = head;while (current != null) {current.displayLink();current = current.next;}}public void displayListReverse() {//反序遍历Link<T> p = head, q = head.next, t;while (q != null) {//指针反向,遍历的数据顺序向后t = q.next; //no3if (p == head) {// 当为原来的头时,头的.next应该置空p.next = null;}q.next = p;// no3 -> no1  pointer reversep = q; //start is reverseq = t; //no3 start}//上面循环中的if里,把head.next 置空了, 而当q为null不执行循环时,p就为原来的最且一个数据项,反转后把p赋给headhead = p; displayList();}class Link<T> {//链结点T data;//数据域Link<T> next; //后继指针,结点链域Link(T data) {this.data = data;}void displayLink() {System.out.println("the data is " + data.toString());}}public static void main(String[] args) {SingleLinkedList2<Integer> list = new SingleLinkedList2<Integer>();list.insertLast(33);list.insertLast(78);list.insertLast(24);list.insertLast(22);list.insertLast(56);list.displayList();list.deleteLast();list.displayList();System.out.println("find:" + list.find(56));System.out.println("find:" + list.find(33));System.out.println("delete find:" + list.delete(99));System.out.println("delete find:" + list.delete(78));list.displayList();System.out.println("----reverse----");list.displayListReverse();}}
打印

List (head-->last):the data is 33the data is 78the data is 24the data is 22the data is 56List (head-->last):the data is 33the data is 78the data is 24the data is 22find:nullfind:linked_list.SingleLinkedList2$Link@4b71bbc9delete find:nulldelete find:linked_list.SingleLinkedList2$Link@17dfafd1List (head-->last):the data is 33the data is 24the data is 22----reverse----List (head-->last):the data is 22the data is 24the data is 33


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 车辆行驶中遭核生化打击时怎么办? 硫酸气体熏到嗓子怎么办 硫酸味儿进嗓子了怎么办 吸入二氧化氯气体后怎么办 衣服84消毒 变色了怎么办 我的身份证被诈骗犯知道了怎么办 母婴店来客人少怎么办 键盘驱动被卸载了怎么办 广州 狗狗随地大小便怎么办 上海 狗狗随地大小便怎么办 服装店人流量少生意差怎么办 2018年服装店生意特别差怎么办 打印机拍的照片打出黑怎么办 租的汽车撞了怎么办 神州租车车坏了怎么办 深圳市公安局办保安员证怎么办? 老婆被车撞了后失忆了怎么办 生气引起的短暂失忆怎么办 win10系统忘记开机密码怎么办 戴尔win10密码忘了怎么办 戴尔电脑win10密码忘了怎么办 出门忘记带身份证了怎么办 一年染了7次头发怎么办 低头久了颈椎疼怎么办 一只眼睛磨得慌怎么办 没有睡好眼睛痛怎么办 好几天没休息好怎么办 血糖高睡不好觉怎么办 眼睛感觉磨的慌怎么办 痔疮手术后大便疼肛裂痛怎么办 肛裂排便困难痛怎么办 智齿导致的牙疼怎么办 肛裂一直不愈合怎么办 孕妇肛裂拉屎疼怎么办 产后50天小肚子突出怎么办 肚子像怀孕一样大怎么办 蹲厕所拉不出来怎么办 生气导致回奶了怎么办 老公每晚要吃奶才睡觉怎么办 分分钟想把老公杀掉怎么办 老公出轨闹的厉害离家出走怎么办?