0
点赞
收藏
分享

微信扫一扫

单链表、双链表和环形链表(相关知识点)


链表

数据结构中有一种叫链表的,它不像数组有空间限制,可以无限(内存范围内)添加元素
1.单链表

单链表的存储如下

单链表、双链表和环形链表(相关知识点)_单链表


(1)链表是以结点来一个个存储的链式存储

(2)他有两个域一个数据域用来存储数据,还有一个指针域用来存储下一个结点的存储的位置

(3)链表有带有头节点的链表,也有不带头结点的链表

带头节点的单链表如下

单链表、双链表和环形链表(相关知识点)_数据结构_02

核心代码如下

public class Linkedlist {
//定义头结点
private Node head = new Node(-1, null);

//添加结点
public void addNode(Node headnode) {
//定义一个临时变量来遍历链表
Node temp = head;
while (true) { //链表末尾
if (temp == null) {
break;
}
temp = temp.next;
}
//添加
temp.next = headnode;
}

//添加有排序的结点
public void addboNode(Node headNode) {
//定义一个临时变量来遍历链表
Node temp = head;
//做一个标记
boolean flag = false;
while (true) {
//链表末尾
if (temp.next == null) {
break;
}
//看是否找到
if (temp.data > headNode.data) {
break;
}
//找到
else if (temp.data == headNode.data) {
flag = true;
break;
}

}
if (flag) {
throw new RuntimeException("已经存在");
} else {
headNode.next = temp.next;
temp.next = headNode;
}
}

//更新结点
public void update(Node headnode) {
Node temp = head;
boolean flag = false;
//链表是否为空
if (temp.next == null) {
throw new RuntimeException("链表为空");
}
while (true) {
if (temp == null) {
break;
}
if (temp.data == headnode.data) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.data = headnode.data;
} else {
System.out.println("没有找到该结点");
}

}

//删除结点
public void del(int a) {
Node temp = head;
if (temp.next == null) {
throw new RuntimeException("链表为空");
}
boolean flag = false;

while (true) {
if (temp == null) {
break;
}
if (temp.data == a) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else {
System.out.println("没有找到");
}
}

//遍历链表
public void list() {
Node temp = head;
if (temp.next == null) {
throw new RuntimeException("链表为空");
}
while (true) {
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}
//内部结点类
class Node {
//数据域
private int data;
//指针域
private Node next;

public Node(int data, Node next) {
this.data = data;
this.next = next;
}

@Override
//方便查看
public String toString() {
return "data=" + data ;

}

}


}

2.双链表
双链表跟单链表基本一样,改变的就是遍历的时候可以双向遍历,删除的时候可以自删除,不需要那个临时结点。

核心代码如下

public class DoubleLinkedlist {

//定义头结点
private Node head=new Node(-1);

//获取头节点
public Node getHead()
{
return head;
}

//遍历链表
public void list() {
Node temp = head;
if (temp.next == null) {
throw new RuntimeException("链表为空");
}
while (true) {
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}

//添加结点
public void addNode(Node headnode) {
//定义一个临时变量来遍历链表
Node temp = head;
while (true) { //链表末尾
if (temp == null) {
break;
}
temp = temp.next;
}
//添加
temp.next = headnode;
headnode.pre=temp;
}

//更新结点
public void update(Node headnode) {
Node temp = head;
boolean flag = false;
//链表是否为空
if (temp.next == null) {
throw new RuntimeException("链表为空");
}
while (true) {
if (temp == null) {
break;
}
if (temp.data == headnode.data) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.data = headnode.data;
} else {
System.out.println("没有找到该结点");
}

}

//删除结点
public void del(int a) {
Node temp = head;
if (temp.next == null) {
throw new RuntimeException("链表为空");
}
boolean flag = false;

while (true) {
if (temp == null) {
break;
}
if (temp.data == a) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next=temp.next.next;
temp.pre.next=temp.next;

}else if (temp.next!=null)
{
temp.next.pre=temp.pre;
}
else {
System.out.println("没有找到");
}
}


}

//结点类
class Node{
//数据域
public int data;
//向后指针
public Node next;
//向前指针
public Node pre;

public Node(int data)
{
this.data=data;
}

@Override
//方便查看
public String toString() {
return "data=" + data;
}

3.环形链表

环形的链表构成一个圈,尾结点不再指向空指针,指向第一个结点从而构成一个环

核心代码如下

public class circleLinkedlist {

//定义初始结点
private Node firstNode=null;
//添加结点
public void addNode(int a)
{
if (a<1)
{
throw new RuntimeException("数据有误");
}
//用来构成环形的辅助
Node curnode=null;
for (int i = 1; i <=a ; i++) {

Node node=new Node(i);
//第一个结点
if (i==1)
{
firstNode=node;
firstNode.next=firstNode;
curnode=firstNode;
}else { //非第一个结点
curnode.next=node;
node.next=firstNode;
curnode=node;
}

}

}

//遍历链表
public void show()
{
if (firstNode==null)
{
throw new RuntimeException("链表为空");
}
Node curNode=firstNode;
while (true)
{
System.out.println(curNode);
if (curNode.next==firstNode)
{
break;
}
curNode=curNode.next;
}
}
//删除结点
public void putNode(int a)
{
if (firstNode==null)
{
throw new RuntimeException("链表为空");
}
//辅助变量
Node cur=firstNode;
while (true)
{
if (cur.next==firstNode)
{
break;
}
if (cur.next.data==a)
{
//去掉一个结点
cur.next=cur.next.next;
break;
}

}

}


}


//结点类
class Node {
//数据域
public int data;
//向后指针
public Node next;

public Node(int data) {
this.data = data;
}

@Override
//方便查看
public String toString() {
return "data=" + data;
}
}

单链表、双链表和环形链表(相关知识点)_单链表_03


举报

相关推荐

0 条评论