`
imiduo
  • 浏览: 10929 次
  • 性别: Icon_minigender_2
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类

java数据结构--链表

阅读更多

(1)简单链表
Java代码
package ChapterFive;  
 
class Link<E> {  
 
    public E data;  
 
    public Link<E> next;  
 
    public Link(E data) {  
        this.data = data;  
    }  
}  
 
class LinkList<E> {  
 
    public Link<E> first;  
    //链表中数据项的个数  
    public int size;  
 
    public LinkList() {  
        first = null;  
        size = 0;  
    }  
    //在表头插入新的数据  
    public void insertFirst(E value) {  
        Link<E> link = new Link<E>(value);  
        link.next = first;  
        first = link;  
        size++;  
    }  
    //判断链表是否为空  
    public boolean isEmpty() {  
        return size == 0;  
    }  
    //删除表头  
    public Link<E> deleteFirst() {  
        Link<E> temp = first;  
        first = first.next;  
        size--;  
        return temp;  
    }  
    //输出链表中的所有数据  
    public void display() {  
        Link<E> curr = first;  
        while (curr != null) {  
            System.out.print(curr.data + " ");  
            curr = curr.next;  
        }  
        System.out.println();  
    }  
    //返回链表中数据项的个数  
    public int size() {  
        return size;  
    }  
    //获取从头至尾的第i个数据项  
    public Link<E> get(int i) {  
        if (i > size() - 1 || i < 0)  
            try {  
                throw new IndexOutOfBoundsException();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        Link<E> curr = first;  
        for (int n = 0; n < size(); n++) {  
            if (n == i)  
                return curr;  
            else 
                curr = curr.next;  
        }  
        return null;  
    }  
    //输出从头至尾的第i个数据项  
    public void remove(int i) {  
        if (i == 0)  
            deleteFirst();  
        else if (i == size() - 1)  
            get(i - 1).next = null;  
        else {  
            get(i - 1).next = get(i + 1);  
        }  
        size--;  
    }  
}  
 
public class Link_list {  
    public static void main(String[] args) {  
        LinkList<Long> ll = new LinkList<Long>();  
        for (int i = 0; i < 10; i++) {  
            Long value = (long) (Math.random() * 100);  
            ll.insertFirst(value);  
        }  
        ll.display();  
        while (!ll.isEmpty()) {  
            ll.deleteFirst();  
            ll.display();  
        }  
        System.out.println("Ok");  
    }  


package ChapterFive;

class Link<E> {

public E data;

public Link<E> next;

public Link(E data) {
this.data = data;
}
}

class LinkList<E> {

public Link<E> first;
//链表中数据项的个数
public int size;

public LinkList() {
first = null;
size = 0;
}
//在表头插入新的数据
public void insertFirst(E value) {
Link<E> link = new Link<E>(value);
link.next = first;
first = link;
size++;
}
//判断链表是否为空
public boolean isEmpty() {
return size == 0;
}
//删除表头
public Link<E> deleteFirst() {
Link<E> temp = first;
first = first.next;
size--;
return temp;
}
//输出链表中的所有数据
public void display() {
Link<E> curr = first;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
//返回链表中数据项的个数
public int size() {
return size;
}
//获取从头至尾的第i个数据项
public Link<E> get(int i) {
if (i > size() - 1 || i < 0)
try {
throw new IndexOutOfBoundsException();
} catch (Exception e) {
e.printStackTrace();
}
Link<E> curr = first;
for (int n = 0; n < size(); n++) {
if (n == i)
return curr;
else
curr = curr.next;
}
return null;
}
//输出从头至尾的第i个数据项
public void remove(int i) {
if (i == 0)
deleteFirst();
else if (i == size() - 1)
get(i - 1).next = null;
else {
get(i - 1).next = get(i + 1);
}
size--;
}
}

public class Link_list {
public static void main(String[] args) {
LinkList<Long> ll = new LinkList<Long>();
for (int i = 0; i < 10; i++) {
Long value = (long) (Math.random() * 100);
ll.insertFirst(value);
}
ll.display();
while (!ll.isEmpty()) {
ll.deleteFirst();
ll.display();
}
System.out.println("Ok");
}
}
(2)链栈
Java代码
package ChapterFive;  
 
class LinkStack<E> {  
 
    LinkList<E> linkList;  
 
    int size;  
 
    public LinkStack() {  
        size = 0;  
        linkList = new LinkList<E>();  
    }  
    //入栈  
    public void push(E value) {  
        linkList.insertFirst(value);  
        size++;  
    }  
    //出栈  
    public Link<E> pop() {  
        size--;  
        return linkList.deleteFirst();  
    }  
    //返回栈顶元素  
    public Link<E> top() {  
        return linkList.first;  
    }  
    //判断栈是否为空  
    public boolean isEmpty() {  
        return size == 0;  
    }  
    //显示栈中的全部数据  
    public void display() {  
        linkList.display();  
    }  
}  
 
public class Link_stack {  
    public static void main(String[] args) {  
        LinkStack<Long> ls = new LinkStack<Long>();  
        for (int i = 0; i < 10; i++) {  
            Long value = new Long((long) (Math.random() * 100));  
            ls.push(value);  
        }  
        while (!ls.isEmpty()) {  
            ls.pop();  
            ls.display();  
        }  
        System.out.println("Ok");  
    }  


package ChapterFive;

class LinkStack<E> {

LinkList<E> linkList;

int size;

public LinkStack() {
size = 0;
linkList = new LinkList<E>();
}
//入栈
public void push(E value) {
linkList.insertFirst(value);
size++;
}
//出栈
public Link<E> pop() {
size--;
return linkList.deleteFirst();
}
//返回栈顶元素
public Link<E> top() {
return linkList.first;
}
//判断栈是否为空
public boolean isEmpty() {
return size == 0;
}
//显示栈中的全部数据
public void display() {
linkList.display();
}
}

public class Link_stack {
public static void main(String[] args) {
LinkStack<Long> ls = new LinkStack<Long>();
for (int i = 0; i < 10; i++) {
Long value = new Long((long) (Math.random() * 100));
ls.push(value);
}
while (!ls.isEmpty()) {
ls.pop();
ls.display();
}
System.out.println("Ok");
}
}
(3)有序表
Java代码
package ChapterFive;  
 
class SortedLink {  
 
    public Link<Long> first;  
 
    int size;  
 
    public SortedLink() {  
        first = null;  
        size = 0;  
    }  
    //向有序链表中插入数据  
    public void insert(long value) {  
        Link<Long> newLink = new Link<Long>(value);  
        Link<Long> previous = null;  
        Link<Long> curr = first;  
        while (curr != null && (value > curr.data)) {  
            previous = curr;  
            curr = curr.next;  
        }  
        if (previous == null)// 链表为空(在表头插入)  
            first = newLink;  
        else 
            previous.next = newLink;//插入新的节点  
        newLink.next = curr;  
        size++;  
    }  
    //删除第一个节点  
    public Link<Long> remove() {  
        Link<Long> temp = first;  
        first = first.next;  
        size--;  
        return temp;  
    }  
    //判断链表是否为空  
    public boolean isEmpty() {  
        return size == 0;  
    }  
    //输出链表的所有数据  
    public void display() {  
        Link<Long> curr = first;  
        while (curr != null) {  
            System.out.print(curr.data + " ");  
            curr = curr.next;  
        }  
        System.out.println();  
    }  
}  
 
public class SortedLinkApp {  
    public static void main(String[] args) {  
        SortedLink sl = new SortedLink();  
        for (int i = 0; i < 10; i++) {  
            sl.insert((long) (Math.random() * 100));  
        }  
        while (!sl.isEmpty()) {  
            sl.remove();  
            sl.display();  
        }  
    }  


package ChapterFive;

class SortedLink {

public Link<Long> first;

int size;

public SortedLink() {
first = null;
size = 0;
}
//向有序链表中插入数据
public void insert(long value) {
Link<Long> newLink = new Link<Long>(value);
Link<Long> previous = null;
Link<Long> curr = first;
while (curr != null && (value > curr.data)) {
previous = curr;
curr = curr.next;
}
if (previous == null)// 链表为空(在表头插入)
first = newLink;
else
previous.next = newLink;//插入新的节点
newLink.next = curr;
size++;
}
//删除第一个节点
public Link<Long> remove() {
Link<Long> temp = first;
first = first.next;
size--;
return temp;
}
//判断链表是否为空
public boolean isEmpty() {
return size == 0;
}
//输出链表的所有数据
public void display() {
Link<Long> curr = first;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
}

public class SortedLinkApp {
public static void main(String[] args) {
SortedLink sl = new SortedLink();
for (int i = 0; i < 10; i++) {
sl.insert((long) (Math.random() * 100));
}
while (!sl.isEmpty()) {
sl.remove();
sl.display();
}
}
}
(4)双向链表
Java代码
package ChapterFive;  
 
class DoubleLink<E> {  
 
    public Link<E> first;  
 
    public Link<E> last;  
 
    int size;  
 
    @SuppressWarnings("hiding")  
    class Link<E> {  
        public E data;  
 
        public Link<E> next;// 链表的下一项  
 
        public Link<E> previous;// 链表的前一项  
 
        public Link(E value) {  
            this.data = value;  
        }  
    }  
 
    public DoubleLink() {  
        first = null;  
        last = null;  
        size = 0;  
    }  
 
    // 在链表的首部插入一项  
    public void insertFirst(E value) {  
        Link<E> newLink = new Link<E>(value);  
        if (isEmpty())// 如果链表为空则first == last  
            last = newLink;  
        else 
            first.previous = newLink;// 确定原first与newLink的前后关系  
        newLink.next = first;  
        first = newLink;// 设置新的first值  
        size++;  
    }  
 
    // 在链表的尾部插入一项  
    public void insertLast(E value) {  
        Link<E> newLink = new Link<E>(value);  
        if (isEmpty())// 如果链表为空则last == first  
            first = newLink;  
        else {  
            last.next = newLink;// 确定原last与newLink的前后关系  
            newLink.previous = last;  
        }  
        last = newLink;// 设置新的last值  
        size++;  
    }  
 
    // 删除双向链表的表头  
    public Link<E> deleteFirst() {  
        Link<E> temp = first;  
        if (first.next == null)// 链表中只有一项数据  
            last = null;  
        else 
            first.next.previous = null;// 销毁原链表的头部  
        first = first.next;  
        size--;  
        return temp;  
    }  
 
    // 删除链表的最后一项  
    public Link<E> deleteLast() {  
        Link<E> temp = last;  
        if (first.next == null)// 链表中只有一项数据  
            first = null;  
        else 
            last.previous.next = null;// 销毁原链表的尾部  
        last = last.previous;  
        size--;  
        return temp;  
    }  
 
    // 判断链表是否为空  
    public boolean isEmpty() {  
        return size == 0;  
    }  
 
    // 输出链表中的所有数据项  
    public void display() {  
        Link<E> curr = first;  
        while (curr != null) {  
            System.out.print(curr.data + " ");  
            curr = curr.next;  
        }  
        System.out.println();  
    }  
}  
 
public class DoubleLinkApp {  
    public static void main(String[] args) {  
        DoubleLink<Integer> dl = new DoubleLink<Integer>();  
        for (int i = 0; i < 5; i++) {  
            dl.insertFirst((int) (Math.random() * 100));  
        }  
        for (int i = 0; i < 5; i++) {  
            dl.insertLast((int) (Math.random() * 100));  
        }  
        dl.display();  
        while (!dl.isEmpty()) {  
            dl.deleteFirst();  
            dl.deleteLast();  
            dl.display();  
        }  
        System.out.println("Ok");  
    }  

 

分享到:
评论

相关推荐

    Java数据结构--链表

    Java 数据结构 链表 Java链表 数据结构链表

    javalist数据结构-Java数据结构-------List.pdf

    javalist数据结构_Java数据结构-------List 三种List:ArrayList,Vector,LinkedList 类继承关系图 ArrayList和Vector通过数组实现,⼏乎使⽤了相同的算法;区别是ArrayList不是线程安全的,Vector绝⼤多数⽅法做了...

    数据结构-链表 JAVA语言实现

    数据结构-链表 JAVA语言实现,包含单向链表、双向链表、循环链表的遍历、删除和插入 详细介绍:http://blog.csdn.net/z740852294/article/details/77369439

    Java语言编写的数据结构-链表实现

    Java语言编写的数据结构-链表实现。包括顺序表和单链表、双链表

    图解数据结构--使用Java

    全书内容浅显易懂,利用大量且丰富的图示与范例, 详解复杂的抽象理论,从最基本的数据结构概念开始 说明,再以Java工具加以诠释阵列结构、堆栈、链表 、队列、排序、查找等重要的概念,引领读者抓住重 点轻松进入...

    Java-数据结构课设-链表实现通讯录管理系统

    
 第四个模块——Create()的功能是:创建新的数据记录。 
 第五个模块——Add()的功能是:增加新的数据记录,并返回选单。 
 第六个模块——Find()的功能是:按要求查询相关的信息,如果找到了,则显示该信息...

    数据结构-链表.pdf

    该资源主要是针对Java开发人员使用的“数据结构-链表”基础知识概况。本人承诺,该资源实属本人自己撰写,绝无抄袭!

    java 数据结构 链表

    java 数据结构 链表 自己写的 java 数据结构 链表 自己写的 java 数据结构 链表 自己写的

    Java数据结构 线性表,链表,哈希表是常用的数据结构

    Java数据结构 线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构

    Java数据结构篇-链表与数组实现栈.pptx.pptx

    数据结构的定义 数据结构是计算机存储、组织数据的方式,用于高效地访问和修改数据。...Java提供了丰富的数据结构库,包括数组、链表、栈、队列等,这些数据结构为程序员提供了处理各种问题的工具和方法。

    链表(数据结构--Java版)

    NULL 博文链接:https://zzqrj.iteye.com/blog/512813

    数据结构-链表(LingkedList)介绍和Java示例代码

    介绍数据结构链表(LingkedList)的概念、特点、优缺点、适用场景和Java示例代码

    Java算法实例-双向链表操作

    Java算法实例-双向链表操作,封装性高,考试、学习都可使用

    链表-使用JAVA语言实现链表数据结构.zip

    链表 链表_使用JAVA语言实现链表数据结构

    Java实现的循环链表源码

    由于在项目中需要用到循环链表,然而在JDK没有其实现,所以用Java语言实现了循环链表,供大家学习和参考。若有任何问题请发送E-Mail:wn_lut@126.com,以交流及改进。 Package:com.utilities.structs 打开方式:...

    java 数据结构 遍历链表程序

    java 数据结构 遍历链表程序 有研究或探讨的请加群:37424970 或联系本人MSN或邮箱:zhuseahui@yahoo.com.cn

    数据结构课程设计-城市链表

    电子工业出版社 数据结构课程设计 将若干城市的信息,存入一个带头结点的单链表。结点中的城市信息包括城市名和城市的位置坐标。要求能过利用城市名和位置坐标进行有关查找、插入、删除、更新等操作。

    数据结构-从应用到实现 (java版)

    主要内容包括:算法效率的输入规模、阶和大O,数据结构的无序和有序列表,队列和栈基于数组和链表的设计实例,递归详解,二叉查找树和AVL树,堆、散列表和排序以及图论等。对于每一种数据结构的性质和用途,《计算机...

Global site tag (gtag.js) - Google Analytics