白天贴了一个链表的数据结构,基本上都是看着书写的,晚上自己动手写了一个,这里贴出来给大家砸鸡蛋哈,
1,链表节点
package pku.ss.datastructure.ILinkedList;
public interface ILinkedList {
/**
* get the size of the list
* @return the size of the list
*/
public int getSize();
/**
* Judge that if the list is empty
* @return true or false
*/
public boolean isEmpty();
/**
* make the list empty
*/
public void makeEmpty();
/**
* Judge that if the list contains the node whose element equals x
* @param x
* @return true or false
*/
public boolean contains(Object x);
/**
* remove the node whose element equals x, and return a
* boolean value that if there is a x in list then return true,
* else return false
* @param x
* @return true or false
*/
public boolean remove(Object x);
/**
* Insert a node behind k'st into list with node's element equals x
* @param x
* @param k
* @return true or false
*/
public boolean insert(Object x, int k);
/**
* get the first element of list
* @return element of the first node
*/
public Object getFirst();
/**
* get the last element of list
* @return element of the last node
*/
public Object getLast();
/**
* print the elements of the list
*/
public void printList();
}
2,链表结构
package pku.ss.datastructure.LinkedList;
import pku.ss.datastructure.ILinkedList.ILinkedList;
public class LinkedList implements ILinkedList {
private int size;
private ListNode header;
/**
* Initialize a list with max capability of 0
*
* @param size
*/
public LinkedList() {
this.size = 0;
header = new ListNode(null);
}
@Override
public boolean contains(Object x) {
if (size == 0) {
System.out.println("The list is empty!");
return false;
}
ListNode temp = header.next;
while (temp != null && !temp.element.equals(x))
temp = temp.next;
if (temp == null)
return false;
else
return true;
}
@Override
public Object getFirst() {
if (size == 0) {
System.out.println("The list is empty");
return null;
}
return header.next.element;
}
@Override
public Object getLast() {
if (size == 0) {
System.out.println("The list is empty");
return null;
}
ListNode temp = header.next;
while (temp.next != null)
temp = temp.next;
return temp.element;
}
@Override
public int getSize() {
return this.size;
}
public boolean insert(Object x) {
return insert(x, 0);
}
@Override
public boolean insert(Object x, int k) {
if (size < k)
return false;
int count = 0;
ListNode temp = header;
while (count < k && temp.next != null) {
temp = temp.next;
count++;
}
ListNode aNode = new ListNode(x);
aNode.next = temp.next;
temp.next = aNode;
size++;
return true;
}
@Override
public boolean isEmpty() {
if (size != 0)
return false;
else
return true;
}
@Override
public void makeEmpty() {
header = null;
size = 0;
}
@Override
public boolean remove(Object x) {
if (!contains(x))
return false;
ListNode temp = header;
while (temp.next != null && !temp.next.element.equals(x))
temp = temp.next;
temp.next = temp.next.next;
size--;
return true;
}
public void printList() {
if (size == 0)
System.out.println("The list is Empty");
else {
ListNode temp = header.next;
while (temp != null) {
System.out.print(temp.element + " ");
temp = temp.next;
}
}
System.out.println();
System.out.println("***************************");
}
}
3,测试类
package pku.ss.datastructure.Demo;
import pku.ss.datastructure.LinkedList.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insert("H");
list.insert("G");
list.insert("F");
list.insert("E");
list.insert("D");
list.insert("C");
list.insert("B");
list.insert("A");
list.printList();
list.remove("A");
list.printList();
list.insert("D1", 3);
list.printList();
if (list.isEmpty())
System.out.println("Empty");
else
list.printList();
System.out.println("The list's size is: " + list.getSize());
System.out.println("*******************");
System.out.println("The first element is: " + list.getFirst());
System.out.println("*******************");
System.out.println("The last element is: " + list.getLast());
System.out.println("*******************");
list.makeEmpty();
if (list.isEmpty())
System.out.println("Empty");
else
list.printList();
}
}
分享到:
相关推荐
Java 数据结构 链表 Java链表 数据结构链表
Java数据结构 线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构
java 数据结构 链表 自己写的 java 数据结构 链表 自己写的 java 数据结构 链表 自己写的
java 数据结构 遍历链表程序 有研究或探讨的请加群:37424970 或联系本人MSN或邮箱:zhuseahui@yahoo.com.cn
这是个java编的双向链表的演示,数据结构是编程中很重要,但很难懂的一部分
数据结构-链表 JAVA语言实现,包含单向链表、双向链表、循环链表的遍历、删除和插入 详细介绍:http://blog.csdn.net/z740852294/article/details/77369439
JAVA实现链表 有序二叉树 队列的代码例子
list v2 用Object对象,接口inteface,迭代器Iterator实现linklist,Arraylist
Java语言编写的数据结构-链表实现。包括顺序表和单链表、双链表
基于java数据结构链表写的猴子选大王,其实就是一个约瑟夫环问题,采用java数据结构链表写的。有点小问题。当输入一只猴子,报数为1时删除会出错。没有实现动态显示猴子的添加和删除。
数据结构的定义 数据结构是计算机存储、组织数据的方式,用于高效地访问和修改数据。...Java提供了丰富的数据结构库,包括数组、链表、栈、队列等,这些数据结构为程序员提供了处理各种问题的工具和方法。
用java实现了数据结构中的链表,作为新手学习数据结构和java的资料。
数据结构 链表
数据结构,用Java实现链表 private class Node { private String data; private Node next; public Node(String dataPortioin) { data = dataPortioin; next = null; } public Node(String ...
基于Java实现数据结构链表相关程序
基于java数据结构链表实验报告.pdf
基于Java实现数据结构链表相关程序.pdf
JAVA链表实现类(数据结构学习).chm
基于java技术,利用swing作为界面展示,实现数据结构链表、堆栈的演示
第四个模块——Create()的功能是:创建新的数据记录。 第五个模块——Add()的功能是:增加新的数据记录,并返回选单。 第六个模块——Find()的功能是:按要求查询相关的信息,如果找到了,则显示该信息...