首页 > 编程知识 正文

数据结构单向循环链表,单链表双向链表循环链表

时间:2023-05-04 15:53:08 阅读:240696 作者:1959

1.双向链表的定义

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。

2.双向链表的插入与删除操作 插入

删除

代码实现 public class DoubleLinked { private class Node{ public int getData() { return data; } public void setData(int data) { this.data = data; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } private int data; private Node next; private Node prior; public Node getPrior() { return prior; } public void setPrior(Node prior) { this.prior = prior; } public Node(int data, Node next,Node prior){ this.data = data; this.next = next; this.prior = prior; } } private Node head; //头插法 public void headinsert (int data){ Node node = new Node(data,null,null); if (null == this.head){ this.head = node; return; } node.setNext(this.head); this.head.setPrior(node); this.head = node; } //尾插法 public void tailinsert(int data){ Node node = new Node(data,null,null); if (null == this.head){ this.head = node; return; } Node p = this.head; while (null != p.getNext()){ p = p.getNext(); } node.setPrior(p); p.setNext(node); } //中间插入法 public void midinsert(int data){ Node node = new Node(data,null,null); if (null == this.head){ this.head = node; return; } if (this.head.getData() > data){ node.setNext(this.head); this.head.setPrior(node); this.head = node; return; } Node p = this.head; while (null != p.getNext()){ if (p.getNext().getData() > data ){ break; } p = p.getNext(); } if (null == p.getNext()) { node.setPrior(p); p.setNext(node); return; } node.setPrior(p); node.setNext(p.getNext()); p.getNext().setPrior(node); p.setNext(node); } //删除操作 public void remove(int data) { if (null == this.head) { System.out.println("链表为空!"); return; } if (this.head.getData() == data) { this.head = this.head.getNext(); print(); return; } Node p = this.head; while (null != p.getNext()) { if (p.getNext().getData() == data) { break; } p = p.getNext(); } if (null == p.getNext()) { System.out.println(data + " 不存在!"); } else { p.setNext(p.getNext().getNext()); if (null != p.getNext()) { p.getNext().setPrior(p); } print(); }// p.getPrior().setNext(p.getNext());// p.getNext().setPrior(p.getPrior()); } //打印链表 public void print(){ if (null == this.head){ return; } Node p = this.head; while(null != p){ System.out.print(p.getData()+" "); p = p.getNext(); } System.out.println(); } public static void main(String[] args) { DoubleLinked list = new DoubleLinked(); list.headinsert(5); list.headinsert(3); list.headinsert(7); list.headinsert(2);// list.midinsert(11);// list.midinsert(5);// list.midinsert(3);// list.midinsert(9); list.tailinsert(4); list.tailinsert(1); list.tailinsert(6); list.tailinsert(8); list.print(); System.out.println("删除后------"); list.remove(8); }}

输出结果:

2 7 3 5 4 1 6 8 删除后------2 7 3 5 4 1 6 3.循环链表的定义

将单链表中终端结点的指针端自空指针改为指向头结点,就使整个单链表形成一个环,这种头尾:相接的单链表称为单循环链表,简称循环链表( circular linkedlist) 。

单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形成一个环。

循环单链表的优点是从链尾到链头比较方便。当要处理的数据元素序列具有环型结构特点时,适合于采用循环单链表。

4.循环链表的插入与删除代码实现 public class CircleLinked { private class Node{ public int getData() { return data; } public void setData(int data) { this.data = data; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } private int data; private Node next; private Node prior; public Node getPrior() { return prior; } public void setPrior(Node prior) { this.prior = prior; } public Node(int data, Node next,Node prior){ this.data = data; this.next = next; this.prior = prior; } } Node head ; Node tail ; //头插法(可变参数) public void headInsert(int... data){ if (null == data || 0 == data.length) { return; } for (int d : data) { Node node = new Node(d,null,null); if (head == null){ head = node; tail = node; continue; } node.setNext(head); head = node; tail.setNext(head); } } //尾插法(可变参数) public void tailInsert(int... data) { if (null == data || 0 == data.length) { return; } for (int d : data) { Node node = new Node(d, null, null); if (head == null) { head = node; this.head.setNext(this.head); tail = node; continue; } Node p = this.head; while (head != p.getNext()) { p = p.getNext(); } node.setPrior(p); p.setNext(node); tail.setNext(head); } } //判断是否为循环链表 public boolean isCircle(){ Node fast = head; Node slow = head; if (head == null || head.getNext() == null){ return false; } while(null != head){ fast = fast.getNext().getNext(); slow = slow.getNext(); if (fast == slow){ break; }else if (fast == null || fast.getNext() == null){ return false; } } return true; } //删除节点 public void remove(int data){ if (null == this.head ){ System.out.println("链表为空!"); return; } if (this.head.getData() == data){ this.head = this.head.getNext(); this.tail.setNext(head); return; } Node p = this.head; while(head != p.getNext()){ if (p.getNext().getData() == data){ break; } p = p.getNext(); } if (head != p.getNext()){ p.setNext(p.getNext().getNext()); }else{ System.out.println(data + " 不存在!"); } } //打印链表 public void print(){ if (head == null){ return; } Node p = head; while (p.getNext() != head) { System.out.println(p.getData()+" "); p = p.getNext(); } System.out.println(p.getData()); } public static void main(String[] args) { CircleLinked linked = new CircleLinked(); linked.headInsert(3,6,8,4,9,1); linked.print(); System.out.println(linked.isCircle()); linked.remove(3); linked.print(); }}



本人才疏学浅,若有错,请指出
谢谢 !

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。