java数据结构-链表的实现
来源:互联网 发布:vb制作计算器 编辑:程序博客网 时间:2024/06/07 02:45
//链表的实现主要是由链节点不断相连而构成
class Link{//链节点
public int data;//数据项
public Link next;//引用,指向下一个链节点,初始为NULL
public Link(int d){
data=d;
next=null;
}
public void showLink(){
System.out.println("{"+data+"}");
}
}
class LinkList{//链表,只包含first
public Link first;
public void insert(int data){//插入一个链节点,总是从链首插入
Link newLink=new Link(data);
newLink.next=first;
first=newLink;
}
public Link delete(){//删除一个链节点,总是从头删除
Link tmp=first;
first=first.next;
return tmp;
}
public void show(){//显示链表
Link current=first;
while(current!=null){
current.showLink();
current=current.next;
}
}
public boolean isEmpty(){
return first==null;
}
public boolean searchKey(int key){
if(!isEmpty()){
Link current=first;
Link prior;
while(current!=null){
if(current.data==key)
return true;
else{
prior=current;
current=current.next;
}
}
return false;
}
return false;
}
public Link deleteKey(int key){
Link current=first;
Link prior=first;
Link tmp=null;
if(!isEmpty()){
while(current!=null){
if(current.data==key)
break;
else{
prior=current;
current=current.next;
}
}
}
if(current==first){
first=first.next;
}
if(current!=null){
tmp=current;
prior.next=current.next;
}
return tmp;
}
public void insertRear(int data){
Link newLink=new Link(data);
if(first==null)
first=newLink;
else{
Link current=first;
while(current.next!=null){
current=current.next;
}
current.next=newLink;
}
}
}
public class LinkListTest{
public static void main(String[] args){
LinkList ll=new LinkList();
ll.insertRear(10);
ll.insertRear(50);
ll.insertRear(20);
ll.insertRear(30);
ll.show();
/*
ll.insert(10);
ll.insert(50);
ll.insert(20);
ll.insert(30);
ll.show();
System.out.println("-------------------------------");
ll.delete();
ll.show();
System.out.println("----------好玩---------------------");
System.out.println(ll.searchKey(20));
System.out.println(ll.deleteKey(50));
System.out.println("----------好玩---------------------");
ll.show();*/
}
}
class Link{//链节点
public int data;//数据项
public Link next;//引用,指向下一个链节点,初始为NULL
public Link(int d){
data=d;
next=null;
}
public void showLink(){
System.out.println("{"+data+"}");
}
}
class LinkList{//链表,只包含first
public Link first;
public void insert(int data){//插入一个链节点,总是从链首插入
Link newLink=new Link(data);
newLink.next=first;
first=newLink;
}
public Link delete(){//删除一个链节点,总是从头删除
Link tmp=first;
first=first.next;
return tmp;
}
public void show(){//显示链表
Link current=first;
while(current!=null){
current.showLink();
current=current.next;
}
}
public boolean isEmpty(){
return first==null;
}
public boolean searchKey(int key){
if(!isEmpty()){
Link current=first;
Link prior;
while(current!=null){
if(current.data==key)
return true;
else{
prior=current;
current=current.next;
}
}
return false;
}
return false;
}
public Link deleteKey(int key){
Link current=first;
Link prior=first;
Link tmp=null;
if(!isEmpty()){
while(current!=null){
if(current.data==key)
break;
else{
prior=current;
current=current.next;
}
}
}
if(current==first){
first=first.next;
}
if(current!=null){
tmp=current;
prior.next=current.next;
}
return tmp;
}
public void insertRear(int data){
Link newLink=new Link(data);
if(first==null)
first=newLink;
else{
Link current=first;
while(current.next!=null){
current=current.next;
}
current.next=newLink;
}
}
}
public class LinkListTest{
public static void main(String[] args){
LinkList ll=new LinkList();
ll.insertRear(10);
ll.insertRear(50);
ll.insertRear(20);
ll.insertRear(30);
ll.show();
/*
ll.insert(10);
ll.insert(50);
ll.insert(20);
ll.insert(30);
ll.show();
System.out.println("-------------------------------");
ll.delete();
ll.show();
System.out.println("----------好玩---------------------");
System.out.println(ll.searchKey(20));
System.out.println(ll.deleteKey(50));
System.out.println("----------好玩---------------------");
ll.show();*/
}
}
0 0
- 数据结构之链表的Java实现
- java数据结构链表的实现
- Java数据结构之链表的实现
- java数据结构-链表的实现
- java 链表数据结构实现
- 数据结构-Java实现链表
- [java数据结构]--java双向链表LinkedList的简单实现
- 数据结构的JAVA实现
- 数据结构之链表的增删操作的Java实现
- Java数据结构之简单的链表的实现
- java链表的数据结构和二叉树的实现
- [数据结构]java实现的简单链表的 头/尾插法
- 数据结构之双向链表的Java实现
- 数据结构:二叉树的二叉链表存储--Java实现
- 数据结构:二叉树的三叉链表存储--Java实现
- 数据结构Java实现 --单向链表的插入和删除
- 【数据结构】链表的原理及java实现
- Java -- 栈、队列等数据结构的简单链表实现
- linux的一些基本安全策略
- LeetCode 371:Sum of Two Integers
- 关于Docker的常见疑问
- 详解WSGI
- mfc里各控件随窗口的放缩而放缩
- java数据结构-链表的实现
- 软件测试
- 201509-4-高速公路
- 利用PHP判断iPhone、iPad、Android、PC设备
- 机器人时代即将来临?
- Codeforces--368C--Sereja and Algorithm(规律)
- ubuntu安装软件时提示有未能满足的依赖关系
- java数据结构-链表实现堆栈 控制插入的方式,成为有序链表
- getSG