(学习java)带有头结点的单向循环链表

来源:互联网 发布:js获取当前数组下标 编辑:程序博客网 时间:2024/05/18 00:24
import java.util.Scanner;//头结点的单向循环链表public class SLineListNode {public static void main(String[] args) {System.out.println("1.查看链表");System.out.println("2.创建链表");System.out.println("3.清空链表");System.out.println("4.查询结点");System.out.println("5.删除结点");System.out.println("6.合并链表");System.out.println("0.退出程序");System.out.println("====================");Scanner sc = new Scanner(System.in);LineList L = new LineList();boolean bool = true;while(bool){System.out.println("请输入操作数 : ");int  n = sc.nextInt();switch(n){case 0:bool = false;break;case 1:L.list();continue;case 2:System.out.println("请选择:1.插入尾结点  2.插入任意结点");Scanner ss = new Scanner(System.in);System.out.println("请选择使用哪种方法:");int m = ss.nextInt();if(m == 1){System.out.println("当输入链表的长度");int size = ss.nextInt();while(L.size<size){System.out.println("请输入插入的结点值:");int num1 = ss.nextInt();L.addNode(num1);}}else if(m == 2){System.out.println("链表的长度" + L.size);System.out.println("请输入要插入的链表位置");int local = ss.nextInt();System.out.println("请输入要插入的结点值:");int num2 = ss.nextInt();L.insert(local, num2);}else{System.out.println("输入的方法错误!");}continue;case 3:L.clearList();continue;case 4:System.out.println("请输入你要查询的结点位置");int local2 = sc.nextInt();L.search(local2);continue;case 5:System.out.println("链表的长度" + L.size);System.out.println("请输入你要删除的结点位置");int local3 = sc.nextInt();int num3 = L.delete(local3);System.out.println("删除的结点值为" + num3);continue;case 6:System.out.println("请创建想要合并的链表");LineList L2 = new LineList();System.out.println("请选择:1.插入尾结点  2.插入任意结点");Scanner ss1 = new Scanner(System.in);System.out.println("请选择使用哪种方法:");int m1 = ss1.nextInt();if(m1 == 1){System.out.println("当输入链表的长度");int size = ss1.nextInt();while(L2.size<size){System.out.println("请输入插入的结点值:");int num1 = ss1.nextInt();L2.addNode(num1);}}else if(m1 == 2){System.out.println("链表的长度" + L.size);System.out.println("请输入要插入的链表位置");int local = ss1.nextInt();System.out.println("请输入要插入的结点值:");int num2 = ss1.nextInt();L2.insert(local, num2);}else{System.out.println("输入的方法错误!");}System.out.println("您创建的链表");L2.list();L.connect(L2);System.out.println("合并后的链表");L.list();continue;default:System.out.println("错误的输出值");}}}// 定义结点类public static class Node {private int data;private Node next;public Node() {}public Node(int data) {this.data = data;this.next = null;}}public interface SList {public void list();public void addNode(int data);public void init();public void insert(int i, int data);public int search(int i);public int delete(int i);public void clearList();public void connect(LineList L);}public static class LineList implements SList {// 定义头结点private Node head;private Node rear;private int size;public LineList(){head = new Node();size = 0;}@Override// 初始化循环表public void init() {head = new Node();size = 0;}@Override// 添加循环表结点public void addNode(int data) {if (head.next == null) {Node node = new Node(data);head.next = node;rear = node;rear.next = head;size++;} else {Node temp = head;while (temp != rear) {temp = temp.next;}Node node = new Node(data);temp.next = node;rear = node;rear.next = head;size++;}}// 循环链表的遍历public void list() {if (head.next == null) {System.out.println("空链表");System.out.println("链表的长度" + size);} else if (head.next == rear) {System.out.println(head.next.data);System.out.println("链表的长度" + size);} else {Node temp = head;while (temp.next != rear) {temp = temp.next;System.out.print(temp.data + "->");}temp = temp.next;System.out.println(temp.data);System.out.println("链表的长度" + size);}}@Override// 循环链表的插入public void insert(int i, int data) {int j = 1;if (i < 1 || i > size + 1) {System.out.println("错误的插入位置i!");} else {Node temp = head;while (temp != rear && j < i) {temp = temp.next;j++;}// 插入的结点正好在尾端if (temp == rear) {Node node = new Node(data);temp.next = node;rear = node;rear.next = head;size++;} else {Node node = new Node(data);node.next = temp.next;temp.next = node;size++;}}}@Override// 搜索单链表指定i的结点并返回该值public int search(int i) {int j = 0;if (i < 1 || i > size) {System.out.println("错误的输入i!");return 0;} else {Node temp = head;while (temp != rear && j < i) {temp = temp.next;j++;}int n = temp.data;return n;}}@Override// 删除指定i位置的结点并返回该节点的值public int delete(int i) {int j = 1;if (i < 1 || i > size) {System.out.println("错误的输入i!");return 0;} else {Node temp = head;while (temp.next != rear && j < i) {temp = temp.next;j++;}if (temp.next == rear) {int n = rear.data;rear = null;rear = temp;rear.next = head;size--;return n;} else {Node p = temp.next;int n = temp.next.data;temp.next = temp.next.next;p = null;size--;return n;}}}@Override// 请空链表public void clearList() {// 直接清空头,尾结点后初始化/* * head = null; rear = null; init(); */// 将每个结点清空Node temp;while (head.next != rear) {temp = head.next;head.next = head.next.next;temp = null;}rear = null;head = null;init();}@Override// 将第二个循环链表连接到第一个循环链表上public void connect(LineList L) {if (head == null || L.head == null) {System.out.println("链表不是循环链表~");} else {Node node = new Node(rear.data);Node temp = head;while (temp.next != rear) {temp = temp.next;}temp.next = node;rear = null;node.next = L.head.next;L.head = null;rear = L.rear;rear.next = head;size += L.size;}}}}

阅读全文
0 0
原创粉丝点击