无头结点的单链表(java实现)
来源:互联网 发布:centos 32位下载 编辑:程序博客网 时间:2024/04/30 01:31
import java.util.Random;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
class List {
Node first = null;
List(int n) {
Random rand = new Random(96);
for (int i = 0; i < n; i++) {
insert(rand.nextInt() %100);
}
}
public void insert(int data) {
Node node = new Node(data);
Node tmp;
if (first == null) {
first = node;
return;
}
if (first.data < data) {
node.next = first;
first = node;
return;
}
tmp = first;
while (tmp.next != null) {
if (data > tmp.next.data)
break;
tmp = tmp.next;
}
node.next = tmp.next;
tmp.next = node;
}
public void delete(int data) {
Node tmp;
if (data == first.data) {
first = first.next;
return;
}
tmp = first;
while (tmp.next != null) {
if (data == tmp.next.data) {
tmp.next = tmp.next.next;
return;
}
tmp = tmp.next;
}
}
public void reverse1(){
Node pre, cur, last;
pre = null;
cur = first;
while (cur != null) {
last = cur.next;
cur.next = pre;
pre = cur;
cur = last;
}
first = pre;
}
public void reverse2(){
Node cur, last;
cur = first;
if (cur == null || cur.next == null)
return;
while (cur.next.next != null)
cur = cur.next;
last = cur.next;
cur.next = null;
reverse2();
last.next = cur;
first = last;
}
public void reverse3() {
Node cur, last;
cur = first;
if(cur == null || cur.next == null)
return;
last = cur.next;
first = last;
reverse3();
last.next = cur;
cur.next = null;
}
public void show() {
Node tmp;
tmp = first;
while (tmp != null) {
System.out.print(tmp.data + " ");
tmp = tmp.next;
}
System.out.println();
}
}
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
class List {
Node first = null;
List(int n) {
Random rand = new Random(96);
for (int i = 0; i < n; i++) {
insert(rand.nextInt() %100);
}
}
public void insert(int data) {
Node node = new Node(data);
Node tmp;
if (first == null) {
first = node;
return;
}
if (first.data < data) {
node.next = first;
first = node;
return;
}
tmp = first;
while (tmp.next != null) {
if (data > tmp.next.data)
break;
tmp = tmp.next;
}
node.next = tmp.next;
tmp.next = node;
}
public void delete(int data) {
Node tmp;
if (data == first.data) {
first = first.next;
return;
}
tmp = first;
while (tmp.next != null) {
if (data == tmp.next.data) {
tmp.next = tmp.next.next;
return;
}
tmp = tmp.next;
}
}
public void reverse1(){
Node pre, cur, last;
pre = null;
cur = first;
while (cur != null) {
last = cur.next;
cur.next = pre;
pre = cur;
cur = last;
}
first = pre;
}
public void reverse2(){
Node cur, last;
cur = first;
if (cur == null || cur.next == null)
return;
while (cur.next.next != null)
cur = cur.next;
last = cur.next;
cur.next = null;
reverse2();
last.next = cur;
first = last;
}
public void reverse3() {
Node cur, last;
cur = first;
if(cur == null || cur.next == null)
return;
last = cur.next;
first = last;
reverse3();
last.next = cur;
cur.next = null;
}
public void show() {
Node tmp;
tmp = first;
while (tmp != null) {
System.out.print(tmp.data + " ");
tmp = tmp.next;
}
System.out.println();
}
}
- 无头结点的单链表(java实现)
- 实现无头结点单链表的基本操作函数
- 关于有头结点和无头结点的单链表
- 单链表实现(C语言版,无头结点)
- c实现无头结点单链表
- 有头结点的单链表(java实现)
- 单链表(无头结点)
- 无头结点单链表
- 单链表的创建(有头结点和无头结点)
- 无头结点的链表逆置
- C++实现无头结点的双向链表
- 数据结构试验二 线性表实现(无头结点)
- 链栈的实现(单指针 无头结点) C语言版
- 分别用头插法和尾插法建立有头结点和无头结点的单链表
- 单链表(无头节点)的实现
- 链表的归并(无头结点,c语言)
- (学习java)没有头结点的单链表
- 单链表的建立(头部延长、尾部延长)、插入操作、删除操作(无头结点的删除、有头结点的删除)
- 有头结点的单链表(java实现)
- Android中抓取手机视频流数据
- addChildAt()的使用——可视化组件
- Beyond Compare添加到右键菜单
- HDU 1080 最长公共子序列变形
- 无头结点的单链表(java实现)
- python 文件操作(二)
- 向juddiv3注册服务
- java.lang.NoClassDefFoundError: javax/el/ExpressionFactory
- Android App集成支付宝
- poj3281 - Dining
- mvc中json格式的使用方法示例
- Java注解(2)
- Hibernate中复合主键映射