java实现链表
来源:互联网 发布:淘宝banner图片 编辑:程序博客网 时间:2024/05/29 18:32
package 剑指Offer;
public class LinkedList {
public Node pHead;
public static void main(String[] args) {
LinkedList ll = new LinkedList();
for(int i = 0; i < 10; i++) {
ll.insert(i);
}
ll.delete(0);
ll.print();
}
public void insert(int value) {
if(pHead == null) {
pHead = new Node(value);
}
else {
Node temp = pHead;
while(temp.next != null) {
temp = temp.next;
}
temp.next = new Node(value);
}
}
public void delete(int value) {
if(pHead.value == value) {
pHead = pHead.next;
return;
}
Node temp = pHead;
while(temp.next != null && temp.next.value != value) {
temp = temp.next;
}
if(temp.next != null && temp.next.value == value) {
temp.next = temp.next.next;
} else {
System.out.println("Not Found!");
}
}
public void print() {
if(pHead == null) {
return;
}
System.out.print(pHead.value + " ");
while(pHead.next != null) {
pHead = pHead.next;
System.out.print(pHead.value + " ");
}
}
class Node {
int value;
Node next;
Node(int data) {
value = data;
}
}
}
public class LinkedList {
public Node pHead;
public static void main(String[] args) {
LinkedList ll = new LinkedList();
for(int i = 0; i < 10; i++) {
ll.insert(i);
}
ll.delete(0);
ll.print();
}
public void insert(int value) {
if(pHead == null) {
pHead = new Node(value);
}
else {
Node temp = pHead;
while(temp.next != null) {
temp = temp.next;
}
temp.next = new Node(value);
}
}
public void delete(int value) {
if(pHead.value == value) {
pHead = pHead.next;
return;
}
Node temp = pHead;
while(temp.next != null && temp.next.value != value) {
temp = temp.next;
}
if(temp.next != null && temp.next.value == value) {
temp.next = temp.next.next;
} else {
System.out.println("Not Found!");
}
}
public void print() {
if(pHead == null) {
return;
}
System.out.print(pHead.value + " ");
while(pHead.next != null) {
pHead = pHead.next;
System.out.print(pHead.value + " ");
}
}
class Node {
int value;
Node next;
Node(int data) {
value = data;
}
}
}
0 0
- JAVA实现链表
- Java实现链表
- Java实现链表
- java实现链表
- Java 链表实现
- Java实现链表
- java 链表实现
- java实现链表
- java 实现链表
- java实现链表
- java实现链表
- Java实现链表
- Java实现链表
- Java链表实现
- java链表实现
- java 链表实现
- JAVA实现链表
- java实现链表
- 自定义Java注解的方式与应用
- javaweb学习(6)--Session
- C语言学习:结构体(笔记)
- Mysql主从数据库配置
- javaweb学习(7)--Application/servletContext
- java实现链表
- Altium designer窗口杂乱?给你两支招
- 尝试做新事情30天-阅读架构即未来01
- 矩阵,向量求导问题
- MVC
- springboot学习之-helloword
- vpn简介
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 数学、A题第三次被系统hack了 (┬_┬)、集合交集
- Node.js JSON模块