java判断环形链表的可视化代码
时间: 2023-07-12 10:11:27 浏览: 53
以下是一个简单的 Java 代码示例,用于可视化设计环形链表的结构:
```java
import javax.swing.*;
import java.awt.*;
public class CircularLinkedListVisualization extends JFrame {
private LinkedListNode startNode;
private int nodeCount;
public CircularLinkedListVisualization(LinkedListNode startNode, int nodeCount) {
this.startNode = startNode;
this.nodeCount = nodeCount;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int radius = Math.min(centerX, centerY) - 50;
double angleStep = 2 * Math.PI / nodeCount;
LinkedListNode currentNode = startNode;
for (int i = 0; i < nodeCount; i++) {
int x = (int) (centerX + radius * Math.cos(i * angleStep));
int y = (int) (centerY + radius * Math.sin(i * angleStep));
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillOval(x - 20, y - 20, 40, 40);
g2d.setColor(Color.BLACK);
g2d.drawOval(x - 20, y - 20, 40, 40);
g2d.drawString(String.valueOf(currentNode.getValue()), x, y);
currentNode = currentNode.getNext();
}
g2d.setColor(Color.BLACK);
g2d.drawOval(centerX - radius - 20, centerY - radius - 20, (radius + 20) * 2, (radius + 20) * 2);
g2d.drawString("Start", centerX - radius - 20, centerY - radius - 40);
g2d.drawString("End", centerX + radius + 20, centerY + radius + 40);
g2d.drawLine(centerX - radius - 20, centerY, centerX - radius, centerY);
g2d.drawLine(centerX + radius + 20, centerY, centerX + radius, centerY);
}
public static void main(String[] args) {
LinkedListNode startNode = new LinkedListNode(1);
LinkedListNode node2 = new LinkedListNode(2);
LinkedListNode node3 = new LinkedListNode(3);
LinkedListNode node4 = new LinkedListNode(4);
startNode.setNext(node2);
node2.setNext(node3);
node3.setNext(node4);
node4.setNext(startNode);
CircularLinkedListVisualization visualization = new CircularLinkedListVisualization(startNode, 4);
}
}
class LinkedListNode {
private int value;
private LinkedListNode next;
public LinkedListNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public LinkedListNode getNext() {
return next;
}
public void setNext(LinkedListNode next) {
this.next = next;
}
}
```
这个示例代码创建了一个环形链表,其中的四个节点形成了一个环形链表。节点使用圆形的形状,箭头表示节点之间的关系。通过 `JFrame` 类和 `Graphics` 类将图形显示出来。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)