JAVA 实现一个链表
class Node {
public int data;
public Node next;
public Node() {
data = 0;
next = null;
}
public Node(int d, Node n) {
data = d;
next = n;
}
public void setData(int data) {
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
Node getNext() {
return next;
}
int getData() {
return data;
}
}
class LinkList {
private Node head, end, current;
LinkList() {
head = end = current = null;
}
void print() {
if (head == null) {
System.out.println("The list is empty!");
System.exit(0);
} else {
current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}
}
void insertTail(int d) {
Node newnode = new Node(d, null);
if (head == null) {
head = newnode;
end = newnode;
newnode.next = null;
} else {
end.next = newnode;
end = newnode;
end.next = null;
}
}
void insertHead(int d) {
Node newnode = new Node(d, null);
if (head == null) {
head = newnode;
end = newnode;
newnode.next = null;
} else {
newnode.next = head;
head = newnode;
}
}
int length() {
if (head == null)
return 0;
else {
int len = 0;
current = head;
while (current != null) {
len++;
current = current.next;
}
return len;
}
}
int search(int d) {
int index;
if (head == null) {
System.out.println("Search in a empty list is illeagal!");
System.exit(1);
} else {
index = 0;
current = head;
while (current != null) {
index++;
if (current.data == d)
break;
current = current.next;
}
if(current != null)
return index;
}
return -1;
}
public static void main(String args[]) {
LinkList one = new LinkList();
for (int i = 10; i < 20; i++)
one.insertTail(i);
one.print();
System.out.println();
System.out.println(one.search(16));
}
}
分享到:
相关推荐
AWS_中文入门开发教学_-_申请AWS账号_-_aws_signup_p.03
ITU-T P.1140提供了车载紧急呼叫系统的语音通讯测量技术和测试方法,以符合车舱内免提通讯的特定要求。
小红伞P版KEY 截止到2014.03.03,截止2013年11月19日仍然可用
h43ia1_1.00.03.h(K470P-I7-D2 BIOS)
HPE 服务器Gen10&Gen10Plus SPP Gen10 Service Pack for ProLiant (SPP) V2022.03.0
PhoenixP2P服务器版 2014.03.30更新: [1]p2p组网引擎接口部分加入安全上下限检查. [2]修改超范围调用的日志记录功能。 [3]增加超范围参数的实时统计功能。 [4]修复Phoenix引擎ip交换参数被错误关闭的问题. ...
PrimeTime® User Guide Version P-2019.03-SP2, June 2019.pdf
P/N:110401107240X DATE:2018.05.03 REV
NewPart.pNewPart.p NewPart.pNewPart.p NewPart.p NewPart.pNewPart.pNewPart.p NewPart.pNewPart.p NewPart.p NewPart.p
南海移动03年联谊会活动策划方案.ppt.baiduyun.p.downloading
酷派5910_4.0.048.P1.121210.5910.CPB.part03.rar(要5个文件放到一起解压缩)
ANSI ISA 95.00.03-2013 Enterprise-Control System Integration - P
汉博研究院2011年03月福州泰禾城市广场项目市场研究及定位报告.pdf.baiduyun.p.downloading
我的世界bsl7.1光影,低配机的福音!!!
React快速上手开发 ,Stoyan Stefanov ,P208 ,2017.03—正——PDF电子书下载+带书签目录+高清完整版 (1).pdf
XXXX03西安航天科技园项目定位策划报告95P.pptx
104、例题 2 最敏捷的机器人《信息学奥赛一本通提高篇》(2020.03.23)a
现场问题处理清单
ISA 95.00.03-2013.xdf