您的位置:首页 > 编程语言 > Java开发

单链表-java

2015-11-09 10:01 267 查看
如果您发现代码有任何问题,请及时留言,或者QQ498442999与我讨论,期待与您一起进步~

//链表类
class LinkList
{
// 结点类
class Node
{
int data;
Node next;

// 构造函数
Node()
{

}

// 构造函数
Node(int data)
{
this.data = data;
}
}

Node first;
Node r;

// 构造函数 初始化链表
public LinkList()
{
first = new Node();
r = first;
}

// 构造函数 尾插法建表
public LinkList(int[] a)
{
this(); // 构造函数之间的调用,指this.LinkList()
for (int i = 0; i < a.length; i++)
{
AddNodeR(a[i]);
}
}

// 向表尾添加一个数据项为data节点
public void AddNodeR(int data)
{
Node n = new Node(data);
r.next = n;
r = n;
}

// 向表头添加一个数据项为data的节点
public void AddNodeF(int data)
{
Node n = new Node(data);
n.next = first.next;
first.next = n;
}

// 在第i个位置添加一个节点
public void AddNodeI(int i, int data)
{
if (i <= 0)
{
System.out.println("Error:超出索引");
} else
{
Node current = first;
int j = 0;
while (j < i - 1 && current != null)
{
j++;
current = current.next;
}
if (current == null || current.next == null)
{
System.out.println("Error:超出索引");
} else
{
Node n = new Node(data);
n.next = current.next;
current.next = n;
}
}
}

// 删除第i个位置上的结点
public void DeleteNode(int i)
{
if (i <= 0)
{
System.out.println("Error:超出索引");
} else
{
Node current = first;
int j = 0;
while (j < i - 1 && current != null)
{
j++;
current = current.next;
}
if (current == null || current.next == null)
{
System.out.println("Error:超出索引");
} else
{
if (current.next == r)
{
r = current;
}
current.next = current.next.next;
}
}
}

// 打印链表所有元素
public void DispList()
{
Node current = first.next;
while (current != null)
{
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}

// 判断链表是否为空表
public boolean ListEmpty()
{
if (first.next == null)
return true;
else
return false;
}

// 获取链表元素个数
public int ListLength()
{
int n = 0;
Node current = first.next;
while (current != null)
{
n++;
current = current.next;
}
return n;
}

// 获取第i个元素的值
public int GetElem(int i)
{
Node current = first.next;
int j = 1;
while (j < i && current != null)
{
j++;
current = current.next;
}
if (j == i)
return current.data;
else
{
System.out.print("Error:超出索引 ");
return 0;
}
}

// 查找数据项为data的结点第一次出现的位置
public int LocateNode(int data)
{
Node current = first.next;
int i = 1;
while (current != null)
{
if (current.data == data)
return i;
i++;
current = current.next;
}
return 0;
}
}

public class MyLinkList
{
public static void main(String args[])
{
int[] a = { 1, 2, 3, 4, 5 };
System.out.println("创建链表,并初始化");
LinkList L = new LinkList();
System.out.println("此时链表为空表?" + L.ListEmpty() + " 长度为:" + L.ListLength());
System.out.println("重新实例化链表,并调用构造函数插入数组a={1,2,3,4,5}");
L = new LinkList(a);
System.out.print("此时链表为空表?" + L.ListEmpty() + " 长度为:" + L.ListLength() + " 打印链表:");
L.DispList();
System.out.println("链表第3位的值:" + L.GetElem(3));
System.out.println("结点数据项为3的结点在链表中第一次出现的次序:" + L.LocateNode(3));
System.out.println("在第3位添加32");
L.AddNodeI(3, 32);
System.out.print("链表为空表?" + L.ListEmpty() + " 长度为:" + L.ListLength() + " 打印链表:");
L.DispList();
System.out.println("删除第3位的结点");
L.DeleteNode(3);
System.out.print("链表为空表?" + L.ListEmpty() + " 长度为:" + L.ListLength() + " 打印链表:");
L.DispList();
System.out.println("在表头插入数据项为0的结点");
L.AddNodeF(0);
System.out.print("链表为空表?" + L.ListEmpty() + " 长度为:" + L.ListLength() + " 打印链表:");
L.DispList();
System.out.println("在尾插入数据项为6的结点");
L.AddNodeR(6);
System.out.print("链表为空表?" + L.ListEmpty() + " 长度为:" + L.ListLength() + " 打印链表:");
L.DispList();

L = null; // 销毁链表
}
}


输出:

创建链表,并初始化

此时链表为空表?true 长度为:0

重新实例化链表,并调用构造函数插入数组a={1,2,3,4,5}

此时链表为空表?false 长度为:5 打印链表:1 2 3 4 5

链表第3位的值:3

结点数据项为3的结点在链表中第一次出现的次序:3

在第3位添加32

链表为空表?false 长度为:6 打印链表:1 2 32 3 4 5

删除第3位的结点

链表为空表?false 长度为:5 打印链表:1 2 3 4 5

在表头插入数据项为0的结点

链表为空表?false 长度为:6 打印链表:0 1 2 3 4 5

在尾插入数据项为6的结点

链表为空表?false 长度为:7 打印链表:0 1 2 3 4 5 6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: